The CPython standard library provides a comprehensive suite of modules for data serialization and format handling. These range from Python-specific object serialization (pickle) to industry-standard interchange formats (json, xml, csv) and binary data manipulation (struct). Additionally, high-level utilities for archive management (zipfile, tarfile, shutil) allow for efficient data bundling and compression.
The pickle module implements binary protocols for serializing and de-serializing a Python object structure. For performance, CPython provides a highly optimized C implementation in _pickle.c.
The serialization process is managed by the Pickler class, while de-serialization is handled by Unpickler. The C implementation uses a stack-based virtual machine to process opcodes that represent object construction steps Modules/_pickle.c64-143
Key Components:
MARK, STOP, LIST, and DICT Modules/_pickle.c67-105PickleBuffer for out-of-band data transfers Lib/test/pickletester.py221-224The following diagram maps the high-level Python API to the underlying C implementation entities.
Pickle System Mapping
Sources: Modules/_pickle.c11-27 Modules/_pickle.c34-40 Modules/_pickle.c171-216
The json module provides an interface for encoding and decoding JSON (JavaScript Object Notation). It includes a C accelerator (_json.c) for the scanning and encoding phases.
PyScannerObject iterates through strings to find JSON tokens. It supports custom hooks like object_hook, object_pairs_hook, and parse_float Modules/_json.c31-40PyEncoderObject handles the transformation of Python objects into JSON strings, managing indent, key_separator, and sort_keys Modules/_json.c55-67JSON Internal Structure
| Entity | Role | Code Reference |
|---|---|---|
json_decoder | Global string literal for the decoder module | Include/internal/pycore_global_strings.h51 |
JSONDecodeError | Specialized exception for JSON parsing failures | Include/internal/pycore_global_strings.h78 |
scan_once_unicode | Core C function for recursive JSON scanning | Modules/_json.c91-92 |
encoder_listencode_obj | Recursive C function for encoding Python objects to JSON | Modules/_json.c110-111 |
Sources: Modules/_json.c31-81 Include/internal/pycore_global_strings.h51 Include/internal/pycore_global_strings.h78
The struct module performs conversions between Python values and C structs represented as Python bytes objects. It uses a format string as a compact description of the layout.
The module compiles format strings into internal structures. It uses specific C types to ensure cross-platform consistency of binary representations.
Key Implementation Structures:
formatdef: Defines the translation function, size, and alignment for each format character Modules/_struct.c47-55formatcode: Represents a compiled segment of a format string, including offset and repeat count Modules/_struct.c57-62PyStructObject: The core object representing a compiled format string Modules/_struct.c66-74Struct Code Mapping
Sources: Modules/_struct.c47-74 Modules/_struct.c11-13
CPython provides modules for manipulating various archive formats and performing data compression.
The zipfile module supports reading and writing ZIP files, implementing the PKWARE specification Lib/zipfile/__init__.py77-81
structEndArchive (End of Central Directory) Lib/zipfile/__init__.py84-86 and structFileHeader (Local File Header) Lib/zipfile/__init__.py156-159ZIP_STORED (0), ZIP_DEFLATED (8), ZIP_BZIP2 (12), ZIP_LZMA (14), and ZIP_ZSTANDARD (93) Lib/zipfile/__init__.py61-65The tarfile module handles TAR archives, supporting various formats including USTAR, GNU, and PAX Lib/tarfile.py103-106
GNU_MAGIC and POSIX_MAGIC Lib/tarfile.py78-79REGTYPE), links (LNKTYPE), directories (DIRTYPE), and extended headers (XHDTYPE) Lib/tarfile.py85-101zipfile and tarfile can utilize zlib, bz2, lzma, and zstd if available Lib/zipfile/__init__.py16-36 Lib/test/test_tarfile.py26-45zlib is unavailable Lib/zipfile/__init__.py16-21Sources: Lib/zipfile/__init__.py61-159 Lib/tarfile.py75-106 Lib/test/test_tarfile.py26-45 Lib/base64.py1-20
CPython manages a global set of strings and objects to optimize serialization and runtime performance.
To optimize memory and comparison speed for serialization keys (like JSON field names or XML tags), CPython interns many global strings during interpreter initialization Include/internal/pycore_unicodeobject_generated.h12-13
Common Interned Strings for Serialization:
json_decoder Include/internal/pycore_global_strings.h51utf_8 Include/internal/pycore_global_strings.h57__getstate__, __setstate__, __reduce__, __reduce_ex__ (used by pickle) Include/internal/pycore_global_strings.h147-194Small Integer Optimization: CPython maintains a cache of small integers (typically -5 to 256) which are frequently used as values in serialized data formats Include/internal/pycore_runtime_init_generated.h15-226
Sources: Include/internal/pycore_unicodeobject_generated.h12-163 Include/internal/pycore_global_strings.h51-194 Include/internal/pycore_runtime_init_generated.h15-226
Refresh this wiki