/Doc/library/marshal.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 131 lines · 92 code · 39 blank · 0 comment · 0 complexity · 79403115d47629ba5c7e235faa8b56b6 MD5 · raw file

  1. :mod:`marshal` --- Internal Python object serialization
  2. =======================================================
  3. .. module:: marshal
  4. :synopsis: Convert Python objects to streams of bytes and back (with different
  5. constraints).
  6. This module contains functions that can read and write Python values in a binary
  7. format. The format is specific to Python, but independent of machine
  8. architecture issues (e.g., you can write a Python value to a file on a PC,
  9. transport the file to a Sun, and read it back there). Details of the format are
  10. undocumented on purpose; it may change between Python versions (although it
  11. rarely does). [#]_
  12. .. index::
  13. module: pickle
  14. module: shelve
  15. object: code
  16. This is not a general "persistence" module. For general persistence and
  17. transfer of Python objects through RPC calls, see the modules :mod:`pickle` and
  18. :mod:`shelve`. The :mod:`marshal` module exists mainly to support reading and
  19. writing the "pseudo-compiled" code for Python modules of :file:`.pyc` files.
  20. Therefore, the Python maintainers reserve the right to modify the marshal format
  21. in backward incompatible ways should the need arise. If you're serializing and
  22. de-serializing Python objects, use the :mod:`pickle` module instead -- the
  23. performance is comparable, version independence is guaranteed, and pickle
  24. supports a substantially wider range of objects than marshal.
  25. .. warning::
  26. The :mod:`marshal` module is not intended to be secure against erroneous or
  27. maliciously constructed data. Never unmarshal data received from an
  28. untrusted or unauthenticated source.
  29. Not all Python object types are supported; in general, only objects whose value
  30. is independent from a particular invocation of Python can be written and read by
  31. this module. The following types are supported: ``None``, integers, long
  32. integers, floating point numbers, strings, Unicode objects, tuples, lists, sets,
  33. dictionaries, and code objects, where it should be understood that tuples, lists
  34. and dictionaries are only supported as long as the values contained therein are
  35. themselves supported; and recursive lists and dictionaries should not be written
  36. (they will cause infinite loops).
  37. .. warning::
  38. On machines where C's ``long int`` type has more than 32 bits (such as the
  39. DEC Alpha), it is possible to create plain Python integers that are longer
  40. than 32 bits. If such an integer is marshaled and read back in on a machine
  41. where C's ``long int`` type has only 32 bits, a Python long integer object
  42. is returned instead. While of a different type, the numeric value is the
  43. same. (This behavior is new in Python 2.2. In earlier versions, all but the
  44. least-significant 32 bits of the value were lost, and a warning message was
  45. printed.)
  46. There are functions that read/write files as well as functions operating on
  47. strings.
  48. The module defines these functions:
  49. .. function:: dump(value, file[, version])
  50. Write the value on the open file. The value must be a supported type. The
  51. file must be an open file object such as ``sys.stdout`` or returned by
  52. :func:`open` or :func:`os.popen`. It must be opened in binary mode (``'wb'``
  53. or ``'w+b'``).
  54. If the value has (or contains an object that has) an unsupported type, a
  55. :exc:`ValueError` exception is raised --- but garbage data will also be written
  56. to the file. The object will not be properly read back by :func:`load`.
  57. .. versionadded:: 2.4
  58. The *version* argument indicates the data format that ``dump`` should use
  59. (see below).
  60. .. function:: load(file)
  61. Read one value from the open file and return it. If no valid value is read
  62. (e.g. because the data has a different Python version's incompatible marshal
  63. format), raise :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. The
  64. file must be an open file object opened in binary mode (``'rb'`` or
  65. ``'r+b'``).
  66. .. note::
  67. If an object containing an unsupported type was marshalled with :func:`dump`,
  68. :func:`load` will substitute ``None`` for the unmarshallable type.
  69. .. function:: dumps(value[, version])
  70. Return the string that would be written to a file by ``dump(value, file)``. The
  71. value must be a supported type. Raise a :exc:`ValueError` exception if value
  72. has (or contains an object that has) an unsupported type.
  73. .. versionadded:: 2.4
  74. The *version* argument indicates the data format that ``dumps`` should use
  75. (see below).
  76. .. function:: loads(string)
  77. Convert the string to a value. If no valid value is found, raise
  78. :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. Extra characters in the
  79. string are ignored.
  80. In addition, the following constants are defined:
  81. .. data:: version
  82. Indicates the format that the module uses. Version 0 is the historical format,
  83. version 1 (added in Python 2.4) shares interned strings and version 2 (added in
  84. Python 2.5) uses a binary format for floating point numbers. The current version
  85. is 2.
  86. .. versionadded:: 2.4
  87. .. rubric:: Footnotes
  88. .. [#] The name of this module stems from a bit of terminology used by the designers of
  89. Modula-3 (amongst others), who use the term "marshalling" for shipping of data
  90. around in a self-contained form. Strictly speaking, "to marshal" means to
  91. convert some data from internal to external form (in an RPC buffer for instance)
  92. and "unmarshalling" for the reverse process.