# HG changeset patch # User Ville Skyttä # Date 1472655797 -10800 # Wed Aug 31 18:03:17 2016 +0300 # Branch ascii # Node ID 9a1b4496ec55ea81ff9d2b38cf6ff6a9c3d1134b # Parent 1340e298aa7ef62ac657f31cd969a1db6a61ac6d Use 'ascii' instead of 'us-ascii' to bypass lookup machinery diff --git a/Doc/library/email.util.rst b/Doc/library/email.util.rst --- a/Doc/library/email.util.rst +++ b/Doc/library/email.util.rst @@ -184,7 +184,7 @@ is not, the string is encoded using the empty string for *language*. -.. function:: collapse_rfc2231_value(value, errors='replace', fallback_charset='us-ascii') +.. function:: collapse_rfc2231_value(value, errors='replace', fallback_charset='ascii') When a header parameter is encoded in :rfc:`2231` format, :meth:`Message.get_param ` may return a @@ -193,7 +193,7 @@ string. Optional *errors* is passed to the *errors* argument of :class:`str`'s :func:`~str.encode` method; it defaults to ``'replace'``. Optional *fallback_charset* specifies the character set to use if the one in the - :rfc:`2231` header is not known by Python; it defaults to ``'us-ascii'``. + :rfc:`2231` header is not known by Python; it defaults to ``'ascii'``. For convenience, if the *value* passed to :func:`collapse_rfc2231_value` is not a tuple, it should be a string and it is returned unquoted. diff --git a/Lib/email/_header_value_parser.py b/Lib/email/_header_value_parser.py --- a/Lib/email/_header_value_parser.py +++ b/Lib/email/_header_value_parser.py @@ -452,7 +452,7 @@ for part in self: spart = str(part) try: - spart.encode('us-ascii') + spart.encode('ascii') res.append(spart) except UnicodeEncodeError: if last_ew is None: @@ -538,7 +538,7 @@ for part in self: spart = str(part) try: - spart.encode('us-ascii') + spart.encode('ascii') res.append(spart) except UnicodeEncodeError: is_ew = True @@ -1021,7 +1021,7 @@ token_type = 'parameter' sectioned = False extended = False - charset = 'us-ascii' + charset = 'ascii' @property def section_number(self): @@ -1147,7 +1147,7 @@ # unknown character set to make it easy to find, # because otherwise unknown charset is a silent # failure. - value = value.decode('us-ascii', 'surrogateescape') + value = value.decode('ascii', 'surrogateescape') if utils._has_surrogates(value): param.defects.append(errors.UndecodableBytesDefect()) value_parts.append(value) @@ -1259,7 +1259,7 @@ def cte_encode(self, charset, policy): value = str(self) try: - value.encode('us-ascii') + value.encode('ascii') return value except UnicodeEncodeError: return _ew.encode(value, charset) diff --git a/Lib/email/message.py b/Lib/email/message.py --- a/Lib/email/message.py +++ b/Lib/email/message.py @@ -892,7 +892,7 @@ return failobj if isinstance(charset, tuple): # RFC 2231 encoded, so decode it, and it better end up as ascii. - pcharset = charset[0] or 'us-ascii' + pcharset = charset[0] or 'ascii' try: # LookupError will be raised if the charset isn't known to # Python. UnicodeError will be raised if the encoded text @@ -901,9 +901,9 @@ charset = str(as_bytes, pcharset) except (LookupError, UnicodeError): charset = charset[2] - # charset characters must be in us-ascii range + # charset characters must be in ascii range try: - charset.encode('us-ascii') + charset.encode('ascii') except UnicodeError: return failobj # RFC 2046, $4.1.2 says charsets are not case sensitive diff --git a/Lib/email/utils.py b/Lib/email/utils.py --- a/Lib/email/utils.py +++ b/Lib/email/utils.py @@ -317,7 +317,7 @@ return new_params def collapse_rfc2231_value(value, errors='replace', - fallback_charset='us-ascii'): + fallback_charset='ascii'): if not isinstance(value, tuple) or len(value) != 3: return unquote(value) # While value comes to us as a unicode string, we need it to be a bytes diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py --- a/Lib/test/test_ssl.py +++ b/Lib/test/test_ssl.py @@ -1899,7 +1899,7 @@ if support.verbose and self.server.connectionchatty: sys.stdout.write(" server: read CB tls-unique from client, sending our CB data...\n") data = self.sslconn.get_channel_binding("tls-unique") - self.write(repr(data).encode("us-ascii") + b"\n") + self.write(repr(data).encode("ascii") + b"\n") else: if (support.verbose and self.server.connectionchatty): @@ -3043,7 +3043,7 @@ s.write(b"CB tls-unique\n") peer_data_repr = s.read().strip() self.assertEqual(peer_data_repr, - repr(cb_data).encode("us-ascii")) + repr(cb_data).encode("ascii")) s.close() # now, again @@ -3065,7 +3065,7 @@ s.write(b"CB tls-unique\n") peer_data_repr = s.read().strip() self.assertEqual(peer_data_repr, - repr(new_cb_data).encode("us-ascii")) + repr(new_cb_data).encode("ascii")) s.close() def test_compression(self): diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -1749,11 +1749,11 @@ request = urllib.request.Request("http://www.example.com/") self.assertEqual(None, request.data) - opener.open(request, "1".encode("us-ascii")) + opener.open(request, "1".encode("ascii")) self.assertEqual(b"1", request.data) self.assertEqual("1", request.get_header("Content-length")) - opener.open(request, "1234567890".encode("us-ascii")) + opener.open(request, "1234567890".encode("ascii")) self.assertEqual(b"1234567890", request.data) self.assertEqual("10", request.get_header("Content-length"))