On this page
marshal
Prev: mailbox | Up: stdlib_module | Next: math
Rel: pickle, copyreg, shelve, dbm, sqlite3
Ref: docs.python.org — marshal
Tags: #public #python3 #stdlib
Import
import marshal
Overview
CPython's internal binary serializer (dumps(), loads(), dump(), load()) used exclusively for reading and writing .pyc bytecode files (code objects and basic built-in primitives). Never use marshal for general application persistence or data exchange—use json, sqlite3, or pickle instead.
Quick Start
import marshal
data = {"version": 1, "flags": (True, False), "items": [10, 20, 30]}
blob = marshal.dumps(data)
assert marshal.loads(blob) == data
Implementation Walkthrough
1. Why marshal Exists Alongside pickle
Unlike pickle, marshal can serialize Python code objects (types.CodeType, produced by compile()) directly, which is how .pyc bytecode caches work, and it runs faster for simple literals—at the cost of zero cross-version format stability.
Gotchas & Boundaries
- Not secure on untrusted data: Malformed
marshalbytes can crash the CPython interpreter or load untrusted code objects; never callmarshal.loads()on untrusted input. - No cross-version compatibility: The binary format changes between Python versions (
marshal.version), and custom classes or recursive structures are not generally supported.
Common Functions
- marshal.dump() —
dump(value, file, version=5, /, *, allow_code=True)— Write the value on the open file. - marshal.dumps() —
dumps(value, version=5, /, *, allow_code=True)— Return the bytes object that would be written to a file by dump(value, file). - marshal.load() —
load(file, /, *, allow_code=True)— Read one value from the open file and return it. - marshal.loads() —
loads(bytes, /, *, allow_code=True)— Convert the bytes-like object to a value.
Constants & Attributes
marshal.version(int) =5
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/bz2
- tuts/python3/stdlib_module/compression
- tuts/python3/stdlib_module/copyreg
- tuts/python3/stdlib_module/dbm
- tuts/python3/stdlib_module/gzip
- tuts/python3/stdlib_module/lzma
- tuts/python3/stdlib_module/mailbox
- tuts/python3/stdlib_module/marshal/dump
- tuts/python3/stdlib_module/marshal/dumps
- tuts/python3/stdlib_module/marshal/load
- tuts/python3/stdlib_module/marshal/loads
- tuts/python3/stdlib_module/math
- tuts/python3/stdlib_module/pickle
- tuts/python3/stdlib_module/shelve
- tuts/python3/stdlib_module/sqlite3
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/tarfile
- tuts/python3/stdlib_module/zipfile
- tuts/python3/stdlib_module/zlib
Related notes
- tuts/python3/stdlib_module/copyreg Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/dbm Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/mailbox Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/marshal/dump Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/marshal/dumps Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/marshal/load Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/marshal/loads Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/math Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pickle Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/shelve Linked here; links to this page; shared tags: #stdlib