Skip to content

Commit 4fce98a

Browse files
authored
gh-141510: Change marshal version to 6 (#145551)
Fix SliceTestCase: test also that version 4 fails with ValueError.
1 parent c3fb0d9 commit 4fce98a

File tree

6 files changed

+29
-5
lines changed

6 files changed

+29
-5
lines changed

Doc/library/marshal.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ this module. The following types are supported:
5151
* Strings (:class:`str`) and :class:`bytes`.
5252
:term:`Bytes-like objects <bytes-like object>` like :class:`bytearray` are
5353
marshalled as :class:`!bytes`.
54-
* Containers: :class:`tuple`, :class:`list`, :class:`set`, :class:`frozenset`,
55-
and (since :data:`version` 5), :class:`slice`.
54+
* Containers: :class:`tuple`, :class:`list`, :class:`dict`, :class:`frozendict`
55+
(since :data:`version` 6), :class:`set`, :class:`frozenset`, and
56+
:class:`slice` (since :data:`version` 5).
5657
It should be understood that these are supported only if the values contained
5758
therein are themselves supported.
5859
Recursive containers are supported since :data:`version` 3.
@@ -71,6 +72,10 @@ this module. The following types are supported:
7172

7273
Added format version 5, which allows marshalling slices.
7374

75+
.. versionchanged:: next
76+
77+
Added format version 6, which allows marshalling :class:`frozendict`.
78+
7479

7580
The module defines these functions:
7681

@@ -173,6 +178,8 @@ In addition, the following constants are defined:
173178
4 Python 3.4 Efficient representation of short strings
174179
------- --------------- ----------------------------------------------------
175180
5 Python 3.14 Support for :class:`slice` objects
181+
------- --------------- ----------------------------------------------------
182+
6 Python 3.15 Support for :class:`frozendict` objects
176183
======= =============== ====================================================
177184

178185

Include/cpython/marshal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ PyAPI_FUNC(PyObject *) PyMarshal_ReadObjectFromString(const char *,
66
Py_ssize_t);
77
PyAPI_FUNC(PyObject *) PyMarshal_WriteObjectToString(PyObject *, int);
88

9-
#define Py_MARSHAL_VERSION 5
9+
#define Py_MARSHAL_VERSION 6
1010

1111
PyAPI_FUNC(long) PyMarshal_ReadLongFromFile(FILE *);
1212
PyAPI_FUNC(int) PyMarshal_ReadShortFromFile(FILE *);

Lib/test/test_marshal.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,15 @@ def testDict(self):
570570
self.helper(dictobj)
571571
self.helper3(dictobj)
572572

573+
def testFrozenDict(self):
574+
for obj in self.keys:
575+
dictobj = frozendict({"hello": obj, "goodbye": obj, obj: "hello"})
576+
self.helper(dictobj)
577+
578+
for version in range(6):
579+
with self.assertRaises(ValueError):
580+
marshal.dumps(dictobj, version)
581+
573582
def testModule(self):
574583
with open(__file__, "rb") as f:
575584
code = f.read()
@@ -635,7 +644,7 @@ def test_slice(self):
635644
with self.subTest(obj=str(obj)):
636645
self.helper(obj)
637646

638-
for version in range(4):
647+
for version in range(5):
639648
with self.assertRaises(ValueError):
640649
marshal.dumps(obj, version)
641650

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`marshal` now supports :class:`frozendict` objects. The marshal format
2+
version was increased to 6. Patch by Victor Stinner.

Programs/_freeze_module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ compile_and_marshal(const char *name, const char *text)
134134
return NULL;
135135
}
136136

137-
assert(Py_MARSHAL_VERSION >= 5);
137+
assert(Py_MARSHAL_VERSION >= 6);
138138
PyObject *marshalled = PyMarshal_WriteObjectToString(code, Py_MARSHAL_VERSION);
139139
Py_CLEAR(code);
140140
if (marshalled == NULL) {

Python/marshal.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,12 @@ w_complex_object(PyObject *v, char flag, WFILE *p)
580580
Py_ssize_t pos;
581581
PyObject *key, *value;
582582
if (PyFrozenDict_CheckExact(v)) {
583+
if (p->version < 6) {
584+
w_byte(TYPE_UNKNOWN, p);
585+
p->error = WFERR_UNMARSHALLABLE;
586+
return;
587+
}
588+
583589
W_TYPE(TYPE_FROZENDICT, p);
584590
}
585591
else {

0 commit comments

Comments
 (0)