Debian Codegraph @

Doc/c-api/marshal.rst

raw
1 .. highlight:: c
2
3 .. _marshalling-utils:
4
5 Data marshalling support
6 ========================
7
8 These routines allow C code to work with serialized objects using the same
9 data format as the :mod:`marshal` module. There are functions to write data
10 into the serialization format, and additional functions that can be used to
11 read the data back. Files used to store marshalled data must be opened in
12 binary mode.
13
14 Numeric values are stored with the least significant byte first.
15
16 The module supports several versions of the data format; see
17 the :py:mod:`Python module documentation <marshal>` for details.
18
19 .. c:macro:: Py_MARSHAL_VERSION
20
21 The current format version. See :py:data:`marshal.version`.
22
23 .. c:function:: void PyMarshal_WriteLongToFile(long value, FILE *file, int version)
24
25 Marshal a :c:expr:`long` integer, *value*, to *file*. This will only write
26 the least-significant 32 bits of *value*; regardless of the size of the
27 native :c:expr:`long` type. *version* indicates the file format.
28
29 This function can fail, in which case it sets the error indicator.
30 Use :c:func:`PyErr_Occurred` to check for that.
31
32 .. c:function:: void PyMarshal_WriteObjectToFile(PyObject *value, FILE *file, int version)
33
34 Marshal a Python object, *value*, to *file*.
35 *version* indicates the file format.
36
37 This function can fail, in which case it sets the error indicator.
38 Use :c:func:`PyErr_Occurred` to check for that.
39
40 .. c:function:: PyObject* PyMarshal_WriteObjectToString(PyObject *value, int version)
41
42 Return a bytes object containing the marshalled representation of *value*.
43 *version* indicates the file format.
44
45
46 The following functions allow marshalled values to be read back in.
47
48
49 .. c:function:: long PyMarshal_ReadLongFromFile(FILE *file)
50
51 Return a C :c:expr:`long` from the data stream in a :c:expr:`FILE*` opened
52 for reading. Only a 32-bit value can be read in using this function,
53 regardless of the native size of :c:expr:`long`.
54
55 On error, sets the appropriate exception (:exc:`EOFError`) and returns
56 ``-1``.
57
58
59 .. c:function:: int PyMarshal_ReadShortFromFile(FILE *file)
60
61 Return a C :c:expr:`short` from the data stream in a :c:expr:`FILE*` opened
62 for reading. Only a 16-bit value can be read in using this function,
63 regardless of the native size of :c:expr:`short`.
64
65 On error, sets the appropriate exception (:exc:`EOFError`) and returns
66 ``-1``.
67
68
69 .. c:function:: PyObject* PyMarshal_ReadObjectFromFile(FILE *file)
70
71 Return a Python object from the data stream in a :c:expr:`FILE*` opened for
72 reading.
73
74 On error, sets the appropriate exception (:exc:`EOFError`, :exc:`ValueError`
75 or :exc:`TypeError`) and returns ``NULL``.
76
77
78 .. c:function:: PyObject* PyMarshal_ReadLastObjectFromFile(FILE *file)
79
80 Return a Python object from the data stream in a :c:expr:`FILE*` opened for
81 reading. Unlike :c:func:`PyMarshal_ReadObjectFromFile`, this function
82 assumes that no further objects will be read from the file, allowing it to
83 aggressively load file data into memory so that the de-serialization can
84 operate from data in memory rather than reading a byte at a time from the
85 file. Only use this variant if you are certain that you won't be reading
86 anything else from the file.
87
88 On error, sets the appropriate exception (:exc:`EOFError`, :exc:`ValueError`
89 or :exc:`TypeError`) and returns ``NULL``.
90
91
92 .. c:function:: PyObject* PyMarshal_ReadObjectFromString(const char *data, Py_ssize_t len)
93
94 Return a Python object from the data stream in a byte buffer
95 containing *len* bytes pointed to by *data*.
96
97 On error, sets the appropriate exception (:exc:`EOFError`, :exc:`ValueError`
98 or :exc:`TypeError`) and returns ``NULL``.
99