blob: da402a8086245eb24a4c3505c1e247a2b9ceb4f8 [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
37 Return a string 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
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
56
Georg Brandl60203b42010-10-06 10:11:56 +000057.. c:function:: int PyMarshal_ReadShortFromFile(FILE *file)
Georg Brandl54a3faa2008-01-20 09:30:57 +000058
Georg Brandl60203b42010-10-06 10:11:56 +000059 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 +000060 for reading. Only a 16-bit value can be read in using this function,
Georg Brandl60203b42010-10-06 10:11:56 +000061 regardless of the native size of :c:type:`short`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000062
63
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
Georg Brandl54a3faa2008-01-20 09:30:57 +000067 reading. On error, sets the appropriate exception (:exc:`EOFError` or
68 :exc:`TypeError`) and returns *NULL*.
69
70
Georg Brandl60203b42010-10-06 10:11:56 +000071.. c:function:: PyObject* PyMarshal_ReadLastObjectFromFile(FILE *file)
Georg Brandl54a3faa2008-01-20 09:30:57 +000072
Georg Brandl60203b42010-10-06 10:11:56 +000073 Return a Python object from the data stream in a :c:type:`FILE\*` opened for
74 reading. Unlike :c:func:`PyMarshal_ReadObjectFromFile`, this function
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000075 assumes that no further objects will be read from the file, allowing it to
76 aggressively load file data into memory so that the de-serialization can
77 operate from data in memory rather than reading a byte at a time from the
78 file. Only use these variant if you are certain that you won't be reading
79 anything else from the file. On error, sets the appropriate exception
80 (:exc:`EOFError` or :exc:`TypeError`) and returns *NULL*.
Georg Brandl54a3faa2008-01-20 09:30:57 +000081
82
Georg Brandl60203b42010-10-06 10:11:56 +000083.. c:function:: PyObject* PyMarshal_ReadObjectFromString(char *string, Py_ssize_t len)
Georg Brandl54a3faa2008-01-20 09:30:57 +000084
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000085 Return a Python object from the data stream in a character buffer
86 containing *len* bytes pointed to by *string*. On error, sets the
87 appropriate exception (:exc:`EOFError` or :exc:`TypeError`) and returns
88 *NULL*.
89