[Python-checkins] cpython (merge 3.5 -> default): Issue #26045: Merge http.client error addition from 3.5
martin.panter
python-checkins at python.org
Mon Feb 8 06:57:17 EST 2016
https://hg.python.org/cpython/rev/9896ead3cc1d
changeset: 100184:9896ead3cc1d
parent: 100182:a9c9e4054f3f
parent: 100183:966bd147ccb5
user: Martin Panter <vadmium+py at gmail.com>
date: Tue Feb 09 11:57:11 2016 +0000
summary:
Issue #26045: Merge http.client error addition from 3.5
files:
Lib/http/client.py | 17 ++++++++++++++++-
Lib/test/test_httplib.py | 2 +-
Misc/NEWS | 3 +++
3 files changed, 20 insertions(+), 2 deletions(-)
diff --git a/Lib/http/client.py b/Lib/http/client.py
--- a/Lib/http/client.py
+++ b/Lib/http/client.py
@@ -146,6 +146,21 @@
_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
@@ -1173,7 +1188,7 @@
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):
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -1042,7 +1042,7 @@
# 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":
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -170,6 +170,9 @@
Library
-------
+- Issue #26045: Add UTF-8 suggestion to error message when posting a
+ non-Latin-1 string with http.client.
+
- Issue #26039: Added zipfile.ZipInfo.from_file() and zipinfo.ZipInfo.is_dir().
Patch by Thomas Kluyver.
--
Repository URL: https://hg.python.org/cpython
More information about the Python-checkins
mailing list