]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix bytes.__bytes__ to not truncate at a zero byte (GH-27902)
authorMark Dickinson <mdickinson@enthought.com>
Mon, 23 Aug 2021 14:24:12 +0000 (15:24 +0100)
committerGitHub <noreply@github.com>
Mon, 23 Aug 2021 14:24:12 +0000 (15:24 +0100)
Lib/test/test_bytes.py
Objects/bytesobject.c

index c45c69f3f22c1fa7d220ceb118275e46e9580514..c2fe2cc25420f82a0e93dd7cdd39f0402b198f39 100644 (file)
@@ -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)
 
index 13f94b4c82fbc5ce32c0c736716d41f99ada20f7..27f766bef6ed8450ad7ed8bd46a49a9d8ec43e98 100644 (file)
@@ -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));
     }
 }