@@ -23,7 +23,11 @@ transfer of Python objects through RPC calls, see the modules :mod:`pickle` and
23
23
:mod: `shelve `. The :mod: `marshal ` module exists mainly to support reading and
24
24
writing the "pseudo-compiled" code for Python modules of :file: `.pyc ` files.
25
25
Therefore, the Python maintainers reserve the right to modify the marshal format
26
- in backward incompatible ways should the need arise. If you're serializing and
26
+ in backward incompatible ways should the need arise.
27
+ The format of code objects is not compatible between Python versions,
28
+ even if the version of the format is the same.
29
+ De-serializing a code object in the incorrect Python version has undefined behavior.
30
+ If you're serializing and
27
31
de-serializing Python objects, use the :mod: `pickle ` module instead -- the
28
32
performance is comparable, version independence is guaranteed, and pickle
29
33
supports a substantially wider range of objects than marshal.
@@ -40,7 +44,8 @@ Not all Python object types are supported; in general, only objects whose value
40
44
is independent from a particular invocation of Python can be written and read by
41
45
this module. The following types are supported: booleans, integers, floating
42
46
point numbers, complex numbers, strings, bytes, bytearrays, tuples, lists, sets,
43
- frozensets, dictionaries, and code objects, where it should be understood that
47
+ frozensets, dictionaries, and code objects (if *allow_code * is true),
48
+ where it should be understood that
44
49
tuples, lists, sets, frozensets and dictionaries are only supported as long as
45
50
the values contained therein are themselves supported. The
46
51
singletons :const: `None `, :const: `Ellipsis ` and :exc: `StopIteration ` can also be
@@ -54,27 +59,32 @@ bytes-like objects.
54
59
The module defines these functions:
55
60
56
61
57
- .. function :: dump(value, file[ , version] )
62
+ .. function :: dump(value, file, version=version, /, *, allow_code=True )
58
63
59
64
Write the value on the open file. The value must be a supported type. The
60
65
file must be a writeable :term: `binary file `.
61
66
62
67
If the value has (or contains an object that has) an unsupported type, a
63
68
:exc: `ValueError ` exception is raised --- but garbage data will also be written
64
69
to the file. The object will not be properly read back by :func: `load `.
70
+ :ref: `Code objects <code-objects >` are only supported if *allow_code * is true.
65
71
66
72
The *version * argument indicates the data format that ``dump `` should use
67
73
(see below).
68
74
69
75
.. audit-event :: marshal.dumps value,version marshal.dump
70
76
77
+ .. versionchanged :: 3.13
78
+ Added the *allow_code * parameter.
71
79
72
- .. function :: load(file)
80
+
81
+ .. function :: load(file, /, *, allow_code=True)
73
82
74
83
Read one value from the open file and return it. If no valid value is read
75
84
(e.g. because the data has a different Python version's incompatible marshal
76
- format), raise :exc: `EOFError `, :exc: `ValueError ` or :exc: `TypeError `. The
77
- file must be a readable :term: `binary file `.
85
+ format), raise :exc: `EOFError `, :exc: `ValueError ` or :exc: `TypeError `.
86
+ :ref: `Code objects <code-objects >` are only supported if *allow_code * is true.
87
+ The file must be a readable :term: `binary file `.
78
88
79
89
.. audit-event :: marshal.load "" marshal.load
80
90
@@ -88,24 +98,32 @@ The module defines these functions:
88
98
This call used to raise a ``code.__new__ `` audit event for each code object. Now
89
99
it raises a single ``marshal.load `` event for the entire load operation.
90
100
101
+ .. versionchanged :: 3.13
102
+ Added the *allow_code * parameter.
103
+
91
104
92
- .. function :: dumps(value[ , version] )
105
+ .. function :: dumps(value, version=version, /, *, allow_code=True )
93
106
94
107
Return the bytes object that would be written to a file by ``dump(value, file) ``. The
95
108
value must be a supported type. Raise a :exc: `ValueError ` exception if value
96
109
has (or contains an object that has) an unsupported type.
110
+ :ref: `Code objects <code-objects >` are only supported if *allow_code * is true.
97
111
98
112
The *version * argument indicates the data format that ``dumps `` should use
99
113
(see below).
100
114
101
115
.. audit-event :: marshal.dumps value,version marshal.dump
102
116
117
+ .. versionchanged :: 3.13
118
+ Added the *allow_code * parameter.
103
119
104
- .. function :: loads(bytes)
120
+
121
+ .. function :: loads(bytes, /, *, allow_code=True)
105
122
106
123
Convert the :term: `bytes-like object ` to a value. If no valid value is found, raise
107
- :exc: `EOFError `, :exc: `ValueError ` or :exc: `TypeError `. Extra bytes in the
108
- input are ignored.
124
+ :exc: `EOFError `, :exc: `ValueError ` or :exc: `TypeError `.
125
+ :ref: `Code objects <code-objects >` are only supported if *allow_code * is true.
126
+ Extra bytes in the input are ignored.
109
127
110
128
.. audit-event :: marshal.loads bytes marshal.load
111
129
@@ -114,6 +132,9 @@ The module defines these functions:
114
132
This call used to raise a ``code.__new__ `` audit event for each code object. Now
115
133
it raises a single ``marshal.loads `` event for the entire load operation.
116
134
135
+ .. versionchanged :: 3.13
136
+ Added the *allow_code * parameter.
137
+
117
138
118
139
In addition, the following constants are defined:
119
140
0 commit comments