On this page
copyreg
Prev: copy | Up: stdlib_module | Next: csv
Rel: pickle, shelve, marshal, dbm, sqlite3
Ref: docs.python.org — copyreg
Tags: #public #python3 #stdlib
Import
import copyreg
Overview
Registry for custom pickle and copy reduction hooks (copyreg.pickle(), copyreg.constructor(), copyreg.add_extension()). Use copyreg when you need to make a third-party class or C extension type picklable without modifying its class definition (i.e., when you can't add __reduce__ directly to the class).
Quick Start
import copyreg
import pickle
class Point:
def __init__(self, x: int, y: int) -> None:
self.x, self.y = x, y
def pickle_point(p: Point):
return (Point, (p.x, p.y))
copyreg.pickle(Point, pickle_point)
restored = pickle.loads(pickle.dumps(Point(3, 4)))
assert (restored.x, restored.y) == (3, 4)
Implementation Walkthrough
1. When to Use copyreg vs. __reduce__
- Define
__reduce__or__getstate__/__setstate__directly on classes you own. - Register with
copyreg.pickle(cls, reducer_fn)only for external types or C types you cannot subclass or edit.
Gotchas & Boundaries
- Global dispatch table:
copyreg.pickle()mutates the process-widecopyreg.dispatch_table; avoid registering conflicting reducers for built-in or shared third-party types inside libraries. - Security: Like pickle,
copyregconstructors run during unpickling—never unpickle untrusted data.
Common Functions
- copyreg.add_extension() —
add_extension(module, name, code)— Register an extension code. - copyreg.clear_extension_cache() —
clear_extension_cache()—copyreg.clear_extension_cache()function. - copyreg.constructor() —
constructor(object)—copyreg.constructor()function. - copyreg.pickle() —
pickle(ob_type, pickle_function, constructor_ob=None)—copyreg.pickle()function. - copyreg.remove_extension() —
remove_extension(module, name, code)— Unregister an extension code. For testing only.
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/bz2
- tuts/python3/stdlib_module/compression
- tuts/python3/stdlib_module/copy
- tuts/python3/stdlib_module/copyreg/add_extension
- tuts/python3/stdlib_module/copyreg/clear_extension_cache
- tuts/python3/stdlib_module/copyreg/constructor
- tuts/python3/stdlib_module/copyreg/pickle
- tuts/python3/stdlib_module/copyreg/remove_extension
- tuts/python3/stdlib_module/csv
- tuts/python3/stdlib_module/dbm
- tuts/python3/stdlib_module/gzip
- tuts/python3/stdlib_module/lzma
- tuts/python3/stdlib_module/marshal
- 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/copy Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/copyreg/add_extension Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/copyreg/clear_extension_cache Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/copyreg/constructor Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/copyreg/pickle Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/copyreg/remove_extension Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/csv 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/marshal Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pickle Linked here; links to this page; shared tags: #stdlib