https://github.com/python/cpython/commit/c24b1aab446fb4a74aedc5e26a27ea39b3ed4fee commit: c24b1aab446fb4a74aedc5e26a27ea39b3ed4fee branch: 3.15 author: Miss Islington (bot) <[email protected]> committer: hugovk <[email protected]> date: 2026-09-15T16:44:47+03:00 summary:
[3.15] gh-128509: Use bytes singletons in marshal (GH-157398) (#157433) Co-authored-by: Victor Stinner <[email protected]> files: A Misc/NEWS.d/next/Library/2026-09-13-07-14-35.gh-issue-128509.9F2GCE.rst M Lib/test/test_marshal.py M Python/marshal.c diff --git a/Lib/test/test_marshal.py b/Lib/test/test_marshal.py index 9c4d91c456dc5d9..6ec1e088bab5468 100644 --- a/Lib/test/test_marshal.py +++ b/Lib/test/test_marshal.py @@ -116,6 +116,14 @@ def test_bytes(self): for s in [b"", b"Andr\xe8 Previn", b"abc", b" "*10000]: self.helper(s) + @support.cpython_only + def test_bytes_singleton(self): + for version in range(marshal.version + 1): + for sample in [b"", b"x"]: + new = marshal.loads(marshal.dumps(sample, version)) + self.assertIs(new, sample) + + class ExceptionTestCase(unittest.TestCase): def test_exceptions(self): new = marshal.loads(marshal.dumps(StopIteration)) diff --git a/Misc/NEWS.d/next/Library/2026-09-13-07-14-35.gh-issue-128509.9F2GCE.rst b/Misc/NEWS.d/next/Library/2026-09-13-07-14-35.gh-issue-128509.9F2GCE.rst new file mode 100644 index 000000000000000..7699242c8c647ea --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-09-13-07-14-35.gh-issue-128509.9F2GCE.rst @@ -0,0 +1,2 @@ +:func:`marshal.load` and :func:`marshal.loads` can now get 1-byte string +singletons. Patch by Victor Stinner. diff --git a/Python/marshal.c b/Python/marshal.c index 25353f6e6896249..1269ebd18b210fd 100644 --- a/Python/marshal.c +++ b/Python/marshal.c @@ -1308,16 +1308,12 @@ r_object(RFILE *p) } break; } - v = PyBytes_FromStringAndSize((char *)NULL, n); - if (v == NULL) - break; ptr = r_string(n, p); if (ptr == NULL) { - Py_DECREF(v); break; } - memcpy(PyBytes_AS_STRING(v), ptr, n); - retval = v; + // Get a singleton for 1-byte string + retval = PyBytes_FromStringAndSize(ptr, n); // can be NULL R_REF(retval); break; } _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
