8000 bpo-36582: Make collections.UserString.encode() return bytes, not str… · python/cpython@2a16eea · GitHub
[go: up one dir, main page]

Skip to content

Commit 2a16eea

Browse files
asquirhettinger
authored andcommitted
bpo-36582: Make collections.UserString.encode() return bytes, not str (GH-13138)
1 parent 98d90f7 commit 2a16eea

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

Lib/collections/__init__.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,12 +1184,10 @@ def count(self, sub, start=0, end=_sys.maxsize):
11841184
if isinstance(sub, UserString):
11851185
sub = sub.data
11861186
return self.data.count(sub, start, end)
1187-
def encode(self, encoding=None, errors=None): # XXX improve this?
1188-
if encoding:
1189-
if errors:
1190-
return self.__class__(self.data.encode(encoding, errors))
1191-
return self.__class__(self.data.encode(encoding))
1192-
return self.__class__(self.data.encode())
1187+
def encode(self, encoding='utf-8', errors='strict'):
1188+
encoding = 'utf-8' if encoding is None else encoding
1189+
errors = 'strict' if errors is None else errors
1190+
return self.data.encode(encoding, errors)
11931191
def endswith(self, suffix, start=0, end=_sys.maxsize):
11941192
return self.data.endswith(suffix, start, end)
11951193
def expandtabs(self, tabsize=8):

Lib/test/test_userstring.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ def __rmod__(self, other):
5151
str3 = ustr3('TEST')
5252
self.assertEqual(fmt2 % str3, 'value is TEST')
5353

54+
def test_encode_default_args(self):
55+
self.checkequal(b'hello', 'hello', 'encode')
56+
# Check that encoding defaults to utf-8
57+
self.checkequal(b'\xf0\xa3\x91\x96', '\U00023456', 'encode')
58+
# Check that errors defaults to 'strict'
59+
self.checkraises(UnicodeError, '\ud800', 'encode')
60+
61+
def test_encode_explicit_none_args(self):
62+
self.checkequal(b'hello', 'hello', 'encode', None, None)
63+
# Check that encoding defaults to utf-8
64+
self.checkequal(b'\xf0\xa3\x91\x96', '\U00023456', 'encode', None, None)
65+
# Check that errors defaults to 'strict'
66+
self.checkraises(UnicodeError, '\ud800', 'encode' 8000 , None, None)
67+
5468

5569
if __name__ == "__main__":
5670
unittest.main()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ Arnaud Fontaine
512512
Michael Foord
513513
Amaury Forgeot d'Arc
514514
Doug Fort
515+
Daniel Fortunov
515516
Evens Fortuné
516517
Chris Foster
517518
John Fouhy
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``UserString.encode()`` to correctly return ``bytes`` rather than a ``UserString`` instance.

0 commit comments

Comments
 (0)
0