[Python-checkins] bpo-36582: Make collections.UserString.encode() return bytes, not str (GH-13138) (GH-15557)
Raymond Hettinger
webhook-mailer at python.org
Wed Aug 28 00:59:58 EDT 2019
https://github.com/python/cpython/commit/2cb82d2a88710b0af10b9d9721a9710ecc037e72
commit: 2cb82d2a88710b0af10b9d9721a9710ecc037e72
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Raymond Hettinger <rhettinger at users.noreply.github.com>
date: 2019-08-27T21:59:54-07:00
summary:
bpo-36582: Make collections.UserString.encode() return bytes, not str (GH-13138) (GH-15557)
(cherry picked from commit 2a16eea71f56c2d8f38c295c8ce71a9a9a140aff)
Co-authored-by: Daniel Fortunov <asqui at users.noreply.github.com>
files:
A Misc/NEWS.d/next/Library/2019-05-07-17-42-36.bpo-36582.L_dxR6.rst
M Lib/collections/__init__.py
M Lib/test/test_userstring.py
M Misc/ACKS
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index 469690be8c8e..cadf1c72f08d 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -1200,12 +1200,10 @@ def count(self, sub, start=0, end=_sys.maxsize):
if isinstance(sub, UserString):
sub = sub.data
return self.data.count(sub, start, end)
- def encode(self, encoding=None, errors=None): # XXX improve this?
- if encoding:
- if errors:
- return self.__class__(self.data.encode(encoding, errors))
- return self.__class__(self.data.encode(encoding))
- return self.__class__(self.data.encode())
+ def encode(self, encoding='utf-8', errors='strict'):
+ encoding = 'utf-8' if encoding is None else encoding
+ errors = 'strict' if errors is None else errors
+ return self.data.encode(encoding, errors)
def endswith(self, suffix, start=0, end=_sys.maxsize):
return self.data.endswith(suffix, start, end)
def expandtabs(self, tabsize=8):
diff --git a/Lib/test/test_userstring.py b/Lib/test/test_userstring.py
index 19b0acfc760f..4d1d8b6b6fe2 100644
--- a/Lib/test/test_userstring.py
+++ b/Lib/test/test_userstring.py
@@ -51,6 +51,20 @@ def __rmod__(self, other):
str3 = ustr3('TEST')
self.assertEqual(fmt2 % str3, 'value is TEST')
+ def test_encode_default_args(self):
+ self.checkequal(b'hello', 'hello', 'encode')
+ # Check that encoding defaults to utf-8
+ self.checkequal(b'\xf0\xa3\x91\x96', '\U00023456', 'encode')
+ # Check that errors defaults to 'strict'
+ self.checkraises(UnicodeError, '\ud800', 'encode')
+
+ def test_encode_explicit_none_args(self):
+ self.checkequal(b'hello', 'hello', 'encode', None, None)
+ # Check that encoding defaults to utf-8
+ self.checkequal(b'\xf0\xa3\x91\x96', '\U00023456', 'encode', None, None)
+ # Check that errors defaults to 'strict'
+ self.checkraises(UnicodeError, '\ud800', 'encode', None, None)
+
if __name__ == "__main__":
unittest.main()
diff --git a/Misc/ACKS b/Misc/ACKS
index ab874e929931..5a8494f1a827 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -509,6 +509,7 @@ Arnaud Fontaine
Michael Foord
Amaury Forgeot d'Arc
Doug Fort
+Daniel Fortunov
Evens Fortuné
Chris Foster
John Fouhy
diff --git a/Misc/NEWS.d/next/Library/2019-05-07-17-42-36.bpo-36582.L_dxR6.rst b/Misc/NEWS.d/next/Library/2019-05-07-17-42-36.bpo-36582.L_dxR6.rst
new file mode 100644
index 000000000000..34f16fcde879
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-05-07-17-42-36.bpo-36582.L_dxR6.rst
@@ -0,0 +1 @@
+Fix ``UserString.encode()`` to correctly return ``bytes`` rather than a ``UserString`` instance.
\ No newline at end of file
More information about the Python-checkins
mailing list