blob: 6d9879cb6114865002fb70666d19257ff1d882e2 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _marshalling-utils:
4
5Data marshalling support
6========================
7
8These routines allow C code to work with serialized objects using the same data
9format as the :mod:`marshal` module. There are functions to write data into the
10serialization format, and additional functions that can be used to read the data
11back. Files used to store marshalled data must be opened in binary mode.
12
13Numeric values are stored with the least significant byte first.
14
15The module supports two versions of the data format: version 0 is the historical
Georg Brandle6bcc912008-05-12 18:05:20 +000016version, version 1 shares interned strings in the file, and upon unmarshalling.
17Version 2 uses a binary format for floating point numbers.
18*Py_MARSHAL_VERSION* indicates the current file format (currently 2).
Georg Brandl54a3faa2008-01-20 09:30:57 +000019
20
21.. cfunction:: void PyMarshal_WriteLongToFile(long value, FILE *file, int version)
22
23 Marshal a :ctype:`long` integer, *value*, to *file*. This will only write the
24 least-significant 32 bits of *value*; regardless of the size of the native
25 :ctype:`long` type. *version* indicates the file format.
26
27
28.. cfunction:: void PyMarshal_WriteObjectToFile(PyObject *value, FILE *file, int version)
29
30 Marshal a Python object, *value*, to *file*.
31 *version* indicates the file format.
32
33
34.. cfunction:: PyObject* PyMarshal_WriteObjectToString(PyObject *value, int version)
35
36 Return a string object containing the marshalled representation of *value*.
37 *version* indicates the file format.
38
39
40The following functions allow marshalled values to be read back in.
41
42XXX What about error detection? It appears that reading past the end of the
43file will always result in a negative numeric value (where that's relevant), but
44it's not clear that negative values won't be handled properly when there's no
45error. What's the right way to tell? Should only non-negative values be written
46using these routines?
47
48
49.. cfunction:: long PyMarshal_ReadLongFromFile(FILE *file)
50
51 Return a C :ctype:`long` from the data stream in a :ctype:`FILE\*` opened for
52 reading. Only a 32-bit value can be read in using this function, regardless of
53 the native size of :ctype:`long`.
54
55
56.. cfunction:: int PyMarshal_ReadShortFromFile(FILE *file)
57
58 Return a C :ctype:`short` from the data stream in a :ctype:`FILE\*` opened for
59 reading. Only a 16-bit value can be read in using this function, regardless of
60 the native size of :ctype:`short`.
61
62
63.. cfunction:: PyObject* PyMarshal_ReadObjectFromFile(FILE *file)
64
65 Return a Python object from the data stream in a :ctype:`FILE\*` opened for
66 reading. On error, sets the appropriate exception (:exc:`EOFError` or
67 :exc:`TypeError`) and returns *NULL*.
68
69
70.. cfunction:: PyObject* PyMarshal_ReadLastObjectFromFile(FILE *file)
71
72 Return a Python object from the data stream in a :ctype:`FILE\*` opened for
73 reading. Unlike :cfunc:`PyMarshal_ReadObjectFromFile`, this function assumes
74 that no further objects will be read from the file, allowing it to aggressively
75 load file data into memory so that the de-serialization can operate from data in
76 memory rather than reading a byte at a time from the file. Only use these
77 variant if you are certain that you won't be reading anything else from the
78 file. On error, sets the appropriate exception (:exc:`EOFError` or
79 :exc:`TypeError`) and returns *NULL*.
80
81
82.. cfunction:: PyObject* PyMarshal_ReadObjectFromString(char *string, Py_ssize_t len)
83
84 Return a Python object from the data stream in a character buffer containing
85 *len* bytes pointed to by *string*. On error, sets the appropriate exception
86 (:exc:`EOFError` or :exc:`TypeError`) and returns *NULL*.