Skip to content

[3.8] bpo-36582: Make collections.UserString.encode() return bytes, not str (GH-13138) #15557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions Lib/collections/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_userstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ Arnaud Fontaine
Michael Foord
Amaury Forgeot d'Arc
Doug Fort
Daniel Fortunov
Evens Fortuné
Chris Foster
John Fouhy
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``UserString.encode()`` to correctly return ``bytes`` rather than a ``UserString`` instance.
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy