Mercurial > cpython-lru_cache-weakref
annotate Lib/copy_reg.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | f419b2e96a98 |
| children |
| rev | line source |
|---|---|
|
15159
e71971a402d3
In the module docstring, clarify that this is used to register pickle
Fred Drake <fdrake@acm.org>
parents:
11691
diff
changeset
|
1 """Helper to provide extensibility for pickle/cPickle. |
|
e71971a402d3
In the module docstring, clarify that this is used to register pickle
Fred Drake <fdrake@acm.org>
parents:
11691
diff
changeset
|
2 |
|
e71971a402d3
In the module docstring, clarify that this is used to register pickle
Fred Drake <fdrake@acm.org>
parents:
11691
diff
changeset
|
3 This is only useful to add pickle support for extension types defined in |
|
e71971a402d3
In the module docstring, clarify that this is used to register pickle
Fred Drake <fdrake@acm.org>
parents:
11691
diff
changeset
|
4 C, not for instances of user-defined classes. |
|
e71971a402d3
In the module docstring, clarify that this is used to register pickle
Fred Drake <fdrake@acm.org>
parents:
11691
diff
changeset
|
5 """ |
|
e71971a402d3
In the module docstring, clarify that this is used to register pickle
Fred Drake <fdrake@acm.org>
parents:
11691
diff
changeset
|
6 |
|
e71971a402d3
In the module docstring, clarify that this is used to register pickle
Fred Drake <fdrake@acm.org>
parents:
11691
diff
changeset
|
7 from types import ClassType as _ClassType |
|
5558
ef3ba8b99da2
Added some minimal comments and tweaked lay-out a bit.
Guido van Rossum <guido@python.org>
parents:
5282
diff
changeset
|
8 |
|
27563
ab143aed49b9
Add extension management to __all__.
Guido van Rossum <guido@python.org>
parents:
27511
diff
changeset
|
9 __all__ = ["pickle", "constructor", |
|
ab143aed49b9
Add extension management to __all__.
Guido van Rossum <guido@python.org>
parents:
27511
diff
changeset
|
10 "add_extension", "remove_extension", "clear_extension_cache"] |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15159
diff
changeset
|
11 |
| 5282 | 12 dispatch_table = {} |
| 13 | |
|
15159
e71971a402d3
In the module docstring, clarify that this is used to register pickle
Fred Drake <fdrake@acm.org>
parents:
11691
diff
changeset
|
14 def pickle(ob_type, pickle_function, constructor_ob=None): |
|
e71971a402d3
In the module docstring, clarify that this is used to register pickle
Fred Drake <fdrake@acm.org>
parents:
11691
diff
changeset
|
15 if type(ob_type) is _ClassType: |
|
47189
f419b2e96a98
Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents:
47108
diff
changeset
|
16 raise TypeError("copy_reg is not intended for use with classes") |
|
15159
e71971a402d3
In the module docstring, clarify that this is used to register pickle
Fred Drake <fdrake@acm.org>
parents:
11691
diff
changeset
|
17 |
|
45413
8a187f562959
Removed uses of dict.has_key() from distutils, and uses of
Guido van Rossum <guido@python.org>
parents:
41362
diff
changeset
|
18 if not hasattr(pickle_function, '__call__'): |
|
15159
e71971a402d3
In the module docstring, clarify that this is used to register pickle
Fred Drake <fdrake@acm.org>
parents:
11691
diff
changeset
|
19 raise TypeError("reduction functions must be callable") |
| 5282 | 20 dispatch_table[ob_type] = pickle_function |
| 21 | |
|
29608
be620007ec1f
Revert previous checkin and just add a comment about constructor_ob.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29599
diff
changeset
|
22 # The constructor_ob function is a vestige of safe for unpickling. |
|
be620007ec1f
Revert previous checkin and just add a comment about constructor_ob.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29599
diff
changeset
|
23 # There is no reason for the caller to pass it anymore. |
|
be620007ec1f
Revert previous checkin and just add a comment about constructor_ob.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29599
diff
changeset
|
24 if constructor_ob is not None: |
|
be620007ec1f
Revert previous checkin and just add a comment about constructor_ob.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29599
diff
changeset
|
25 constructor(constructor_ob) |
|
be620007ec1f
Revert previous checkin and just add a comment about constructor_ob.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29599
diff
changeset
|
26 |
| 5282 | 27 def constructor(object): |
|
45413
8a187f562959
Removed uses of dict.has_key() from distutils, and uses of
Guido van Rossum <guido@python.org>
parents:
41362
diff
changeset
|
28 if not hasattr(object, '__call__'): |
|
15159
e71971a402d3
In the module docstring, clarify that this is used to register pickle
Fred Drake <fdrake@acm.org>
parents:
11691
diff
changeset
|
29 raise TypeError("constructors must be callable") |
| 5282 | 30 |
|
5558
ef3ba8b99da2
Added some minimal comments and tweaked lay-out a bit.
Guido van Rossum <guido@python.org>
parents:
5282
diff
changeset
|
31 # Example: provide pickling support for complex numbers. |
| 5282 | 32 |
|
29377
c055979300f1
Patch #750595: Refer to type complex using builtin. Fixes #595837.
Martin v. Löwis <martin@v.loewis.de>
parents:
28018
diff
changeset
|
33 try: |
|
c055979300f1
Patch #750595: Refer to type complex using builtin. Fixes #595837.
Martin v. Löwis <martin@v.loewis.de>
parents:
28018
diff
changeset
|
34 complex |
|
c055979300f1
Patch #750595: Refer to type complex using builtin. Fixes #595837.
Martin v. Löwis <martin@v.loewis.de>
parents:
28018
diff
changeset
|
35 except NameError: |
|
c055979300f1
Patch #750595: Refer to type complex using builtin. Fixes #595837.
Martin v. Löwis <martin@v.loewis.de>
parents:
28018
diff
changeset
|
36 pass |
|
c055979300f1
Patch #750595: Refer to type complex using builtin. Fixes #595837.
Martin v. Löwis <martin@v.loewis.de>
parents:
28018
diff
changeset
|
37 else: |
| 5282 | 38 |
|
29377
c055979300f1
Patch #750595: Refer to type complex using builtin. Fixes #595837.
Martin v. Löwis <martin@v.loewis.de>
parents:
28018
diff
changeset
|
39 def pickle_complex(c): |
|
c055979300f1
Patch #750595: Refer to type complex using builtin. Fixes #595837.
Martin v. Löwis <martin@v.loewis.de>
parents:
28018
diff
changeset
|
40 return complex, (c.real, c.imag) |
|
c055979300f1
Patch #750595: Refer to type complex using builtin. Fixes #595837.
Martin v. Löwis <martin@v.loewis.de>
parents:
28018
diff
changeset
|
41 |
|
c055979300f1
Patch #750595: Refer to type complex using builtin. Fixes #595837.
Martin v. Löwis <martin@v.loewis.de>
parents:
28018
diff
changeset
|
42 pickle(complex, pickle_complex, complex) |
|
20084
48e93ef3c5b7
- Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
43 |
|
27866
bbbba0ee61fb
SF patch #685738 by Michael Stone.
Guido van Rossum <guido@python.org>
parents:
27807
diff
changeset
|
44 # Support for pickling new-style objects |
|
20084
48e93ef3c5b7
- Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
45 |
|
48e93ef3c5b7
- Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
46 def _reconstructor(cls, base, state): |
|
27866
bbbba0ee61fb
SF patch #685738 by Michael Stone.
Guido van Rossum <guido@python.org>
parents:
27807
diff
changeset
|
47 if base is object: |
|
bbbba0ee61fb
SF patch #685738 by Michael Stone.
Guido van Rossum <guido@python.org>
parents:
27807
diff
changeset
|
48 obj = object.__new__(cls) |
|
bbbba0ee61fb
SF patch #685738 by Michael Stone.
Guido van Rossum <guido@python.org>
parents:
27807
diff
changeset
|
49 else: |
|
bbbba0ee61fb
SF patch #685738 by Michael Stone.
Guido van Rossum <guido@python.org>
parents:
27807
diff
changeset
|
50 obj = base.__new__(cls, state) |
|
41362
ef5d3e539384
Fix warnings about object.__init__() signature.
Guido van Rossum <guido@python.org>
parents:
37341
diff
changeset
|
51 if base.__init__ != object.__init__: |
|
ef5d3e539384
Fix warnings about object.__init__() signature.
Guido van Rossum <guido@python.org>
parents:
37341
diff
changeset
|
52 base.__init__(obj, state) |
|
20084
48e93ef3c5b7
- Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
53 return obj |
|
48e93ef3c5b7
- Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
54 |
|
48e93ef3c5b7
- Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
55 _HEAPTYPE = 1<<9 |
|
48e93ef3c5b7
- Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
56 |
|
28018
78c9c4a018cd
Remove _reduce_2, it's now implemented in C.
Guido van Rossum <guido@python.org>
parents:
27962
diff
changeset
|
57 # Python code for object.__reduce_ex__ for protocols 0 and 1 |
|
78c9c4a018cd
Remove _reduce_2, it's now implemented in C.
Guido van Rossum <guido@python.org>
parents:
27962
diff
changeset
|
58 |
|
78c9c4a018cd
Remove _reduce_2, it's now implemented in C.
Guido van Rossum <guido@python.org>
parents:
27962
diff
changeset
|
59 def _reduce_ex(self, proto): |
|
78c9c4a018cd
Remove _reduce_2, it's now implemented in C.
Guido van Rossum <guido@python.org>
parents:
27962
diff
changeset
|
60 assert proto < 2 |
|
20084
48e93ef3c5b7
- Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
61 for base in self.__class__.__mro__: |
| 21045 | 62 if hasattr(base, '__flags__') and not base.__flags__ & _HEAPTYPE: |
|
20084
48e93ef3c5b7
- Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
63 break |
|
48e93ef3c5b7
- Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
64 else: |
|
48e93ef3c5b7
- Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
65 base = object # not really reachable |
|
48e93ef3c5b7
- Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
66 if base is object: |
|
48e93ef3c5b7
- Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
67 state = None |
|
48e93ef3c5b7
- Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
68 else: |
|
21551
3b39ba2b800e
_reduce(): Avoid infinite recursion in the pickler when self.__class__
Guido van Rossum <guido@python.org>
parents:
21045
diff
changeset
|
69 if base is self.__class__: |
|
3b39ba2b800e
_reduce(): Avoid infinite recursion in the pickler when self.__class__
Guido van Rossum <guido@python.org>
parents:
21045
diff
changeset
|
70 raise TypeError, "can't pickle %s objects" % base.__name__ |
|
20084
48e93ef3c5b7
- Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
71 state = base(self) |
|
20152
72a5347d2f9a
Changes to copy() and deepcopy() in copy.py to support __reduce__ as a
Guido van Rossum <guido@python.org>
parents:
20090
diff
changeset
|
72 args = (self.__class__, base, state) |
|
72a5347d2f9a
Changes to copy() and deepcopy() in copy.py to support __reduce__ as a
Guido van Rossum <guido@python.org>
parents:
20090
diff
changeset
|
73 try: |
| 21045 | 74 getstate = self.__getstate__ |
|
20152
72a5347d2f9a
Changes to copy() and deepcopy() in copy.py to support __reduce__ as a
Guido van Rossum <guido@python.org>
parents:
20090
diff
changeset
|
75 except AttributeError: |
|
27807
b95b9abb9bf3
Get rid of the "bozo" __getstate__ that was inserted when __slots__
Guido van Rossum <guido@python.org>
parents:
27761
diff
changeset
|
76 if getattr(self, "__slots__", None): |
|
b95b9abb9bf3
Get rid of the "bozo" __getstate__ that was inserted when __slots__
Guido van Rossum <guido@python.org>
parents:
27761
diff
changeset
|
77 raise TypeError("a class that defines __slots__ without " |
|
b95b9abb9bf3
Get rid of the "bozo" __getstate__ that was inserted when __slots__
Guido van Rossum <guido@python.org>
parents:
27761
diff
changeset
|
78 "defining __getstate__ cannot be pickled") |
| 21045 | 79 try: |
| 80 dict = self.__dict__ | |
| 81 except AttributeError: | |
| 82 dict = None | |
| 83 else: | |
| 84 dict = getstate() | |
|
20152
72a5347d2f9a
Changes to copy() and deepcopy() in copy.py to support __reduce__ as a
Guido van Rossum <guido@python.org>
parents:
20090
diff
changeset
|
85 if dict: |
|
72a5347d2f9a
Changes to copy() and deepcopy() in copy.py to support __reduce__ as a
Guido van Rossum <guido@python.org>
parents:
20090
diff
changeset
|
86 return _reconstructor, args, dict |
|
72a5347d2f9a
Changes to copy() and deepcopy() in copy.py to support __reduce__ as a
Guido van Rossum <guido@python.org>
parents:
20090
diff
changeset
|
87 else: |
|
72a5347d2f9a
Changes to copy() and deepcopy() in copy.py to support __reduce__ as a
Guido van Rossum <guido@python.org>
parents:
20090
diff
changeset
|
88 return _reconstructor, args |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
89 |
|
28018
78c9c4a018cd
Remove _reduce_2, it's now implemented in C.
Guido van Rossum <guido@python.org>
parents:
27962
diff
changeset
|
90 # Helper for __reduce_ex__ protocol 2 |
|
27738
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
91 |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
92 def __newobj__(cls, *args): |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
93 return cls.__new__(cls, *args) |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
94 |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
95 def _slotnames(cls): |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
96 """Return a list of slot names for a given class. |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
97 |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
98 This needs to find slots defined by the class and its bases, so we |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
99 can't simply return the __slots__ attribute. We must walk down |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
100 the Method Resolution Order and concatenate the __slots__ of each |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
101 class found there. (This assumes classes don't modify their |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
102 __slots__ attribute to misrepresent their slots after the class is |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
103 defined.) |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
104 """ |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
105 |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
106 # Get the value from a cache in the class if possible |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
107 names = cls.__dict__.get("__slotnames__") |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
108 if names is not None: |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
109 return names |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
110 |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
111 # Not cached -- calculate the value |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
112 names = [] |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
113 if not hasattr(cls, "__slots__"): |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
114 # This class has no slots |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
115 pass |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
116 else: |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
117 # Slots found -- gather slot names from all base classes |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
118 for c in cls.__mro__: |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
119 if "__slots__" in c.__dict__: |
|
37341
f330048d8bdf
Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents:
29608
diff
changeset
|
120 slots = c.__dict__['__slots__'] |
|
f330048d8bdf
Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents:
29608
diff
changeset
|
121 # if class has a single slot, it can be given as a string |
|
f330048d8bdf
Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents:
29608
diff
changeset
|
122 if isinstance(slots, basestring): |
|
f330048d8bdf
Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents:
29608
diff
changeset
|
123 slots = (slots,) |
|
f330048d8bdf
Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents:
29608
diff
changeset
|
124 for name in slots: |
|
f330048d8bdf
Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents:
29608
diff
changeset
|
125 # special descriptors |
|
f330048d8bdf
Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents:
29608
diff
changeset
|
126 if name in ("__dict__", "__weakref__"): |
|
f330048d8bdf
Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents:
29608
diff
changeset
|
127 continue |
|
f330048d8bdf
Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents:
29608
diff
changeset
|
128 # mangled names |
|
f330048d8bdf
Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents:
29608
diff
changeset
|
129 elif name.startswith('__') and not name.endswith('__'): |
|
f330048d8bdf
Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents:
29608
diff
changeset
|
130 names.append('_%s%s' % (c.__name__, name)) |
|
f330048d8bdf
Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents:
29608
diff
changeset
|
131 else: |
|
f330048d8bdf
Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents:
29608
diff
changeset
|
132 names.append(name) |
|
27738
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
133 |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
134 # Cache the outcome in the class if at all possible |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
135 try: |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
136 cls.__slotnames__ = names |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
137 except: |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
138 pass # But don't die if we can't |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
139 |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
140 return names |
|
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27666
diff
changeset
|
141 |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
142 # A registry of extension codes. This is an ad-hoc compression |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
143 # mechanism. Whenever a global reference to <module>, <name> is about |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
144 # to be pickled, the (<module>, <name>) tuple is looked up here to see |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
145 # if it is a registered extension code for it. Extension codes are |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
146 # universal, so that the meaning of a pickle does not depend on |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
147 # context. (There are also some codes reserved for local use that |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
148 # don't have this restriction.) Codes are positive ints; 0 is |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
149 # reserved. |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
150 |
|
27665
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27662
diff
changeset
|
151 _extension_registry = {} # key -> code |
|
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27662
diff
changeset
|
152 _inverted_registry = {} # code -> key |
|
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27662
diff
changeset
|
153 _extension_cache = {} # code -> object |
|
27662
3a528aa222b6
Brought some module variables into synch with pickle.py's current values.
Tim Peters <tim.peters@gmail.com>
parents:
27573
diff
changeset
|
154 # Don't ever rebind those names: cPickle grabs a reference to them when |
|
3a528aa222b6
Brought some module variables into synch with pickle.py's current values.
Tim Peters <tim.peters@gmail.com>
parents:
27573
diff
changeset
|
155 # it's initialized, and won't see a rebinding. |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
156 |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
157 def add_extension(module, name, code): |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
158 """Register an extension code.""" |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
159 code = int(code) |
|
27666
5d7888fbea00
cPickle can load pickles using proto 2 EXT[124] now, but can't yet
Tim Peters <tim.peters@gmail.com>
parents:
27665
diff
changeset
|
160 if not 1 <= code <= 0x7fffffff: |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
161 raise ValueError, "code out of range" |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
162 key = (module, name) |
|
27665
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27662
diff
changeset
|
163 if (_extension_registry.get(key) == code and |
|
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27662
diff
changeset
|
164 _inverted_registry.get(code) == key): |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
165 return # Redundant registrations are benign |
|
27665
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27662
diff
changeset
|
166 if key in _extension_registry: |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
167 raise ValueError("key %s is already registered with code %s" % |
|
27665
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27662
diff
changeset
|
168 (key, _extension_registry[key])) |
|
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27662
diff
changeset
|
169 if code in _inverted_registry: |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
170 raise ValueError("code %s is already in use for key %s" % |
|
27665
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27662
diff
changeset
|
171 (code, _inverted_registry[code])) |
|
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27662
diff
changeset
|
172 _extension_registry[key] = code |
|
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27662
diff
changeset
|
173 _inverted_registry[code] = key |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
174 |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
175 def remove_extension(module, name, code): |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
176 """Unregister an extension code. For testing only.""" |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
177 key = (module, name) |
|
27665
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27662
diff
changeset
|
178 if (_extension_registry.get(key) != code or |
|
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27662
diff
changeset
|
179 _inverted_registry.get(code) != key): |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
180 raise ValueError("key %s is not registered with code %s" % |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
181 (key, code)) |
|
27665
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27662
diff
changeset
|
182 del _extension_registry[key] |
|
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27662
diff
changeset
|
183 del _inverted_registry[code] |
|
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27662
diff
changeset
|
184 if code in _extension_cache: |
|
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27662
diff
changeset
|
185 del _extension_cache[code] |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
186 |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
187 def clear_extension_cache(): |
|
27665
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27662
diff
changeset
|
188 _extension_cache.clear() |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
189 |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
190 # Standard extension code assignments |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
191 |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
192 # Reserved ranges |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
193 |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
194 # First Last Count Purpose |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
195 # 1 127 127 Reserved for Python standard library |
|
27761
a8a647e1927b
Reserve a range for Zope, not specifically for Zope 3.
Guido van Rossum <guido@python.org>
parents:
27738
diff
changeset
|
196 # 128 191 64 Reserved for Zope |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
197 # 192 239 48 Reserved for 3rd parties |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
198 # 240 255 16 Reserved for private use (will never be assigned) |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
199 # 256 Inf Inf Reserved for future assignment |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
200 |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
21551
diff
changeset
|
201 # Extension codes are assigned by the Python Software Foundation. |
