potentially fixes #338
authorMax Countryman <max.countryman@gmail.com>
Fri, 9 Mar 2012 22:26:57 +0000 (17:26 -0500)
committerMax Countryman <max.countryman@gmail.com>
Fri, 9 Mar 2012 22:30:35 +0000 (17:30 -0500)
This attempts to fix an issue where encoding of a string might fail when the
encoding is set to some unknown format. Here we attempt to catch the
LookupException and subsequently blindly encode the string one final time.
That is we call str() over response.content without specifying an encoding.
This may still fail in certain cases but does properly handle the case of #338
by returning the expected string.

requests/models.py

index 753e83ab2c4aac0dfc3a8c8f6ae340ee6a1f48ff..4bccb1e317f89c9c5d0cf248005479721d99ea3d 100644 (file)
@@ -788,6 +788,9 @@ class Response(object):
         # Decode unicode from given encoding.
         try:
             content = str(self.content, encoding, errors='replace')
+        except LookupError:
+            # try blindly encoding
+            content = str(self.content, errors='replace')
         except (UnicodeError, TypeError):
             pass