blob: c6d1d02a2fa510e99692b8ba4f2e8cd6f18c8050 [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
43XXX What about error detection? It appears that reading past the end of the
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000044file will always result in a negative numeric value (where that's relevant),
45but it's not clear that negative values won't be handled properly when there's
46no error. What's the right way to tell? Should only non-negative values be
47written using these routines?
Georg Brandl54a3faa2008-01-20 09:30:57 +000048
49
Georg Brandl60203b42010-10-06 10:11:56 +000050.. c:function:: long PyMarshal_ReadLongFromFile(FILE *file)
Georg Brandl54a3faa2008-01-20 09:30:57 +000051
Georg Brandl60203b42010-10-06 10:11:56 +000052 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 +000053 for reading. Only a 32-bit value can be read in using this function,
Georg Brandl60203b42010-10-06 10:11:56 +000054 regardless of the native size of :c:type:`long`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000055
Victor Stinner6a318d42015-03-18 13:58:49 +010056 On error, raise an exception and return ``-1``.
57
Georg Brandl54a3faa2008-01-20 09:30:57 +000058
Georg Brandl60203b42010-10-06 10:11:56 +000059.. c:function:: int PyMarshal_ReadShortFromFile(FILE *file)
Georg Brandl54a3faa2008-01-20 09:30:57 +000060
Georg Brandl60203b42010-10-06 10:11:56 +000061 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 +000062 for reading. Only a 16-bit value can be read in using this function,
Georg Brandl60203b42010-10-06 10:11:56 +000063 regardless of the native size of :c:type:`short`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000064
Victor Stinner6a318d42015-03-18 13:58:49 +010065 On error, raise an exception and return ``-1``.
66
Georg Brandl54a3faa2008-01-20 09:30:57 +000067
Georg Brandl60203b42010-10-06 10:11:56 +000068.. c:function:: PyObject* PyMarshal_ReadObjectFromFile(FILE *file)
Georg Brandl54a3faa2008-01-20 09:30:57 +000069
Georg Brandl60203b42010-10-06 10:11:56 +000070 Return a Python object from the data stream in a :c:type:`FILE\*` opened for
Victor Stinner6a318d42015-03-18 13:58:49 +010071 reading.
72
73 On error, sets the appropriate exception (:exc:`EOFError` or
Georg Brandl54a3faa2008-01-20 09:30:57 +000074 :exc:`TypeError`) and returns *NULL*.
75
76
Georg Brandl60203b42010-10-06 10:11:56 +000077.. c:function:: PyObject* PyMarshal_ReadLastObjectFromFile(FILE *file)
Georg Brandl54a3faa2008-01-20 09:30:57 +000078
Georg Brandl60203b42010-10-06 10:11:56 +000079 Return a Python object from the data stream in a :c:type:`FILE\*` opened for
80 reading. Unlike :c:func:`PyMarshal_ReadObjectFromFile`, this function
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000081 assumes that no further objects will be read from the file, allowing it to
82 aggressively load file data into memory so that the de-serialization can
83 operate from data in memory rather than reading a byte at a time from the
84 file. Only use these variant if you are certain that you won't be reading
Victor Stinner6a318d42015-03-18 13:58:49 +010085 anything else from the file.
86
87 On error, sets the appropriate exception (:exc:`EOFError` or
88 :exc:`TypeError`) and returns *NULL*.
Georg Brandl54a3faa2008-01-20 09:30:57 +000089
90
Serhiy Storchakac611a5b2017-03-12 08:53:22 +020091.. c:function:: PyObject* PyMarshal_ReadObjectFromString(const char *data, Py_ssize_t len)
Georg Brandl54a3faa2008-01-20 09:30:57 +000092
Serhiy Storchakac611a5b2017-03-12 08:53:22 +020093 Return a Python object from the data stream in a byte buffer
94 containing *len* bytes pointed to by *data*.
Victor Stinner6a318d42015-03-18 13:58:49 +010095
96 On error, sets the appropriate exception (:exc:`EOFError` or
97 :exc:`TypeError`) and returns *NULL*.
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000098