| 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 two versions of the data format: version 0 is the |
| 17 |
historical version, version 1 shares interned strings in the file, and upon |
| 18 |
unmarshalling. Version 2 uses a binary format for floating-point numbers. |
| 19 |
``Py_MARSHAL_VERSION`` indicates the current file format (currently 2). |
| 20 |
|
| 21 |
|
| 22 |
.. c:function:: void PyMarshal_WriteLongToFile(long value, FILE *file, int version) |
| 23 |
|
| 24 |
Marshal a :c:expr:`long` integer, *value*, to *file*. This will only write |
| 25 |
the least-significant 32 bits of *value*; regardless of the size of the |
| 26 |
native :c:expr:`long` type. *version* indicates the file format. |
| 27 |
|
| 28 |
This function can fail, in which case it sets the error indicator. |
| 29 |
Use :c:func:`PyErr_Occurred` to check for that. |
| 30 |
|
| 31 |
.. c:function:: void PyMarshal_WriteObjectToFile(PyObject *value, FILE *file, int version) |
| 32 |
|
| 33 |
Marshal a Python object, *value*, to *file*. |
| 34 |
*version* indicates the file format. |
| 35 |
|
| 36 |
This function can fail, in which case it sets the error indicator. |
| 37 |
Use :c:func:`PyErr_Occurred` to check for that. |
| 38 |
|
| 39 |
.. c:function:: PyObject* PyMarshal_WriteObjectToString(PyObject *value, int version) |
| 40 |
|
| 41 |
Return a bytes object containing the marshalled representation of *value*. |
| 42 |
*version* indicates the file format. |
| 43 |
|
| 44 |
|
| 45 |
The following functions allow marshalled values to be read back in. |
| 46 |
|
| 47 |
|
| 48 |
.. c:function:: long PyMarshal_ReadLongFromFile(FILE *file) |
| 49 |
|
| 50 |
Return a C :c:expr:`long` from the data stream in a :c:expr:`FILE*` opened |
| 51 |
for reading. Only a 32-bit value can be read in using this function, |
| 52 |
regardless of the native size of :c:expr:`long`. |
| 53 |
|
| 54 |
On error, sets the appropriate exception (:exc:`EOFError`) and returns |
| 55 |
``-1``. |
| 56 |
|
| 57 |
|
| 58 |
.. c:function:: int PyMarshal_ReadShortFromFile(FILE *file) |
| 59 |
|
| 60 |
Return a C :c:expr:`short` from the data stream in a :c:expr:`FILE*` opened |
| 61 |
for reading. Only a 16-bit value can be read in using this function, |
| 62 |
regardless of the native size of :c:expr:`short`. |
| 63 |
|
| 64 |
On error, sets the appropriate exception (:exc:`EOFError`) and returns |
| 65 |
``-1``. |
| 66 |
|
| 67 |
|
| 68 |
.. c:function:: PyObject* PyMarshal_ReadObjectFromFile(FILE *file) |
| 69 |
|
| 70 |
Return a Python object from the data stream in a :c:expr:`FILE*` opened for |
| 71 |
reading. |
| 72 |
|
| 73 |
On error, sets the appropriate exception (:exc:`EOFError`, :exc:`ValueError` |
| 74 |
or :exc:`TypeError`) and returns ``NULL``. |
| 75 |
|
| 76 |
|
| 77 |
.. c:function:: PyObject* PyMarshal_ReadLastObjectFromFile(FILE *file) |
| 78 |
|
| 79 |
Return a Python object from the data stream in a :c:expr:`FILE*` opened for |
| 80 |
reading. Unlike :c:func:`PyMarshal_ReadObjectFromFile`, this function |
| 81 |
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 this variant if you are certain that you won't be reading |
| 85 |
anything else from the file. |
| 86 |
|
| 87 |
On error, sets the appropriate exception (:exc:`EOFError`, :exc:`ValueError` |
| 88 |
or :exc:`TypeError`) and returns ``NULL``. |
| 89 |
|
| 90 |
|
| 91 |
.. c:function:: PyObject* PyMarshal_ReadObjectFromString(const char *data, Py_ssize_t len) |
| 92 |
|
| 93 |
Return a Python object from the data stream in a byte buffer |
| 94 |
containing *len* bytes pointed to by *data*. |
| 95 |
|
| 96 |
On error, sets the appropriate exception (:exc:`EOFError`, :exc:`ValueError` |
| 97 |
or :exc:`TypeError`) and returns ``NULL``. |
| 98 |
|