/Doc/c-api/marshal.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 100 lines · 64 code · 36 blank · 0 comment · 0 complexity · 1ed1522a8939113d71a304fffb1d37e0 MD5 · raw file

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