“Python/docs/3.9/c-api/marshal”的版本间差异

来自菜鸟教程
Python/docs/3.9/c-api/marshal
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:数据编组支持 — Python 文档}}
 
<div id="data-marshalling-support" class="section">
 
<div id="data-marshalling-support" class="section">
  
 
<span id="marshalling-utils"></span>
 
<span id="marshalling-utils"></span>
= Data marshalling support =
+
= 数据编组支持 =
  
These routines allow C code to work with serialized objects using the same
+
这些例程允许 C 代码使用与 [[../../library/marshal#module-marshal|marshal]] 模块相同的数据格式处理序列化对象。 有将数据写入序列化格式的函数,以及可用于读回数据的附加函数。 用于存储编组数据的文件必须以二进制模式打开。
data format as the [[../../library/marshal#module-marshal|<code>marshal</code>]] module. There are functions to write data
 
into the serialization format, and additional functions that can be used to
 
read the data back. Files used to store marshalled data must be opened in
 
binary mode.
 
  
Numeric values are stored with the least significant byte first.
+
数值首先以最低有效字节存储。
  
The module supports two versions of the data format: version 0 is the
+
该模块支持两种版本的数据格式:版本 0 是历史版本,版本 1 共享文件中的内部字符串,以及解组时。 版本 2 对浮点数使用二进制格式。 <code>Py_MARSHAL_VERSION</code> 表示当前文件格式(当前为 2)。
historical version, version 1 shares interned strings in the file, and upon
 
unmarshalling. Version 2 uses a binary format for floating point numbers.
 
<code>Py_MARSHAL_VERSION</code> indicates the current file format (currently 2).
 
  
; void <code>PyMarshal_WriteLongToFile</code><span class="sig-paren">(</span>long ''value'', FILE *''file'', int ''version''<span class="sig-paren">)</span>
+
; <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyMarshal_WriteLongToFile</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">long</span></span><span class="w"> </span><span class="n"><span class="pre">value</span></span>, <span class="n"><span class="pre">FILE</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">file</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">version</span></span><span class="sig-paren">)</span><br />
: Marshal a <span class="xref c c-texpr">long</span> integer, ''value'', to ''file''. This will only write the least-significant 32 bits of ''value''; regardless of the size of the native <span class="xref c c-texpr">long</span> type. ''version'' indicates the file format.
 
  
; void <code>PyMarshal_WriteObjectToFile</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''value'', FILE *''file'', int ''version''<span class="sig-paren">)</span>
+
: 将 <span class="xref c c-texpr">long</span> 整数 '''' 编组到 ''文件'' 。 这只会写入 ''value'' 的最低有效 32 位; 无论原生 <span class="xref c c-texpr">long</span> 类型的大小如何。 ''version'' 表示文件格式。
: Marshal a Python object, ''value'', to ''file''. ''version'' indicates the file format.
 
  
<dl>
+
; <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyMarshal_WriteObjectToFile</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">value</span></span>, <span class="n"><span class="pre">FILE</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">file</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">version</span></span><span class="sig-paren">)</span><br />
<dt>[[../structures#c|PyObject]] *<code>PyMarshal_WriteObjectToString</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''value'', int ''version''<span class="sig-paren">)</span></dt>
+
 
<dd><p>''Return value: New reference.''</p>
+
: 将 Python 对象 ''value'' 编组到 ''file''。 ''version'' 表示文件格式。
<p>Return a bytes object containing the marshalled representation of ''value''.
+
 
''version'' indicates the file format.</p></dd></dl>
+
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyMarshal_WriteObjectToString</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">value</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">version</span></span><span class="sig-paren">)</span><br />
 +
 
 +
: 返回包含 ''值'' 的编组表示的字节对象。 ''version'' 表示文件格式。
  
The following functions allow marshalled values to be read back in.
+
以下函数允许读回编组的值。
  
 
<dl>
 
<dl>
<dt>long <code>PyMarshal_ReadLongFromFile</code><span class="sig-paren">(</span>FILE *''file''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">long</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyMarshal_ReadLongFromFile</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">FILE</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">file</span></span><span class="sig-paren">)</span><br />
<dd><p>Return a C <span class="xref c c-texpr">long</span> from the data stream in a <span class="xref c c-texpr">FILE*</span> opened
+
</dt>
for reading. Only a 32-bit value can be read in using this function,
+
<dd><p>从打开以供读取的 <span class="xref c c-texpr">FILE*</span> 中的数据流中返回 C <span class="xref c c-texpr">long</span>。 无论 <span class="xref c c-texpr">long</span> 的本机大小如何,使用此函数只能读取 32 位值。</p>
regardless of the native size of <span class="xref c c-texpr">long</span>.</p>
+
<p>出错时,设置适当的异常 ([[../../library/exceptions#EOFError|EOFError]]) 并返回 <code>-1</code></p></dd></dl>
<p>On error, sets the appropriate exception ([[../../library/exceptions#EOFError|<code>EOFError</code>]]) and returns
 
<code>-1</code>.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>int <code>PyMarshal_ReadShortFromFile</code><span class="sig-paren">(</span>FILE *''file''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyMarshal_ReadShortFromFile</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">FILE</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">file</span></span><span class="sig-paren">)</span><br />
<dd><p>Return a C <span class="xref c c-texpr">short</span> from the data stream in a <span class="xref c c-texpr">FILE*</span> opened
+
</dt>
for reading. Only a 16-bit value can be read in using this function,
+
<dd><p>从打开以供读取的 <span class="xref c c-texpr">FILE*</span> 中的数据流中返回 C <span class="xref c c-texpr">short</span>。 无论 <span class="xref c c-texpr">short</span> 的本机大小如何,使用此函数只能读取 16 位值。</p>
regardless of the native size of <span class="xref c c-texpr">short</span>.</p>
+
<p>出错时,设置适当的异常 ([[../../library/exceptions#EOFError|EOFError]]) 并返回 <code>-1</code></p></dd></dl>
<p>On error, sets the appropriate exception ([[../../library/exceptions#EOFError|<code>EOFError</code>]]) and returns
 
<code>-1</code>.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyMarshal_ReadObjectFromFile</code><span class="sig-paren">(</span>FILE *''file''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyMarshal_ReadObjectFromFile</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">FILE</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">file</span></span><span class="sig-paren">)</span><br />
<dd><p>''Return value: New reference.''</p>
+
</dt>
<p>Return a Python object from the data stream in a <span class="xref c c-texpr">FILE*</span> opened for
+
<dd><p>从打开以供读取的 <span class="xref c c-texpr">FILE*</span> 中的数据流中返回 Python 对象。</p>
reading.</p>
+
<p>出错时,设置适当的异常([[../../library/exceptions#EOFError|EOFError]][[../../library/exceptions#ValueError|ValueError]] [[../../library/exceptions#TypeError|TypeError]])并返回 <code>NULL</code></p></dd></dl>
<p>On error, sets the appropriate exception ([[../../library/exceptions#EOFError|<code>EOFError</code>]], [[../../library/exceptions#ValueError|<code>ValueError</code>]]
 
or [[../../library/exceptions#TypeError|<code>TypeError</code>]]) and returns <code>NULL</code>.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyMarshal_ReadLastObjectFromFile</code><span class="sig-paren">(</span>FILE *''file''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyMarshal_ReadLastObjectFromFile</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">FILE</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">file</span></span><span class="sig-paren">)</span><br />
<dd><p>''Return value: New reference.''</p>
+
</dt>
<p>Return a Python object from the data stream in a <span class="xref c c-texpr">FILE*</span> opened for
+
<dd><p>从打开以供读取的 <span class="xref c c-texpr">FILE*</span> 中的数据流中返回 Python 对象。 与 [[#c.PyMarshal_ReadObjectFromFile|PyMarshal_ReadObjectFromFile()]] 不同,此函数假定不会从文件中读取更多对象,从而允许它主动将文件数据加载到内存中,以便反序列化可以从内存中的数据进行操作,而不是读取从文件中一次一个字节。 仅当您确定不会从文件中读取任何其他内容时才使用这些变体。</p>
reading. Unlike [[#c.PyMarshal_ReadObjectFromFile|<code>PyMarshal_ReadObjectFromFile()</code>]], this function
+
<p>出错时,设置适当的异常([[../../library/exceptions#EOFError|EOFError]][[../../library/exceptions#ValueError|ValueError]] [[../../library/exceptions#TypeError|TypeError]])并返回 <code>NULL</code></p></dd></dl>
assumes that no further objects will be read from the file, allowing it to
 
aggressively load file data into memory so that the de-serialization can
 
operate from data in memory rather than reading a byte at a time from the
 
file. Only use these variant if you are certain that you won't be reading
 
anything else from the file.</p>
 
<p>On error, sets the appropriate exception ([[../../library/exceptions#EOFError|<code>EOFError</code>]], [[../../library/exceptions#ValueError|<code>ValueError</code>]]
 
or [[../../library/exceptions#TypeError|<code>TypeError</code>]]) and returns <code>NULL</code>.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyMarshal_ReadObjectFromString</code><span class="sig-paren">(</span>''const'' char *''data'', Py_ssize_t ''len''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyMarshal_ReadObjectFromString</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">data</span></span>, <span class="n"><span class="pre">Py_ssize_t</span></span><span class="w"> </span><span class="n"><span class="pre">len</span></span><span class="sig-paren">)</span><br />
<dd><p>''Return value: New reference.''</p>
+
</dt>
<p>Return a Python object from the data stream in a byte buffer
+
<dd><p>从包含 ''data'' 指向的 ''len'' 字节的字节缓冲区中的数据流返回 Python 对象。</p>
containing ''len'' bytes pointed to by ''data''.</p>
+
<p>出错时,设置适当的异常([[../../library/exceptions#EOFError|EOFError]][[../../library/exceptions#ValueError|ValueError]] [[../../library/exceptions#TypeError|TypeError]])并返回 <code>NULL</code></p></dd></dl>
<p>On error, sets the appropriate exception ([[../../library/exceptions#EOFError|<code>EOFError</code>]], [[../../library/exceptions#ValueError|<code>ValueError</code>]]
+
 
or [[../../library/exceptions#TypeError|<code>TypeError</code>]]) and returns <code>NULL</code>.</p></dd></dl>
+
 
 +
</div>
 +
<div class="clearer">
 +
 
  
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:50的最新版本

数据编组支持

这些例程允许 C 代码使用与 marshal 模块相同的数据格式处理序列化对象。 有将数据写入序列化格式的函数,以及可用于读回数据的附加函数。 用于存储编组数据的文件必须以二进制模式打开。

数值首先以最低有效字节存储。

该模块支持两种版本的数据格式:版本 0 是历史版本,版本 1 共享文件中的内部字符串,以及解组时。 版本 2 对浮点数使用二进制格式。 Py_MARSHAL_VERSION 表示当前文件格式(当前为 2)。

void PyMarshal_WriteLongToFile(long value, FILE *file, int version)
long 整数 编组到 文件 。 这只会写入 value 的最低有效 32 位; 无论原生 long 类型的大小如何。 version 表示文件格式。
void PyMarshal_WriteObjectToFile(PyObject *value, FILE *file, int version)
将 Python 对象 value 编组到 fileversion 表示文件格式。
PyObject *PyMarshal_WriteObjectToString(PyObject *value, int version)
返回包含 的编组表示的字节对象。 version 表示文件格式。

以下函数允许读回编组的值。

long PyMarshal_ReadLongFromFile(FILE *file)

从打开以供读取的 FILE* 中的数据流中返回 C long。 无论 long 的本机大小如何,使用此函数只能读取 32 位值。

出错时,设置适当的异常 (EOFError) 并返回 -1

int PyMarshal_ReadShortFromFile(FILE *file)

从打开以供读取的 FILE* 中的数据流中返回 C short。 无论 short 的本机大小如何,使用此函数只能读取 16 位值。

出错时,设置适当的异常 (EOFError) 并返回 -1

PyObject *PyMarshal_ReadObjectFromFile(FILE *file)

从打开以供读取的 FILE* 中的数据流中返回 Python 对象。

出错时,设置适当的异常(EOFErrorValueErrorTypeError)并返回 NULL

PyObject *PyMarshal_ReadLastObjectFromFile(FILE *file)

从打开以供读取的 FILE* 中的数据流中返回 Python 对象。 与 PyMarshal_ReadObjectFromFile() 不同,此函数假定不会从文件中读取更多对象,从而允许它主动将文件数据加载到内存中,以便反序列化可以从内存中的数据进行操作,而不是读取从文件中一次一个字节。 仅当您确定不会从文件中读取任何其他内容时才使用这些变体。

出错时,设置适当的异常(EOFErrorValueErrorTypeError)并返回 NULL

PyObject *PyMarshal_ReadObjectFromString(const char *data, Py_ssize_t len)

从包含 data 指向的 len 字节的字节缓冲区中的数据流返回 Python 对象。

出错时,设置适当的异常(EOFErrorValueErrorTypeError)并返回 NULL