marshal
--- 内部 Python 物件序列化¶
此 module(模組)包含一個能以二進位制格式來讀寫 Python 值的函式。這種格式是 Python 專屬但獨立於機器架構的(例如,你可以在一臺 PC 上寫入某個 Python 值,再將檔案傳到一臺 Mac 上並在那裡讀取它)。這種格式的細節是有意地不在文件上說明的;它可能在不同 Python 版本中被改變(雖然這種情況極少發生)。[1]
This is not a general "persistence" module. For general persistence and
transfer of Python objects through RPC calls, see the modules pickle
and
shelve
. The marshal
module exists mainly to support reading and
writing the "pseudo-compiled" code for Python modules of .pyc
files.
Therefore, the Python maintainers reserve the right to modify the marshal format
in backward incompatible ways should the need arise.
The format of code objects is not compatible between Python versions,
even if the version of the format is the same.
De-serializing a code object in the incorrect Python version has undefined behavior.
If you're serializing and
de-serializing Python objects, use the pickle
module instead -- the
performance is comparable, version independence is guaranteed, and pickle
supports a substantially wider range of objects than marshal.
警告
marshal
module 對於錯誤或惡意構建的資料來說是不安全的。永遠不要 unmarshal 來自不受信任的或來源未經驗證的資料。
有些函式可以讀/寫檔案,還有些函式可以操作類位元組串物件 (bytes-like object)。
Not all Python object types are supported; in general, only objects whose value is independent from a particular invocation of Python can be written and read by this module. The following types are supported:
Strings (
str
) andbytes
. Bytes-like objects likebytearray
are marshalled asbytes
.Containers:
tuple
,list
,set
,frozenset
, and (sinceversion
5),slice
. It should be understood that these are supported only if the values contained therein are themselves supported. Recursive containers are supported sinceversion
3.The singletons
None
,Ellipsis
andStopIteration
.code
objects, if allow_code is true. See note above about version dependence.
在 3.4 版的變更:
Added format version 3, which supports marshalling recursive lists, sets and dictionaries.
Added format version 4, which supports efficient representations of short strings.
在 3.14 版的變更: Added format version 5, which allows marshalling slices.
這個 module 定義了以下函式:
- marshal.dump(value, file, version=version, /, *, allow_code=True)¶
將值寫入被開啟的檔案。值必須為受支援的型別,檔案必須為可寫入的 binary file。
如果值具有(或其所包含的物件具有)不支援的型別,則會引發
ValueError
例外 --- 但是垃圾資料 (garbage data) 也將寫入檔案,物件也無法正確地透過load()
重新讀取。程式碼物件只有在 allow_code 為 true 時才會支援。version 引數指明
dump
應該使用的資料格式(見下文)。引發一個附帶引數
value
與version
的稽核事件 (auditing event)marshal.dumps
。在 3.13 版的變更: 新增 allow_code 參數。
- marshal.load(file, /, *, allow_code=True)¶
從開啟的檔案讀取一個值並回傳。如果讀不到有效的值(例如,由於資料為不同 Python 版本的不相容 marshal 格式),則會引發
EOFError
、ValueError
或TypeError
。程式碼物件只有在 allow_code 為 true 時才會支援。檔案必須為可讀取的 binary file。引發一個沒有附帶引數的稽核事件
marshal.load
。在 3.10 版的變更: 使用此呼叫為每個程式碼物件引發一個
code.__new__
稽核事件。現在它會為整個載入操作引發單個marshal.load
事件。在 3.13 版的變更: 新增 allow_code 參數。
- marshal.dumps(value, version=version, /, *, allow_code=True)¶
回傳將透過
dump(value, file)
來被寫入一個檔案的位元組串物件,其值必須是有支援的型別,如果值(或其包含的任一物件)為不支援的型別則會引發ValueError
。程式碼物件只有在 allow_code 為 true 時才會支援。version 引數指明
dumps
應當使用的資料型別(見下文)。引發一個附帶引數
value
與version
的稽核事件 (auditing event)marshal.dumps
。在 3.13 版的變更: 新增 allow_code 參數。
- marshal.loads(bytes, /, *, allow_code=True)¶
將 bytes-like object 轉換為一個值。如果找不到有效的值,則會引發
EOFError
、ValueError
或TypeError
。程式碼物件只有在 allow_code 為 true 時才會支援。輸入中額外的位元組串會被忽略。引發一個附帶引數
bytes
的稽核事件marshal.loads
。在 3.10 版的變更: 使用此呼叫為每個程式碼物件引發一個
code.__new__
稽核事件。現在它會為整個載入操作引發單個marshal.loads
事件。在 3.13 版的變更: 新增 allow_code 參數。
此外,還定義了以下常數:
- marshal.version¶
Indicates the format that the module uses. Version 0 is the historical first version; subsequent versions add new features. Generally, a new version becomes the default when it is introduced.
Version
Available since
New features
1
Python 2.4
Sharing interned strings
2
Python 2.5
Binary representation of floats
3
Python 3.4
Support for object instancing and recursion
4
Python 3.4
Efficient representation of short strings
5
Python 3.14
Support for
slice
objects
註解