]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #26045: Add UTF-8 suggestion to error in http.client
authorMartin Panter <vadmium+py@gmail.com>
Tue, 9 Feb 2016 10:20:52 +0000 (10:20 +0000)
committerMartin Panter <vadmium+py@gmail.com>
Tue, 9 Feb 2016 10:20:52 +0000 (10:20 +0000)
Based on patch by Guido van Rossum.

Lib/http/client.py
Lib/test/test_httplib.py
Misc/NEWS

index 80c80cf576e75d31ddc21ed79251eb8fa20c57ed..24231b51b79638b35e247d53a7eb0f30e247750a 100644 (file)
@@ -146,6 +146,21 @@ _is_illegal_header_value = re.compile(rb'\n(?![ \t])|\r(?![ \t\n])').search
 _METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'}
 
 
+def _encode(data, name='data'):
+    """Call data.encode("latin-1") but show a better error message."""
+    try:
+        return data.encode("latin-1")
+    except UnicodeEncodeError as err:
+        raise UnicodeEncodeError(
+            err.encoding,
+            err.object,
+            err.start,
+            err.end,
+            "%s (%.20r) is not valid Latin-1. Use %s.encode('utf-8') "
+            "if you want to send it encoded in UTF-8." %
+            (name.title(), data[err.start:err.end], name)) from None
+
+
 class HTTPMessage(email.message.Message):
     # XXX The only usage of this method is in
     # http.server.CGIHTTPRequestHandler.  Maybe move the code there so
@@ -1124,7 +1139,7 @@ class HTTPConnection:
         if isinstance(body, str):
             # RFC 2616 Section 3.7.1 says that text default has a
             # default charset of iso-8859-1.
-            body = body.encode('iso-8859-1')
+            body = _encode(body, 'body')
         self.endheaders(body)
 
     def getresponse(self):
index d809414b6370c85c5984c018525214107d891f77..295b9fb14689bf8b20cacf7506be9041e3a9aedf 100644 (file)
@@ -1042,7 +1042,7 @@ class OfflineTest(TestCase):
         # intentionally omitted for simplicity
         blacklist = {"HTTPMessage", "parse_headers"}
         for name in dir(client):
-            if name in blacklist:
+            if name.startswith("_") or name in blacklist:
                 continue
             module_object = getattr(client, name)
             if getattr(module_object, "__module__", None) == "http.client":
index 5aa57b9233099cc1aff62d961b0c9713d9a4affa..29a09d756af3136287f91a54ad056ace0cc32b2a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -73,6 +73,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #26045: Add UTF-8 suggestion to error message when posting a
+  non-Latin-1 string with http.client.
+
 - Issue #12923: Reset FancyURLopener's redirect counter even if there is an
   exception.  Based on patches by Brian Brazil and Daniel Rocco.