gh-125063: marshal: Add version 5, improve documentation (GH-126829)

* Document that slices can be marshalled
* Deduplicate and organize the list of supported types
  in docs
* Organize the type code list in marshal.c, to make
  it more obvious that this is a versioned format
* Back-fill some historical info

Co-authored-by: Michael Droettboom <mdboom@gmail.com>
This commit is contained in:
Petr Viktorin 2024-11-15 13:48:57 +01:00 committed by GitHub
parent e17486982c
commit d00f7b1b9d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 110 additions and 41 deletions

View file

@ -28,6 +28,13 @@ class HelperMixin:
finally:
os_helper.unlink(os_helper.TESTFN)
def omit_last_byte(data):
"""return data[:-1]"""
# This file's code is used in CompatibilityTestCase,
# but slices need marshal version 5.
# Avoid the slice literal.
return data[slice(0, -1)]
class IntTestCase(unittest.TestCase, HelperMixin):
def test_ints(self):
# Test a range of Python ints larger than the machine word size.
@ -241,7 +248,8 @@ class BugsTestCase(unittest.TestCase):
def test_patch_873224(self):
self.assertRaises(Exception, marshal.loads, b'0')
self.assertRaises(Exception, marshal.loads, b'f')
self.assertRaises(Exception, marshal.loads, marshal.dumps(2**65)[:-1])
self.assertRaises(Exception, marshal.loads,
omit_last_byte(marshal.dumps(2**65)))
def test_version_argument(self):
# Python 2.4.0 crashes for any call to marshal.dumps(x, y)
@ -594,6 +602,19 @@ class InterningTestCase(unittest.TestCase, HelperMixin):
s2 = sys.intern(s)
self.assertNotEqual(id(s2), id(s))
class SliceTestCase(unittest.TestCase, HelperMixin):
def test_slice(self):
for obj in (
slice(None), slice(1), slice(1, 2), slice(1, 2, 3),
slice({'set'}, ('tuple', {'with': 'dict'}, ), self.helper.__code__)
):
with self.subTest(obj=str(obj)):
self.helper(obj)
for version in range(4):
with self.assertRaises(ValueError):
marshal.dumps(obj, version)
@support.cpython_only
@unittest.skipUnless(_testcapi, 'requires _testcapi')
class CAPI_TestCase(unittest.TestCase, HelperMixin):
@ -654,7 +675,7 @@ class CAPI_TestCase(unittest.TestCase, HelperMixin):
self.assertEqual(r, obj)
with open(os_helper.TESTFN, 'wb') as f:
f.write(data[:1])
f.write(omit_last_byte(data))
with self.assertRaises(EOFError):
_testcapi.pymarshal_read_last_object_from_file(os_helper.TESTFN)
os_helper.unlink(os_helper.TESTFN)
@ -671,7 +692,7 @@ class CAPI_TestCase(unittest.TestCase, HelperMixin):
self.assertEqual(p, len(data))
with open(os_helper.TESTFN, 'wb') as f:
f.write(data[:1])
f.write(omit_last_byte(data))
with self.assertRaises(EOFError):
_testcapi.pymarshal_read_object_from_file(os_helper.TESTFN)
os_helper.unlink(os_helper.TESTFN)