Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _marshalling-utils: |
| 4 | |
| 5 | Data marshalling support |
| 6 | ======================== |
| 7 | |
Jeroen Ruigrok van der Werven | 47a7d70 | 2009-04-27 05:43:17 +0000 | [diff] [blame] | 8 | These routines allow C code to work with serialized objects using the same |
| 9 | data format as the :mod:`marshal` module. There are functions to write data |
| 10 | into the serialization format, and additional functions that can be used to |
| 11 | read the data back. Files used to store marshalled data must be opened in |
| 12 | binary mode. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 13 | |
| 14 | Numeric values are stored with the least significant byte first. |
| 15 | |
Jeroen Ruigrok van der Werven | 47a7d70 | 2009-04-27 05:43:17 +0000 | [diff] [blame] | 16 | The module supports two versions of the data format: version 0 is the |
| 17 | historical version, version 1 shares interned strings in the file, and upon |
| 18 | unmarshalling. Version 2 uses a binary format for floating point numbers. |
Georg Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 19 | *Py_MARSHAL_VERSION* indicates the current file format (currently 2). |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 20 | |
| 21 | |
| 22 | .. cfunction:: void PyMarshal_WriteLongToFile(long value, FILE *file, int version) |
| 23 | |
Jeroen Ruigrok van der Werven | 47a7d70 | 2009-04-27 05:43:17 +0000 | [diff] [blame] | 24 | Marshal a :ctype:`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 :ctype:`long` type. *version* indicates the file format. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 27 | |
| 28 | |
| 29 | .. cfunction:: 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 | .. cfunction:: PyObject* PyMarshal_WriteObjectToString(PyObject *value, int version) |
| 36 | |
| 37 | Return a string object containing the marshalled representation of *value*. |
| 38 | *version* indicates the file format. |
| 39 | |
| 40 | |
| 41 | The following functions allow marshalled values to be read back in. |
| 42 | |
| 43 | XXX What about error detection? It appears that reading past the end of the |
Jeroen Ruigrok van der Werven | 47a7d70 | 2009-04-27 05:43:17 +0000 | [diff] [blame] | 44 | file will always result in a negative numeric value (where that's relevant), |
| 45 | but it's not clear that negative values won't be handled properly when there's |
| 46 | no error. What's the right way to tell? Should only non-negative values be |
| 47 | written using these routines? |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | .. cfunction:: long PyMarshal_ReadLongFromFile(FILE *file) |
| 51 | |
Jeroen Ruigrok van der Werven | 47a7d70 | 2009-04-27 05:43:17 +0000 | [diff] [blame] | 52 | Return a C :ctype:`long` from the data stream in a :ctype:`FILE\*` opened |
| 53 | for reading. Only a 32-bit value can be read in using this function, |
| 54 | regardless of the native size of :ctype:`long`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 55 | |
| 56 | |
| 57 | .. cfunction:: int PyMarshal_ReadShortFromFile(FILE *file) |
| 58 | |
Jeroen Ruigrok van der Werven | 47a7d70 | 2009-04-27 05:43:17 +0000 | [diff] [blame] | 59 | Return a C :ctype:`short` from the data stream in a :ctype:`FILE\*` opened |
| 60 | for reading. Only a 16-bit value can be read in using this function, |
| 61 | regardless of the native size of :ctype:`short`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 62 | |
| 63 | |
| 64 | .. cfunction:: PyObject* PyMarshal_ReadObjectFromFile(FILE *file) |
| 65 | |
| 66 | Return a Python object from the data stream in a :ctype:`FILE\*` opened for |
| 67 | reading. On error, sets the appropriate exception (:exc:`EOFError` or |
| 68 | :exc:`TypeError`) and returns *NULL*. |
| 69 | |
| 70 | |
| 71 | .. cfunction:: PyObject* PyMarshal_ReadLastObjectFromFile(FILE *file) |
| 72 | |
| 73 | Return a Python object from the data stream in a :ctype:`FILE\*` opened for |
Jeroen Ruigrok van der Werven | 47a7d70 | 2009-04-27 05:43:17 +0000 | [diff] [blame] | 74 | reading. Unlike :cfunc:`PyMarshal_ReadObjectFromFile`, this function |
| 75 | 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 81 | |
| 82 | |
| 83 | .. cfunction:: PyObject* PyMarshal_ReadObjectFromString(char *string, Py_ssize_t len) |
| 84 | |
Jeroen Ruigrok van der Werven | 47a7d70 | 2009-04-27 05:43:17 +0000 | [diff] [blame] | 85 | 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 | |