FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-141510: Change marshal version to 6 by vstinner · Pull Request #145551 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .h  (1) .py  (1) .rst  (2) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
11 changes: 9 additions & 2 deletions Doc/library/marshal.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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 Include/cpython/marshal.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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 Lib/test/test_marshal.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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 Programs/_freeze_module.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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 Python/marshal.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL