17db96d56Sopenharmony_ci:mod:`marshal` --- Internal Python object serialization 27db96d56Sopenharmony_ci======================================================= 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. module:: marshal 57db96d56Sopenharmony_ci :synopsis: Convert Python objects to streams of bytes and back (with different 67db96d56Sopenharmony_ci constraints). 77db96d56Sopenharmony_ci 87db96d56Sopenharmony_ci-------------- 97db96d56Sopenharmony_ci 107db96d56Sopenharmony_ciThis module contains functions that can read and write Python values in a binary 117db96d56Sopenharmony_ciformat. The format is specific to Python, but independent of machine 127db96d56Sopenharmony_ciarchitecture issues (e.g., you can write a Python value to a file on a PC, 137db96d56Sopenharmony_citransport the file to a Sun, and read it back there). Details of the format are 147db96d56Sopenharmony_ciundocumented on purpose; it may change between Python versions (although it 157db96d56Sopenharmony_cirarely does). [#]_ 167db96d56Sopenharmony_ci 177db96d56Sopenharmony_ci.. index:: 187db96d56Sopenharmony_ci pair: module; pickle 197db96d56Sopenharmony_ci pair: module; shelve 207db96d56Sopenharmony_ci 217db96d56Sopenharmony_ciThis is not a general "persistence" module. For general persistence and 227db96d56Sopenharmony_citransfer of Python objects through RPC calls, see the modules :mod:`pickle` and 237db96d56Sopenharmony_ci:mod:`shelve`. The :mod:`marshal` module exists mainly to support reading and 247db96d56Sopenharmony_ciwriting the "pseudo-compiled" code for Python modules of :file:`.pyc` files. 257db96d56Sopenharmony_ciTherefore, the Python maintainers reserve the right to modify the marshal format 267db96d56Sopenharmony_ciin backward incompatible ways should the need arise. If you're serializing and 277db96d56Sopenharmony_cide-serializing Python objects, use the :mod:`pickle` module instead -- the 287db96d56Sopenharmony_ciperformance is comparable, version independence is guaranteed, and pickle 297db96d56Sopenharmony_cisupports a substantially wider range of objects than marshal. 307db96d56Sopenharmony_ci 317db96d56Sopenharmony_ci.. warning:: 327db96d56Sopenharmony_ci 337db96d56Sopenharmony_ci The :mod:`marshal` module is not intended to be secure against erroneous or 347db96d56Sopenharmony_ci maliciously constructed data. Never unmarshal data received from an 357db96d56Sopenharmony_ci untrusted or unauthenticated source. 367db96d56Sopenharmony_ci 377db96d56Sopenharmony_ci.. index:: object; code, code object 387db96d56Sopenharmony_ci 397db96d56Sopenharmony_ciNot all Python object types are supported; in general, only objects whose value 407db96d56Sopenharmony_ciis independent from a particular invocation of Python can be written and read by 417db96d56Sopenharmony_cithis module. The following types are supported: booleans, integers, floating 427db96d56Sopenharmony_cipoint numbers, complex numbers, strings, bytes, bytearrays, tuples, lists, sets, 437db96d56Sopenharmony_cifrozensets, dictionaries, and code objects, where it should be understood that 447db96d56Sopenharmony_cituples, lists, sets, frozensets and dictionaries are only supported as long as 457db96d56Sopenharmony_cithe values contained therein are themselves supported. The 467db96d56Sopenharmony_cisingletons :const:`None`, :const:`Ellipsis` and :exc:`StopIteration` can also be 477db96d56Sopenharmony_cimarshalled and unmarshalled. 487db96d56Sopenharmony_ciFor format *version* lower than 3, recursive lists, sets and dictionaries cannot 497db96d56Sopenharmony_cibe written (see below). 507db96d56Sopenharmony_ci 517db96d56Sopenharmony_ciThere are functions that read/write files as well as functions operating on 527db96d56Sopenharmony_cibytes-like objects. 537db96d56Sopenharmony_ci 547db96d56Sopenharmony_ciThe module defines these functions: 557db96d56Sopenharmony_ci 567db96d56Sopenharmony_ci 577db96d56Sopenharmony_ci.. function:: dump(value, file[, version]) 587db96d56Sopenharmony_ci 597db96d56Sopenharmony_ci Write the value on the open file. The value must be a supported type. The 607db96d56Sopenharmony_ci file must be a writeable :term:`binary file`. 617db96d56Sopenharmony_ci 627db96d56Sopenharmony_ci If the value has (or contains an object that has) an unsupported type, a 637db96d56Sopenharmony_ci :exc:`ValueError` exception is raised --- but garbage data will also be written 647db96d56Sopenharmony_ci to the file. The object will not be properly read back by :func:`load`. 657db96d56Sopenharmony_ci 667db96d56Sopenharmony_ci The *version* argument indicates the data format that ``dump`` should use 677db96d56Sopenharmony_ci (see below). 687db96d56Sopenharmony_ci 697db96d56Sopenharmony_ci .. audit-event:: marshal.dumps value,version marshal.dump 707db96d56Sopenharmony_ci 717db96d56Sopenharmony_ci 727db96d56Sopenharmony_ci.. function:: load(file) 737db96d56Sopenharmony_ci 747db96d56Sopenharmony_ci Read one value from the open file and return it. If no valid value is read 757db96d56Sopenharmony_ci (e.g. because the data has a different Python version's incompatible marshal 767db96d56Sopenharmony_ci format), raise :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. The 777db96d56Sopenharmony_ci file must be a readable :term:`binary file`. 787db96d56Sopenharmony_ci 797db96d56Sopenharmony_ci .. audit-event:: marshal.load "" marshal.load 807db96d56Sopenharmony_ci 817db96d56Sopenharmony_ci .. note:: 827db96d56Sopenharmony_ci 837db96d56Sopenharmony_ci If an object containing an unsupported type was marshalled with :func:`dump`, 847db96d56Sopenharmony_ci :func:`load` will substitute ``None`` for the unmarshallable type. 857db96d56Sopenharmony_ci 867db96d56Sopenharmony_ci .. versionchanged:: 3.10 877db96d56Sopenharmony_ci 887db96d56Sopenharmony_ci This call used to raise a ``code.__new__`` audit event for each code object. Now 897db96d56Sopenharmony_ci it raises a single ``marshal.load`` event for the entire load operation. 907db96d56Sopenharmony_ci 917db96d56Sopenharmony_ci 927db96d56Sopenharmony_ci.. function:: dumps(value[, version]) 937db96d56Sopenharmony_ci 947db96d56Sopenharmony_ci Return the bytes object that would be written to a file by ``dump(value, file)``. The 957db96d56Sopenharmony_ci value must be a supported type. Raise a :exc:`ValueError` exception if value 967db96d56Sopenharmony_ci has (or contains an object that has) an unsupported type. 977db96d56Sopenharmony_ci 987db96d56Sopenharmony_ci The *version* argument indicates the data format that ``dumps`` should use 997db96d56Sopenharmony_ci (see below). 1007db96d56Sopenharmony_ci 1017db96d56Sopenharmony_ci .. audit-event:: marshal.dumps value,version marshal.dump 1027db96d56Sopenharmony_ci 1037db96d56Sopenharmony_ci 1047db96d56Sopenharmony_ci.. function:: loads(bytes) 1057db96d56Sopenharmony_ci 1067db96d56Sopenharmony_ci Convert the :term:`bytes-like object` to a value. If no valid value is found, raise 1077db96d56Sopenharmony_ci :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. Extra bytes in the 1087db96d56Sopenharmony_ci input are ignored. 1097db96d56Sopenharmony_ci 1107db96d56Sopenharmony_ci .. audit-event:: marshal.loads bytes marshal.load 1117db96d56Sopenharmony_ci 1127db96d56Sopenharmony_ci .. versionchanged:: 3.10 1137db96d56Sopenharmony_ci 1147db96d56Sopenharmony_ci This call used to raise a ``code.__new__`` audit event for each code object. Now 1157db96d56Sopenharmony_ci it raises a single ``marshal.loads`` event for the entire load operation. 1167db96d56Sopenharmony_ci 1177db96d56Sopenharmony_ci 1187db96d56Sopenharmony_ciIn addition, the following constants are defined: 1197db96d56Sopenharmony_ci 1207db96d56Sopenharmony_ci.. data:: version 1217db96d56Sopenharmony_ci 1227db96d56Sopenharmony_ci Indicates the format that the module uses. Version 0 is the historical 1237db96d56Sopenharmony_ci format, version 1 shares interned strings and version 2 uses a binary format 1247db96d56Sopenharmony_ci for floating point numbers. 1257db96d56Sopenharmony_ci Version 3 adds support for object instancing and recursion. 1267db96d56Sopenharmony_ci The current version is 4. 1277db96d56Sopenharmony_ci 1287db96d56Sopenharmony_ci 1297db96d56Sopenharmony_ci.. rubric:: Footnotes 1307db96d56Sopenharmony_ci 1317db96d56Sopenharmony_ci.. [#] The name of this module stems from a bit of terminology used by the designers of 1327db96d56Sopenharmony_ci Modula-3 (amongst others), who use the term "marshalling" for shipping of data 1337db96d56Sopenharmony_ci around in a self-contained form. Strictly speaking, "to marshal" means to 1347db96d56Sopenharmony_ci convert some data from internal to external form (in an RPC buffer for instance) 1357db96d56Sopenharmony_ci and "unmarshalling" for the reverse process. 1367db96d56Sopenharmony_ci 137