blob: b82cc96b64acd9893a7d1b6810237a957c1dc51a [file] [log] [blame]
Georg Brandl79e3d552008-01-19 22:14:27 +00001.. highlightlang:: c
2
3.. _marshalling-utils:
4
5Data marshalling support
6========================
7
Jeroen Ruigrok van der Wervena4c03ab2009-04-25 20:40:10 +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 Brandl79e3d552008-01-19 22:14:27 +000013
14Numeric values are stored with the least significant byte first.
15
Jeroen Ruigrok van der Wervena4c03ab2009-04-25 20:40:10 +000016The module supports two versions of the data format: version 0 is the
17historical version, version 1 (new in Python 2.4) shares interned strings in
18the file, and upon unmarshalling. Version 2 (new in Python 2.5) uses a binary
19format for floating point numbers. *Py_MARSHAL_VERSION* indicates the current
20file format (currently 2).
Georg Brandl79e3d552008-01-19 22:14:27 +000021
22
Sandro Tosi98ed08f2012-01-14 16:42:02 +010023.. c:function:: void PyMarshal_WriteLongToFile(long value, FILE *file, int version)
Georg Brandl79e3d552008-01-19 22:14:27 +000024
Sandro Tosi98ed08f2012-01-14 16:42:02 +010025 Marshal a :c:type:`long` integer, *value*, to *file*. This will only write
Jeroen Ruigrok van der Wervena4c03ab2009-04-25 20:40:10 +000026 the least-significant 32 bits of *value*; regardless of the size of the
Sandro Tosi98ed08f2012-01-14 16:42:02 +010027 native :c:type:`long` type.
Georg Brandl79e3d552008-01-19 22:14:27 +000028
29 .. versionchanged:: 2.4
30 *version* indicates the file format.
31
32
Sandro Tosi98ed08f2012-01-14 16:42:02 +010033.. c:function:: void PyMarshal_WriteObjectToFile(PyObject *value, FILE *file, int version)
Georg Brandl79e3d552008-01-19 22:14:27 +000034
35 Marshal a Python object, *value*, to *file*.
36
37 .. versionchanged:: 2.4
38 *version* indicates the file format.
39
40
Sandro Tosi98ed08f2012-01-14 16:42:02 +010041.. c:function:: PyObject* PyMarshal_WriteObjectToString(PyObject *value, int version)
Georg Brandl79e3d552008-01-19 22:14:27 +000042
43 Return a string object containing the marshalled representation of *value*.
44
45 .. versionchanged:: 2.4
46 *version* indicates the file format.
47
48
49The following functions allow marshalled values to be read back in.
50
51XXX What about error detection? It appears that reading past the end of the
Jeroen Ruigrok van der Wervena4c03ab2009-04-25 20:40:10 +000052file will always result in a negative numeric value (where that's relevant),
53but it's not clear that negative values won't be handled properly when there's
54no error. What's the right way to tell? Should only non-negative values be
55written using these routines?
Georg Brandl79e3d552008-01-19 22:14:27 +000056
57
Sandro Tosi98ed08f2012-01-14 16:42:02 +010058.. c:function:: long PyMarshal_ReadLongFromFile(FILE *file)
Georg Brandl79e3d552008-01-19 22:14:27 +000059
Sandro Tosi98ed08f2012-01-14 16:42:02 +010060 Return a C :c:type:`long` from the data stream in a :c:type:`FILE\*` opened
Jeroen Ruigrok van der Wervena4c03ab2009-04-25 20:40:10 +000061 for reading. Only a 32-bit value can be read in using this function,
Sandro Tosi98ed08f2012-01-14 16:42:02 +010062 regardless of the native size of :c:type:`long`.
Georg Brandl79e3d552008-01-19 22:14:27 +000063
64
Sandro Tosi98ed08f2012-01-14 16:42:02 +010065.. c:function:: int PyMarshal_ReadShortFromFile(FILE *file)
Georg Brandl79e3d552008-01-19 22:14:27 +000066
Sandro Tosi98ed08f2012-01-14 16:42:02 +010067 Return a C :c:type:`short` from the data stream in a :c:type:`FILE\*` opened
Jeroen Ruigrok van der Wervena4c03ab2009-04-25 20:40:10 +000068 for reading. Only a 16-bit value can be read in using this function,
Sandro Tosi98ed08f2012-01-14 16:42:02 +010069 regardless of the native size of :c:type:`short`.
Georg Brandl79e3d552008-01-19 22:14:27 +000070
71
Sandro Tosi98ed08f2012-01-14 16:42:02 +010072.. c:function:: PyObject* PyMarshal_ReadObjectFromFile(FILE *file)
Georg Brandl79e3d552008-01-19 22:14:27 +000073
Sandro Tosi98ed08f2012-01-14 16:42:02 +010074 Return a Python object from the data stream in a :c:type:`FILE\*` opened for
Georg Brandl79e3d552008-01-19 22:14:27 +000075 reading. On error, sets the appropriate exception (:exc:`EOFError` or
76 :exc:`TypeError`) and returns *NULL*.
77
78
Sandro Tosi98ed08f2012-01-14 16:42:02 +010079.. c:function:: PyObject* PyMarshal_ReadLastObjectFromFile(FILE *file)
Georg Brandl79e3d552008-01-19 22:14:27 +000080
Sandro Tosi98ed08f2012-01-14 16:42:02 +010081 Return a Python object from the data stream in a :c:type:`FILE\*` opened for
82 reading. Unlike :c:func:`PyMarshal_ReadObjectFromFile`, this function
Jeroen Ruigrok van der Wervena4c03ab2009-04-25 20:40:10 +000083 assumes that no further objects will be read from the file, allowing it to
84 aggressively load file data into memory so that the de-serialization can
85 operate from data in memory rather than reading a byte at a time from the
86 file. Only use these variant if you are certain that you won't be reading
87 anything else from the file. On error, sets the appropriate exception
88 (:exc:`EOFError` or :exc:`TypeError`) and returns *NULL*.
Georg Brandl79e3d552008-01-19 22:14:27 +000089
90
Sandro Tosi98ed08f2012-01-14 16:42:02 +010091.. c:function:: PyObject* PyMarshal_ReadObjectFromString(char *string, Py_ssize_t len)
Georg Brandl79e3d552008-01-19 22:14:27 +000092
Jeroen Ruigrok van der Wervena4c03ab2009-04-25 20:40:10 +000093 Return a Python object from the data stream in a character buffer
94 containing *len* bytes pointed to by *string*. On error, sets the
95 appropriate exception (:exc:`EOFError` or :exc:`TypeError`) and returns
96 *NULL*.
Jeroen Ruigrok van der Wervenbe82d2f2009-04-25 20:41:40 +000097
98 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +010099 This function used an :c:type:`int` type for *len*. This might require
Jeroen Ruigrok van der Wervenbe82d2f2009-04-25 20:41:40 +0000100 changes in your code for properly supporting 64-bit systems.