diff --git a/requests/exceptions.py b/requests/exceptions.py index cddffd95..f2ac3a16 100644 --- a/requests/exceptions.py +++ b/requests/exceptions.py @@ -110,6 +110,8 @@ class UnrewindableBodyError(RequestException): class ConflictingHeaderError(RequestException): """Mutually exclusive request headers set""" +class UnreachableCodeError(RequestException, RuntimeError): + """Unreachable code block reached""" # Warnings diff --git a/requests/models.py b/requests/models.py index eb68289b..60e6ca94 100644 --- a/requests/models.py +++ b/requests/models.py @@ -527,7 +527,7 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin): elif is_stream and not length: self.headers['Transfer-Encoding'] = 'chunked' else: - assert False, "If body is not null, it must either have a length or be streamable" + raise UnreachableCodeError("Non-null body must have length or be streamable") elif (self.method not in ('GET', 'HEAD')) and (self.headers.get('Content-Length') is None): self.headers['Content-Length'] = '0' diff --git a/requests/utils.py b/requests/utils.py index e36d62af..e541a626 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -884,6 +884,6 @@ def rewind_body(prepared_request): def determine_if_stream(data): """Given data, determines if it should be sent as a stream. """ - is_iterable = hasattr(data, '__iter__') + is_iterable = getattr(data, '__iter__', False) is_io_type = not isinstance(data, (basestring, list, tuple, dict)) return is_iterable and is_io_type