marshal
— 내부 파이썬 객체 직렬화¶
이 모듈은 바이너리 형식으로 파이썬 값을 읽고 쓸 수 있는 함수를 포함합니다. 형식은 파이썬에만 국한되지만, 기계 아키텍처 문제에는 독립적입니다 (예를 들어, 파이썬 값을 PC의 파일에 기록하고 파일을 맥으로 보낸 다음, 그곳에서 다시 읽을 수 있습니다). 형식의 세부 사항은 의도적으로 설명하지 않습니다; 파이썬 버전 간에 변경될 수 있습니다 (드물긴 하지만). [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. 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
모듈은 잘못되었거나 악의적으로 구성된 데이터에 대해 보안성을 갖추려는 것이 아닙니다. 신뢰할 수 없거나 인증되지 않은 출처에서 받은 데이터를 역 마샬 하지 마십시오.
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: booleans, integers, floating-point
numbers, complex numbers, strings, bytes, bytearrays, tuples, lists, sets,
frozensets, dictionaries, and code objects, where it should be understood that
tuples, lists, sets, frozensets and dictionaries are only supported as long as
the values contained therein are themselves supported. The
singletons None
, Ellipsis
and StopIteration
can also be
marshalled and unmarshalled.
For format version lower than 3, recursive lists, sets and dictionaries cannot
be written (see below).
파일을 읽고 쓰는 함수는 물론 바이트열류 객체에서 작동하는 함수도 있습니다.
모듈은 다음 함수를 정의합니다:
- marshal.dump(value, file[, version])¶
열린 파일에 값을 기록합니다. value는 지원되는 형이어야 합니다. 파일은 쓰기 가능한 바이너리 파일 이어야 합니다.
If the value has (or contains an object that has) an unsupported type, a
ValueError
exception is raised — but garbage data will also be written to the file. The object will not be properly read back byload()
.version 인자는
dump
가 사용해야 하는 데이터 형식을 나타냅니다 (아래를 참조하십시오).Raises an auditing event
marshal.dumps
with argumentsvalue
,version
.
- marshal.load(file)¶
Read one value from the open file and return it. If no valid value is read (e.g. because the data has a different Python version’s incompatible marshal format), raise
EOFError
,ValueError
orTypeError
. The file must be a readable binary file.Raises an auditing event
marshal.load
with no arguments.버전 3.10에서 변경: This call used to raise a
code.__new__
audit event for each code object. Now it raises a singlemarshal.load
event for the entire load operation.
- marshal.dumps(value[, version])¶
Return the bytes object that would be written to a file by
dump(value, file)
. The value must be a supported type. Raise aValueError
exception if value has (or contains an object that has) an unsupported type.version 인자는
dumps
가 사용해야 하는 데이터 형식을 나타냅니다 (아래를 참조하십시오).Raises an auditing event
marshal.dumps
with argumentsvalue
,version
.
- marshal.loads(bytes)¶
Convert the bytes-like object to a value. If no valid value is found, raise
EOFError
,ValueError
orTypeError
. Extra bytes in the input are ignored.Raises an auditing event
marshal.loads
with argumentbytes
.버전 3.10에서 변경: This call used to raise a
code.__new__
audit event for each code object. Now it raises a singlemarshal.loads
event for the entire load operation.
또한, 다음 상수가 정의됩니다:
- marshal.version¶
모듈이 사용하는 형식을 나타냅니다. 버전 0은 역사적인 형식이고, 버전 1은 인턴 된 문자열을 공유하고, 버전 2는 부동 소수점 숫자에 바이너리 형식을 사용합니다. 버전 3에서는 객체 인스턴스 화와 재귀에 대한 지원이 추가되었습니다. 현재 버전은 4입니다.
각주