home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / m / marshal < prev    next >
Encoding:
Text File  |  1996-11-14  |  4.7 KB  |  89 lines

  1. <TITLE>marshal -- Python library reference</TITLE>
  2. Next: <A HREF="../i/imp" TYPE="Next">imp</A>  
  3. Prev: <A HREF="../c/copy" TYPE="Prev">copy</A>  
  4. Up: <A HREF="../p/python_services" TYPE="Up">Python Services</A>  
  5. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  6. <H1>3.7. Built-in Module <CODE>marshal</CODE></H1>
  7. This module contains functions that can read and write Python
  8. values in a binary format.  The format is specific to Python, but
  9. independent of machine architecture issues (e.g., you can write a
  10. Python value to a file on a PC, transport the file to a Sun, and read
  11. it back there).  Details of the format are undocumented on purpose;
  12. it may change between Python versions (although it rarely does).<A NAME="footnoteref1" HREF="#footnotetext1">(1)</A>
  13. <P>
  14. This is not a general ``persistency'' module.  For general persistency
  15. and transfer of Python objects through RPC calls, see the modules
  16. <CODE>pickle</CODE> and <CODE>shelve</CODE>.  The <CODE>marshal</CODE> module exists
  17. mainly to support reading and writing the ``pseudo-compiled'' code for
  18. Python modules of `<SAMP>.pyc</SAMP>' files.
  19. Not all Python object types are supported; in general, only objects
  20. whose value is independent from a particular invocation of Python can
  21. be written and read by this module.  The following types are supported:
  22. <CODE>None</CODE>, integers, long integers, floating point numbers,
  23. strings, tuples, lists, dictionaries, and code objects, where it
  24. should be understood that tuples, lists and dictionaries are only
  25. supported as long as the values contained therein are themselves
  26. supported; and recursive lists and dictionaries should not be written
  27. (they will cause infinite loops).
  28. <P>
  29. <B>Caveat:</B> On machines where C's <CODE>long int</CODE> type has more than
  30. 32 bits (such as the DEC Alpha), it
  31. is possible to create plain Python integers that are longer than 32
  32. bits.  Since the current <CODE>marshal</CODE> module uses 32 bits to
  33. transfer plain Python integers, such values are silently truncated.
  34. This particularly affects the use of very long integer literals in
  35. Python modules --- these will be accepted by the parser on such
  36. machines, but will be silently be truncated when the module is read
  37. from the <CODE>.pyc</CODE> instead.<A NAME="footnoteref2" HREF="#footnotetext2">(2)</A>
  38. <P>
  39. There are functions that read/write files as well as functions
  40. operating on strings.
  41. <P>
  42. The module defines these functions:
  43. <P>
  44. <DL><DT><B>dump</B> (<VAR>value</VAR>, <VAR>file</VAR>) -- function of module marshal<DD>
  45. Write the value on the open file.  The value must be a supported
  46. type.  The file must be an open file object such as
  47. <CODE>sys.stdout</CODE> or returned by <CODE>open()</CODE> or
  48. <CODE>posix.popen()</CODE>.
  49. <P>
  50. If the value has (or contains an object that has) an unsupported type,
  51. a <CODE>ValueError</CODE> exception is raised -- but garbage data will also
  52. be written to the file.  The object will not be properly read back by
  53. <CODE>load()</CODE>.
  54. </DL>
  55. <DL><DT><B>load</B> (<VAR>file</VAR>) -- function of module marshal<DD>
  56. Read one value from the open file and return it.  If no valid value
  57. is read, raise <CODE>EOFError</CODE>, <CODE>ValueError</CODE> or
  58. <CODE>TypeError</CODE>.  The file must be an open file object.
  59. <P>
  60. Warning: If an object containing an unsupported type was marshalled
  61. with <CODE>dump()</CODE>, <CODE>load()</CODE> will substitute <CODE>None</CODE> for the
  62. unmarshallable type.
  63. </DL>
  64. <DL><DT><B>dumps</B> (<VAR>value</VAR>) -- function of module marshal<DD>
  65. Return the string that would be written to a file by
  66. <CODE>dump(value, file)</CODE>.  The value must be a supported type.
  67. Raise a <CODE>ValueError</CODE> exception if value has (or contains an
  68. object that has) an unsupported type.
  69. </DL>
  70. <DL><DT><B>loads</B> (<VAR>string</VAR>) -- function of module marshal<DD>
  71. Convert the string to a value.  If no valid value is found, raise
  72. <CODE>EOFError</CODE>, <CODE>ValueError</CODE> or <CODE>TypeError</CODE>.  Extra
  73. characters in the string are ignored.
  74. </DL>
  75. <H2>---------- Footnotes ----------</H2>
  76. <A NAME="footnotetext1" HREF="#footnoteref1">(1)</A>
  77. The name of this module stems from a bit of terminology used
  78. by the designers of Modula-3 (amongst others), who use the term
  79. ``marshalling'' for shipping of data around in a self-contained form.
  80. Strictly speaking, ``to marshal'' means to convert some data from
  81. internal to external form (in an RPC buffer for instance) and
  82. ``unmarshalling'' for the reverse process.<P>
  83. <A NAME="footnotetext2" HREF="#footnoteref2">(2)</A>
  84. A solution would be to refuse such literals in the parser,
  85. since they are inherently non-portable.  Another solution would be to
  86. let the <CODE>marshal</CODE> module raise an exception when an integer value
  87. would be truncated.  At least one of these solutions will be
  88. implemented in a future version.<P>
  89.