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