]> granicus.if.org Git - python/commitdiff
bpo-36582: Make collections.UserString.encode() return bytes, not str (GH-13138)
authorDaniel Fortunov <asqui@users.noreply.github.com>
Wed, 28 Aug 2019 04:38:09 +0000 (06:38 +0200)
committerRaymond Hettinger <rhettinger@users.noreply.github.com>
Wed, 28 Aug 2019 04:38:09 +0000 (21:38 -0700)
Lib/collections/__init__.py
Lib/test/test_userstring.py
Misc/ACKS
Misc/NEWS.d/next/Library/2019-05-07-17-42-36.bpo-36582.L_dxR6.rst [new file with mode: 0644]

index 6a3e599a91e6c137f38a8fe36e35d142a565daee..859b8460e48d595d76b2d31a81806cf005260d6f 100644 (file)
@@ -1184,12 +1184,10 @@ class UserString(_collections_abc.Sequence):
         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):
index 19b0acfc760fa4e220f79e7f6f3e4f5a2c666ac0..4d1d8b6b6fe2d951d2899e9931ab95b42051d4ca 100644 (file)
@@ -51,6 +51,20 @@ class UserStringTest(
         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()
index 4f5ebd67f863e8176d09e9b7e8823c190d199167..d5064f644e24e9eab25976c6fa9be4621a3005f4 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -512,6 +512,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 (file)
index 0000000..34f16fc
--- /dev/null
@@ -0,0 +1 @@
+Fix ``UserString.encode()`` to correctly return ``bytes`` rather than a ``UserString`` instance.
\ No newline at end of file