Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
11 changes: 9 additions & 2 deletions 11 Doc/library/marshal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ this module. The following types are supported:
* Strings (:class:`str`) and :class:`bytes`.
:term:`Bytes-like objects <bytes-like object>` like :class:`bytearray` are
marshalled as :class:`!bytes`.
* Containers: :class:`tuple`, :class:`list`, :class:`set`, :class:`frozenset`,
and (since :data:`version` 5), :class:`slice`.
* Containers: :class:`tuple`, :class:`list`, :class:`dict`, :class:`frozendict`
(since :data:`version` 6), :class:`set`, :class:`frozenset`, and
:class:`slice` (since :data:`version` 5).
It should be understood that these are supported only if the values contained
therein are themselves supported.
Recursive containers are supported since :data:`version` 3.
Expand All @@ -71,6 +72,10 @@ this module. The following types are supported:

Added format version 5, which allows marshalling slices.

.. versionchanged:: next

Added format version 6, which allows marshalling :class:`frozendict`.


The module defines these functions:

Expand Down Expand Up @@ -173,6 +178,8 @@ In addition, the following constants are defined:
4 Python 3.4 Efficient representation of short strings
------- --------------- ----------------------------------------------------
5 Python 3.14 Support for :class:`slice` objects
------- --------------- ----------------------------------------------------
6 Python 3.15 Support for :class:`frozendict` objects
======= =============== ====================================================


Expand Down
2 changes: 1 addition & 1 deletion 2 Include/cpython/marshal.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ PyAPI_FUNC(PyObject *) PyMarshal_ReadObjectFromString(const char *,
Py_ssize_t);
PyAPI_FUNC(PyObject *) PyMarshal_WriteObjectToString(PyObject *, int);

#define Py_MARSHAL_VERSION 5
#define Py_MARSHAL_VERSION 6

PyAPI_FUNC(long) PyMarshal_ReadLongFromFile(FILE *);
PyAPI_FUNC(int) PyMarshal_ReadShortFromFile(FILE *);
Expand Down
11 changes: 10 additions & 1 deletion 11 Lib/test/test_marshal.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,15 @@ def testDict(self):
self.helper(dictobj)
self.helper3(dictobj)

def testFrozenDict(self):
for obj in self.keys:
dictobj = frozendict({"hello": obj, "goodbye": obj, obj: "hello"})
self.helper(dictobj)

for version in range(6):
with self.assertRaises(ValueError):
marshal.dumps(dictobj, version)

def testModule(self):
with open(__file__, "rb") as f:
code = f.read()
Expand Down Expand Up @@ -635,7 +644,7 @@ def test_slice(self):
with self.subTest(obj=str(obj)):
self.helper(obj)

for version in range(4):
for version in range(5):
with self.assertRaises(ValueError):
marshal.dumps(obj, version)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`marshal` now supports :class:`frozendict` objects. The marshal format
version was increased to 6. Patch by Victor Stinner.
2 changes: 1 addition & 1 deletion 2 Programs/_freeze_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ compile_and_marshal(const char *name, const char *text)
return NULL;
}

assert(Py_MARSHAL_VERSION >= 5);
assert(Py_MARSHAL_VERSION >= 6);
PyObject *marshalled = PyMarshal_WriteObjectToString(code, Py_MARSHAL_VERSION);
Py_CLEAR(code);
if (marshalled == NULL) {
Expand Down
6 changes: 6 additions & 0 deletions 6 Python/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,12 @@ w_complex_object(PyObject *v, char flag, WFILE *p)
Py_ssize_t pos;
PyObject *key, *value;
if (PyFrozenDict_CheckExact(v)) {
if (p->version < 6) {
w_byte(TYPE_UNKNOWN, p);
p->error = WFERR_UNMARSHALLABLE;
return;
}

W_TYPE(TYPE_FROZENDICT, p);
}
else {
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.