From b1610df282c67cd87781be166549643e647704ee Mon Sep 17 00:00:00 2001 From: Matt Sweeney Date: Tue, 25 Sep 2012 15:35:30 -0700 Subject: [PATCH 1/2] Handle encoding of `None` when decoding unicode If encoding is None, decoding will throw the following TypeError: TypeError: unicode() argument 2 must be string, not None If this is the case, attempt to run without any set encoding --- requests/models.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/requests/models.py b/requests/models.py index f33c3c3e..305e615e 100644 --- a/requests/models.py +++ b/requests/models.py @@ -834,6 +834,11 @@ class Response(object): # # So we try blindly encoding. content = str(self.content, errors='replace') + except TypeError: + # A TypeError can be raised if encoding is None + # + # So we try blindly encoding. + content = str(self.content, errors='replace') return content From 38aced9f90cdbcdbda1cf402ef759060b08df6de Mon Sep 17 00:00:00 2001 From: Matt Sweeney Date: Wed, 26 Sep 2012 12:38:36 -0700 Subject: [PATCH 2/2] Simplify error handling when decoding unicode --- requests/models.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/requests/models.py b/requests/models.py index 305e615e..2193c6e5 100644 --- a/requests/models.py +++ b/requests/models.py @@ -828,13 +828,10 @@ class Response(object): # Decode unicode from given encoding. try: content = str(self.content, encoding, errors='replace') - except LookupError: + except (LookupError, TypeError): # A LookupError is raised if the encoding was not found which could # indicate a misspelling or similar mistake. # - # So we try blindly encoding. - content = str(self.content, errors='replace') - except TypeError: # A TypeError can be raised if encoding is None # # So we try blindly encoding.