• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. highlight:: c
2
3.. _marshalling-utils:
4
5Data marshalling support
6========================
7
8These 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.
13
14Numeric values are stored with the least significant byte first.
15
16The 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.
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:type:`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:type:`long` type.  *version* indicates the file format.
27
28
29.. c:function:: void PyMarshal_WriteObjectToFile(PyObject *value, FILE *file, int version)
30
31   Marshal a Python object, *value*, to *file*.
32   *version* indicates the file format.
33
34
35.. c:function:: PyObject* PyMarshal_WriteObjectToString(PyObject *value, int version)
36
37   Return a bytes object containing the marshalled representation of *value*.
38   *version* indicates the file format.
39
40
41The following functions allow marshalled values to be read back in.
42
43
44.. c:function:: long PyMarshal_ReadLongFromFile(FILE *file)
45
46   Return a C :c:type:`long` from the data stream in a :c:type:`FILE*` opened
47   for reading.  Only a 32-bit value can be read in using this function,
48   regardless of the native size of :c:type:`long`.
49
50   On error, sets the appropriate exception (:exc:`EOFError`) and returns
51   ``-1``.
52
53
54.. c:function:: int PyMarshal_ReadShortFromFile(FILE *file)
55
56   Return a C :c:type:`short` from the data stream in a :c:type:`FILE*` opened
57   for reading.  Only a 16-bit value can be read in using this function,
58   regardless of the native size of :c:type:`short`.
59
60   On error, sets the appropriate exception (:exc:`EOFError`) and returns
61   ``-1``.
62
63
64.. c:function:: PyObject* PyMarshal_ReadObjectFromFile(FILE *file)
65
66   Return a Python object from the data stream in a :c:type:`FILE*` opened for
67   reading.
68
69   On error, sets the appropriate exception (:exc:`EOFError`, :exc:`ValueError`
70   or :exc:`TypeError`) and returns ``NULL``.
71
72
73.. c:function:: PyObject* PyMarshal_ReadLastObjectFromFile(FILE *file)
74
75   Return a Python object from the data stream in a :c:type:`FILE*` opened for
76   reading.  Unlike :c:func:`PyMarshal_ReadObjectFromFile`, this function
77   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
81   anything else from the file.
82
83   On error, sets the appropriate exception (:exc:`EOFError`, :exc:`ValueError`
84   or :exc:`TypeError`) and returns ``NULL``.
85
86
87.. c:function:: PyObject* PyMarshal_ReadObjectFromString(const char *data, Py_ssize_t len)
88
89   Return a Python object from the data stream in a byte buffer
90   containing *len* bytes pointed to by *data*.
91
92   On error, sets the appropriate exception (:exc:`EOFError`, :exc:`ValueError`
93   or :exc:`TypeError`) and returns ``NULL``.
94
95