8000 bpo-24234: fix bytes.__bytes__ to not truncate at a zero byte by mdickinson · Pull Request #27902 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-24234: fix bytes.__bytes__ to not truncate at a zero byte #27902

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 23, 2021
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
Fix bytes.__bytes__ to not truncate at a zero byte
  • Loading branch information
mdickinson committed Aug 23, 2021
commit 72ea5930dd79b88c8b494ded517d138a89223482
4 changes: 2 additions & 2 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
0