There was an error while loading. Please reload this page.
1 parent a60343e commit 658612aCopy full SHA for 658612a
3 files changed
Lib/test/test_marshal.py
@@ -116,6 +116,14 @@ def test_bytes(self):
116
for s in [b"", b"Andr\xe8 Previn", b"abc", b" "*10000]:
117
self.helper(s)
118
119
+ @support.cpython_only
120
+ def test_bytes_singleton(self):
121
+ for version in range(marshal.version + 1):
122
+ for sample in [b"", b"x"]:
123
+ new = marshal.loads(marshal.dumps(sample, version))
124
+ self.assertIs(new, sample)
125
+
126
127
class ExceptionTestCase(unittest.TestCase):
128
def test_exceptions(self):
129
new = marshal.loads(marshal.dumps(StopIteration))
Misc/NEWS.d/next/Library/2026-09-13-07-14-35.gh-issue-128509.9F2GCE.rst
@@ -0,0 +1,2 @@
1
+:func:`marshal.load` and :func:`marshal.loads` can now get 1-byte string
2
+singletons. Patch by Victor Stinner.
Python/marshal.c
@@ -1388,16 +1388,12 @@ r_object(RFILE *p)
1388
}
1389
break;
1390
1391
- v = PyBytes_FromStringAndSize((char *)NULL, n);
1392
- if (v == NULL)
1393
- break;
1394
ptr = r_string(n, p);
1395
if (ptr == NULL) {
1396
- Py_DECREF(v);
1397
1398
1399
- memcpy(PyBytes_AS_STRING(v), ptr, n);
1400
- retval = v;
+ // Get a singleton for 1-byte string
+ retval = PyBytes_FromStringAndSize(ptr, n); // can be NULL
1401
R_REF(retval);
1402
1403
0 commit comments