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. 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.

경고

The marshal module is not intended to be secure against erroneous or maliciously constructed data. Never unmarshal data received from an untrusted or unauthenticated source.

파일을 읽고 쓰는 함수는 물론 바이트열류 객체에서 작동하는 함수도 있습니다.

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:

버전 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.

모듈은 다음 함수를 정의합니다:

marshal.dump(value, file, version=version, /, *, allow_code=True)

열린 파일에 값을 기록합니다. value는 지원되는 형이어야 합니다. 파일은 쓰기 가능한 바이너리 파일 이어야 합니다.

value가 지원되지 않는 형이면 (또는 지원되지 않는 형의 객체를 담고 있다면) ValueError 예외가 발생합니다 — 하지만, 찌꺼기 데이터도 파일에 기록됩니다. load()로 객체를 제대로 읽을 수 없습니다. 코드 객체allow_code가 참일 때만 지원됩니다.

version 인자는 dump가 사용해야 하는 데이터 형식을 나타냅니다 (아래를 참조하십시오).

Raises an auditing event marshal.dumps with arguments value, version.

버전 3.13에서 변경: Added the allow_code parameter.

marshal.load(file, /, *, allow_code=True)

열린 파일에서 하나의 값을 읽고 그것을 반환합니다. 유효한 값을 읽히지 않으면 (예를 들어, 데이터가 다른 파이썬 버전의 호환되지 않는 마샬 형식이라서) EOFError, ValueError 또는 TypeError를 발생시킵니다. 코드 객체allow_code가 참일 때만 지원됩니다. 파일은 읽을 수 있는 바이너리 파일 이어야 합니다.

Raises an auditing event marshal.load with no arguments.

참고

지원하지 않는 형을 포함하는 객체가 dump()로 마샬 되었으면, load()는 역 마샬이 불가능한 형을 None으로 치환합니다.

버전 3.10에서 변경: This call used to raise a code.__new__ audit event for each code object. Now it raises a single marshal.load event for the entire load operation.

버전 3.13에서 변경: Added the allow_code parameter.

marshal.dumps(value, version=version, /, *, allow_code=True)

dump(value, file)에 의해 파일에 기록될 바이트열 객체를 반환합니다. value는 지원되는 형이어야 합니다. value가 지원되지 않는 형이면 (또는 지원되지 않는 형의 객체를 담고 있다면) ValueError 예외를 발생시킵니다. 코드 객체allow_code가 참일 때만 지원됩니다.

version 인자는 dumps가 사용해야 하는 데이터 형식을 나타냅니다 (아래를 참조하십시오).

Raises an auditing event marshal.dumps with arguments value, version.

버전 3.13에서 변경: Added the allow_code parameter.

marshal.loads(bytes, /, *, allow_code=True)

바이트열류 객체를 값으로 변환합니다. 유효한 값이 없으면 EOFError, ValueError 또는 TypeError를 발생시킵니다. 코드 객체allow_code가 참일 때만 지원됩니다. 입력의 여분의 바이트는 무시됩니다.

Raises an auditing event marshal.loads with argument bytes.

버전 3.10에서 변경: This call used to raise a code.__new__ audit event for each code object. Now it raises a single marshal.loads event for the entire load operation.

버전 3.13에서 변경: Added the allow_code parameter.

또한, 다음 상수가 정의됩니다:

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

각주