blob: 40fca56d8029e9a9f45b6a36c730bc9a2c5e264f [file] [log] [blame]
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00001:mod:`copyreg` --- Register :mod:`pickle` support functions
Georg Brandl71515ca2009-05-17 12:29:12 +00002===========================================================
Georg Brandl116aa622007-08-15 14:28:22 +00003
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00004.. module:: copyreg
Georg Brandl116aa622007-08-15 14:28:22 +00005 :synopsis: Register pickle support functions.
6
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007**Source code:** :source:`Lib/copyreg.py`
Georg Brandl116aa622007-08-15 14:28:22 +00008
9.. index::
10 module: pickle
Georg Brandl116aa622007-08-15 14:28:22 +000011 module: copy
12
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040013--------------
14
Donald Stufft8b852f12014-05-20 12:58:38 -040015The :mod:`copyreg` module offers a way to define functions used while pickling
Ezio Melotti78b18d42012-11-08 11:04:57 +020016specific objects. The :mod:`pickle` and :mod:`copy` modules use those functions
17when pickling/copying those objects. The module provides configuration
18information about object constructors which are not classes.
Benjamin Petersonb5314c62009-12-13 21:04:16 +000019Such constructors may be factory functions or class instances.
Georg Brandl116aa622007-08-15 14:28:22 +000020
21
22.. function:: constructor(object)
23
24 Declares *object* to be a valid constructor. If *object* is not callable (and
25 hence not valid as a constructor), raises :exc:`TypeError`.
26
27
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000028.. function:: pickle(type, function, constructor=None)
Georg Brandl116aa622007-08-15 14:28:22 +000029
Georg Brandl23e8db52008-04-07 19:17:06 +000030 Declares that *function* should be used as a "reduction" function for objects
31 of type *type*. *function* should return either a string or a tuple
32 containing two or three elements.
Georg Brandl116aa622007-08-15 14:28:22 +000033
34 The optional *constructor* parameter, if provided, is a callable object which
35 can be used to reconstruct the object when called with the tuple of arguments
36 returned by *function* at pickling time. :exc:`TypeError` will be raised if
37 *object* is a class or *constructor* is not callable.
38
Antoine Pitrou8d3c2902012-03-04 18:31:48 +010039 See the :mod:`pickle` module for more details on the interface
40 expected of *function* and *constructor*. Note that the
41 :attr:`~pickle.Pickler.dispatch_table` attribute of a pickler
42 object or subclass of :class:`pickle.Pickler` can also be used for
43 declaring reduction functions.
Ezio Melotti78b18d42012-11-08 11:04:57 +020044
45Example
46-------
47
48The example below would like to show how to register a pickle function and how
49it will be used:
50
51 >>> import copyreg, copy, pickle
52 >>> class C(object):
53 ... def __init__(self, a):
54 ... self.a = a
55 ...
56 >>> def pickle_c(c):
57 ... print("pickling a C instance...")
58 ... return C, (c.a,)
59 ...
60 >>> copyreg.pickle(C, pickle_c)
61 >>> c = C(1)
Marco Buttub2a7c2f2017-03-02 12:02:43 +010062 >>> d = copy.copy(c) # doctest: +SKIP
Ezio Melotti78b18d42012-11-08 11:04:57 +020063 pickling a C instance...
Marco Buttub2a7c2f2017-03-02 12:02:43 +010064 >>> p = pickle.dumps(c) # doctest: +SKIP
Ezio Melotti78b18d42012-11-08 11:04:57 +020065 pickling a C instance...