[Python-checkins] Fix bytes.__bytes__ to not truncate at a zero byte (GH-27902)

mdickinson webhook-mailer at python.org
Mon Aug 23 10:24:17 EDT 2021


https://github.com/python/cpython/commit/ae5259171b8ef62165e061b9dea7ad645a5131a2
commit: ae5259171b8ef62165e061b9dea7ad645a5131a2
branch: main
author: Mark Dickinson <mdickinson at enthought.com>
committer: mdickinson <dickinsm at gmail.com>
date: 2021-08-23T15:24:12+01:00
summary:

Fix bytes.__bytes__ to not truncate at a zero byte (GH-27902)

files:
M Lib/test/test_bytes.py
M Objects/bytesobject.c

diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index c45c69f3f22c1..c2fe2cc25420f 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -982,14 +982,14 @@ class BytesTest(BaseBytesTest, unittest.TestCase):
     type2test = bytes
 
     def test__bytes__(self):
-        foo = b'foo'
+        foo = b'foo\x00bar'
         self.assertEqual(foo.__bytes__(), foo)
         self.assertEqual(type(foo.__bytes__()), self.type2test)
 
         class bytes_subclass(bytes):
             pass
 
-        bar = bytes_subclass(b'bar')
+        bar = bytes_subclass(b'bar\x00foo')
         self.assertEqual(bar.__bytes__(), bar)
         self.assertEqual(type(bar.__bytes__()), self.type2test)
 
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 13f94b4c82fbc..27f766bef6ed8 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -1701,7 +1701,7 @@ bytes___bytes___impl(PyBytesObject *self)
         return (PyObject *)self;
     }
     else {
-        return PyBytes_FromString(self->ob_sval);
+        return PyBytes_FromStringAndSize(self->ob_sval, Py_SIZE(self));
     }
 }
 



More information about the Python-checkins mailing list