From d376b6f78acdaa2824efe81c82b80212c802e019 Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Mon, 23 Feb 2015 00:35:47 +0200 Subject: [PATCH] Raise TypeError instead of AssertionError. assert statements will be removed if you run Python in optimized mode (e.g. with -O flag). --- gunicorn/config.py | 3 ++- gunicorn/http/wsgi.py | 9 ++++----- gunicorn/util.py | 3 ++- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/gunicorn/config.py b/gunicorn/config.py index bed4f759..ae387d31 100644 --- a/gunicorn/config.py +++ b/gunicorn/config.py @@ -268,7 +268,8 @@ class Setting(object): return self.value def set(self, val): - assert six.callable(self.validator), "Invalid validator: %s" % self.name + if not six.callable(self.validator): + raise TypeError('Invalid validator: %s' % self.name) self.value = self.validator(val) def __lt__(self, other): diff --git a/gunicorn/http/wsgi.py b/gunicorn/http/wsgi.py index fd545737..d9cef761 100644 --- a/gunicorn/http/wsgi.py +++ b/gunicorn/http/wsgi.py @@ -257,8 +257,8 @@ class Response(object): def process_headers(self, headers): for name, value in headers: - assert isinstance(name, string_types), "%r is not a string" % name - + if not isinstance(name, string_types): + raise TypeError('%r is not a string' % name) value = str(value).strip() lname = name.lower().strip() if lname == "content-length": @@ -322,9 +322,8 @@ class Response(object): def write(self, arg): self.send_headers() - - assert isinstance(arg, binary_type), "%r is not a byte." % arg - + if not isinstance(arg, binary_type): + raise TypeError('%r is not a byte' % arg) arglen = len(arg) tosend = arglen if self.response_length is not None: diff --git a/gunicorn/util.py b/gunicorn/util.py index 3f79fcbb..13e6d65d 100644 --- a/gunicorn/util.py +++ b/gunicorn/util.py @@ -503,7 +503,8 @@ def to_bytestring(value): """Converts a string argument to a byte string""" if isinstance(value, bytes): return value - assert isinstance(value, text_type) + if not isinstance(value, text_type): + raise TypeError('%r is not a string' % value) return value.encode("utf-8")