データ整列化 (data marshalling) のサポート

以下のルーチン群は、 marshal モジュールと同じ形式を使った整列化オブジェクトを C コードから使えるようにします。整列化形式でデータを書き出す関数に加えて、データを読み戻す関数もあります。整列化されたデータを記録するファイルはバイナリモードで開かれていなければなりません。

数値は最小桁が先にくるように記録されます。

The module supports several versions of the data format; see the Python module documentation for details.

The following exceptions can be raised by these functions: ValueError if the value cannot be marshalled, ValueError or TypeError if the data is malformed, EOFError if the end of the data is reached before the value is complete, OSError if reading from or writing to a FILE* fails, KeyboardInterrupt if reading or writing is interrupted by a signal, and MemoryError if memory allocation fails.

バージョン 3.16.0a0 (unreleased) で変更: Previously, in functions taking a FILE*, the reading functions raised EOFError instead of OSError and KeyboardInterrupt, and the writing functions ignored I/O errors and interruptions.

Py_MARSHAL_VERSION

The current format version. See marshal.version.

void PyMarshal_WriteLongToFile(long value, FILE *file, int version)

long 型の整数値 valuefile へ整列化します。この関数は value の下桁 32 ビットを書き込むだけです; ネイティブの long 型サイズには関知しません。 version はファイルフォーマットを示します。

この関数は失敗することがあり、その場合はエラー指示子を設定します。それを確認するために PyErr_Occurred() を使います。

void PyMarshal_WriteObjectToFile(PyObject *value, FILE *file, int version)

Python オブジェクト valuefile へ整列化します。version はファイルフォーマットを示します。

この関数は失敗することがあり、その場合はエラー指示子を設定します。それを確認するために PyErr_Occurred() を使います。

PyObject *PyMarshal_WriteObjectToString(PyObject *value, int version)
戻り値: 新しい参照。

value の整列化表現が入ったバイト列オブジェクトを返します。version はファイルフォーマットを示します。

On error, raises an exception and returns NULL.

以下の関数を使うと、整列化された値を読み戻せます。

long PyMarshal_ReadLongFromFile(FILE *file)

読み出し用に開かれた FILE* 内のデータストリームから、 C の long 型データを読み出して返します。この関数は、ネイティブの long のサイズに関係なく、 32 ビットの値だけを読み出せます。

On error, raises an exception and returns -1.

int PyMarshal_ReadShortFromFile(FILE *file)

読み出し用に開かれた FILE* 内のデータストリームから、 C の short 型データを読み出して返します。この関数は、ネイティブの short のサイズに関係なく、 16 ビットの値だけを読み出せます。

On error, raises an exception and returns -1.

PyObject *PyMarshal_ReadObjectFromFile(FILE *file)
戻り値: 新しい参照。

読み出し用に開かれた FILE* 内のデータストリームから Python オブジェクトを返します。

On error, raises an exception and returns NULL.

PyObject *PyMarshal_ReadLastObjectFromFile(FILE *file)
戻り値: 新しい参照。

Return a Python object from the data stream in a FILE* opened for reading. Unlike PyMarshal_ReadObjectFromFile(), this function 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 this variant if you are certain that you won't be reading anything else from the file.

On error, raises an exception and returns NULL.

PyObject *PyMarshal_ReadObjectFromString(const char *data, Py_ssize_t len)
戻り値: 新しい参照。

data が指す len バイトのバイト列バッファ内のデータストリームから Python オブジェクトを返します。

On error, raises an exception and returns NULL.