| 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 |  | 
| Jeroen Ruigrok van der Werven | a4c03ab | 2009-04-25 20:40:10 +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 | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 13 |  | 
|  | 14 | Numeric values are stored with the least significant byte first. | 
|  | 15 |  | 
| Jeroen Ruigrok van der Werven | a4c03ab | 2009-04-25 20:40:10 +0000 | [diff] [blame] | 16 | The module supports two versions of the data format: version 0 is the | 
|  | 17 | historical version, version 1 (new in Python 2.4) shares interned strings in | 
|  | 18 | the file, and upon unmarshalling.  Version 2 (new in Python 2.5) uses a binary | 
|  | 19 | format for floating point numbers.  *Py_MARSHAL_VERSION* indicates the current | 
|  | 20 | file format (currently 2). | 
| Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 21 |  | 
|  | 22 |  | 
| Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 23 | .. c:function:: void PyMarshal_WriteLongToFile(long value, FILE *file, int version) | 
| Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 24 |  | 
| Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 25 | Marshal a :c:type:`long` integer, *value*, to *file*.  This will only write | 
| Jeroen Ruigrok van der Werven | a4c03ab | 2009-04-25 20:40:10 +0000 | [diff] [blame] | 26 | the least-significant 32 bits of *value*; regardless of the size of the | 
| Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 27 | native :c:type:`long` type. | 
| Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 28 |  | 
|  | 29 | .. versionchanged:: 2.4 | 
|  | 30 | *version* indicates the file format. | 
|  | 31 |  | 
|  | 32 |  | 
| Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 33 | .. c:function:: void PyMarshal_WriteObjectToFile(PyObject *value, FILE *file, int version) | 
| Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 34 |  | 
|  | 35 | Marshal a Python object, *value*, to *file*. | 
|  | 36 |  | 
|  | 37 | .. versionchanged:: 2.4 | 
|  | 38 | *version* indicates the file format. | 
|  | 39 |  | 
|  | 40 |  | 
| Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 41 | .. c:function:: PyObject* PyMarshal_WriteObjectToString(PyObject *value, int version) | 
| Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 42 |  | 
|  | 43 | Return a string object containing the marshalled representation of *value*. | 
|  | 44 |  | 
|  | 45 | .. versionchanged:: 2.4 | 
|  | 46 | *version* indicates the file format. | 
|  | 47 |  | 
|  | 48 |  | 
|  | 49 | The following functions allow marshalled values to be read back in. | 
|  | 50 |  | 
|  | 51 | XXX What about error detection?  It appears that reading past the end of the | 
| Jeroen Ruigrok van der Werven | a4c03ab | 2009-04-25 20:40:10 +0000 | [diff] [blame] | 52 | file will always result in a negative numeric value (where that's relevant), | 
|  | 53 | but it's not clear that negative values won't be handled properly when there's | 
|  | 54 | no error.  What's the right way to tell? Should only non-negative values be | 
|  | 55 | written using these routines? | 
| Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 56 |  | 
|  | 57 |  | 
| Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 58 | .. c:function:: long PyMarshal_ReadLongFromFile(FILE *file) | 
| Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 59 |  | 
| Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 60 | Return a C :c:type:`long` from the data stream in a :c:type:`FILE\*` opened | 
| Jeroen Ruigrok van der Werven | a4c03ab | 2009-04-25 20:40:10 +0000 | [diff] [blame] | 61 | for reading.  Only a 32-bit value can be read in using this function, | 
| Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 62 | regardless of the native size of :c:type:`long`. | 
| Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 63 |  | 
|  | 64 |  | 
| Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 65 | .. c:function:: int PyMarshal_ReadShortFromFile(FILE *file) | 
| Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 66 |  | 
| Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 67 | Return a C :c:type:`short` from the data stream in a :c:type:`FILE\*` opened | 
| Jeroen Ruigrok van der Werven | a4c03ab | 2009-04-25 20:40:10 +0000 | [diff] [blame] | 68 | for reading.  Only a 16-bit value can be read in using this function, | 
| Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 69 | regardless of the native size of :c:type:`short`. | 
| Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 70 |  | 
|  | 71 |  | 
| Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 72 | .. c:function:: PyObject* PyMarshal_ReadObjectFromFile(FILE *file) | 
| Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 73 |  | 
| Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 74 | Return a Python object from the data stream in a :c:type:`FILE\*` opened for | 
| Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 75 | reading.  On error, sets the appropriate exception (:exc:`EOFError` or | 
|  | 76 | :exc:`TypeError`) and returns *NULL*. | 
|  | 77 |  | 
|  | 78 |  | 
| Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 79 | .. c:function:: PyObject* PyMarshal_ReadLastObjectFromFile(FILE *file) | 
| Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 80 |  | 
| Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 81 | 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 Werven | a4c03ab | 2009-04-25 20:40:10 +0000 | [diff] [blame] | 83 | 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 Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 89 |  | 
|  | 90 |  | 
| Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 91 | .. c:function:: PyObject* PyMarshal_ReadObjectFromString(char *string, Py_ssize_t len) | 
| Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 92 |  | 
| Jeroen Ruigrok van der Werven | a4c03ab | 2009-04-25 20:40:10 +0000 | [diff] [blame] | 93 | 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 Werven | be82d2f | 2009-04-25 20:41:40 +0000 | [diff] [blame] | 97 |  | 
|  | 98 | .. versionchanged:: 2.5 | 
| Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 99 | This function used an :c:type:`int` type for *len*. This might require | 
| Jeroen Ruigrok van der Werven | be82d2f | 2009-04-25 20:41:40 +0000 | [diff] [blame] | 100 | changes in your code for properly supporting 64-bit systems. |