blob: 17ec621610b5e3aded74939f5fc6ad499830e178 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _marshalling-utils:
4
5Data marshalling support
6========================
7
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +00008These routines allow C code to work with serialized objects using the same
9data format as the :mod:`marshal` module. There are functions to write data
10into the serialization format, and additional functions that can be used to
11read the data back. Files used to store marshalled data must be opened in
12binary mode.
Georg Brandl54a3faa2008-01-20 09:30:57 +000013
14Numeric values are stored with the least significant byte first.
15
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000016The module supports two versions of the data format: version 0 is the
17historical version, version 1 shares interned strings in the file, and upon
18unmarshalling. Version 2 uses a binary format for floating point numbers.
Georg Brandle6bcc912008-05-12 18:05:20 +000019*Py_MARSHAL_VERSION* indicates the current file format (currently 2).
Georg Brandl54a3faa2008-01-20 09:30:57 +000020
21
Georg Brandl60203b42010-10-06 10:11:56 +000022.. c:function:: void PyMarshal_WriteLongToFile(long value, FILE *file, int version)
Georg Brandl54a3faa2008-01-20 09:30:57 +000023
Georg Brandl60203b42010-10-06 10:11:56 +000024 Marshal a :c:type:`long` integer, *value*, to *file*. This will only write
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000025 the least-significant 32 bits of *value*; regardless of the size of the
Georg Brandl60203b42010-10-06 10:11:56 +000026 native :c:type:`long` type. *version* indicates the file format.
Georg Brandl54a3faa2008-01-20 09:30:57 +000027
28
Georg Brandl60203b42010-10-06 10:11:56 +000029.. c:function:: void PyMarshal_WriteObjectToFile(PyObject *value, FILE *file, int version)
Georg Brandl54a3faa2008-01-20 09:30:57 +000030
31 Marshal a Python object, *value*, to *file*.
32 *version* indicates the file format.
33
34
Georg Brandl60203b42010-10-06 10:11:56 +000035.. c:function:: PyObject* PyMarshal_WriteObjectToString(PyObject *value, int version)
Georg Brandl54a3faa2008-01-20 09:30:57 +000036
Serhiy Storchakac611a5b2017-03-12 08:53:22 +020037 Return a bytes object containing the marshalled representation of *value*.
Georg Brandl54a3faa2008-01-20 09:30:57 +000038 *version* indicates the file format.
39
40
41The following functions allow marshalled values to be read back in.
42
Georg Brandl54a3faa2008-01-20 09:30:57 +000043
Georg Brandl60203b42010-10-06 10:11:56 +000044.. c:function:: long PyMarshal_ReadLongFromFile(FILE *file)
Georg Brandl54a3faa2008-01-20 09:30:57 +000045
Georg Brandl60203b42010-10-06 10:11:56 +000046 Return a C :c:type:`long` from the data stream in a :c:type:`FILE\*` opened
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000047 for reading. Only a 32-bit value can be read in using this function,
Georg Brandl60203b42010-10-06 10:11:56 +000048 regardless of the native size of :c:type:`long`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000049
Berker Peksagdefcffd2018-07-27 07:35:11 +030050 On error, sets the appropriate exception (:exc:`EOFError`) and returns
51 ``-1``.
Victor Stinner6a318d42015-03-18 13:58:49 +010052
Georg Brandl54a3faa2008-01-20 09:30:57 +000053
Georg Brandl60203b42010-10-06 10:11:56 +000054.. c:function:: int PyMarshal_ReadShortFromFile(FILE *file)
Georg Brandl54a3faa2008-01-20 09:30:57 +000055
Georg Brandl60203b42010-10-06 10:11:56 +000056 Return a C :c:type:`short` from the data stream in a :c:type:`FILE\*` opened
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000057 for reading. Only a 16-bit value can be read in using this function,
Georg Brandl60203b42010-10-06 10:11:56 +000058 regardless of the native size of :c:type:`short`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000059
Berker Peksagdefcffd2018-07-27 07:35:11 +030060 On error, sets the appropriate exception (:exc:`EOFError`) and returns
61 ``-1``.
Victor Stinner6a318d42015-03-18 13:58:49 +010062
Georg Brandl54a3faa2008-01-20 09:30:57 +000063
Georg Brandl60203b42010-10-06 10:11:56 +000064.. c:function:: PyObject* PyMarshal_ReadObjectFromFile(FILE *file)
Georg Brandl54a3faa2008-01-20 09:30:57 +000065
Georg Brandl60203b42010-10-06 10:11:56 +000066 Return a Python object from the data stream in a :c:type:`FILE\*` opened for
Victor Stinner6a318d42015-03-18 13:58:49 +010067 reading.
68
Berker Peksagdefcffd2018-07-27 07:35:11 +030069 On error, sets the appropriate exception (:exc:`EOFError`, :exc:`ValueError`
70 or :exc:`TypeError`) and returns *NULL*.
Georg Brandl54a3faa2008-01-20 09:30:57 +000071
72
Georg Brandl60203b42010-10-06 10:11:56 +000073.. c:function:: PyObject* PyMarshal_ReadLastObjectFromFile(FILE *file)
Georg Brandl54a3faa2008-01-20 09:30:57 +000074
Georg Brandl60203b42010-10-06 10:11:56 +000075 Return a Python object from the data stream in a :c:type:`FILE\*` opened for
76 reading. Unlike :c:func:`PyMarshal_ReadObjectFromFile`, this function
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000077 assumes that no further objects will be read from the file, allowing it to
78 aggressively load file data into memory so that the de-serialization can
79 operate from data in memory rather than reading a byte at a time from the
80 file. Only use these variant if you are certain that you won't be reading
Victor Stinner6a318d42015-03-18 13:58:49 +010081 anything else from the file.
82
Berker Peksagdefcffd2018-07-27 07:35:11 +030083 On error, sets the appropriate exception (:exc:`EOFError`, :exc:`ValueError`
84 or :exc:`TypeError`) and returns *NULL*.
Georg Brandl54a3faa2008-01-20 09:30:57 +000085
86
Serhiy Storchakac611a5b2017-03-12 08:53:22 +020087.. c:function:: PyObject* PyMarshal_ReadObjectFromString(const char *data, Py_ssize_t len)
Georg Brandl54a3faa2008-01-20 09:30:57 +000088
Serhiy Storchakac611a5b2017-03-12 08:53:22 +020089 Return a Python object from the data stream in a byte buffer
90 containing *len* bytes pointed to by *data*.
Victor Stinner6a318d42015-03-18 13:58:49 +010091
Berker Peksagdefcffd2018-07-27 07:35:11 +030092 On error, sets the appropriate exception (:exc:`EOFError`, :exc:`ValueError`
93 or :exc:`TypeError`) and returns *NULL*.
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000094