@@ -25,16 +25,16 @@ def constructor(object):
25
25
26
26
# Example: provide pickling support for complex numbers.
27
27
28
- try :
29
- complex
30
- except NameError :
31
- pass
32
- else :
28
+ def pickle_complex (c ):
29
+ return complex , (c .real , c .imag )
33
30
34
- def pickle_complex (c ):
35
- return complex , (c .real , c .imag )
31
+ pickle (complex , pickle_complex , complex )
36
32
37
- pickle (complex , pickle_complex , complex )
33
+ def pickle_union (obj ):
34
+ import functools , operator
35
+ return functools .reduce , (operator .or_ , obj .__args__ )
36
+
37
+ pickle (type (int | str ), pickle_union )
38
38
39
39
# Support for pickling new-style objects
40
40
@@ -48,6 +48,7 @@ def _reconstructor(cls, base, state):
48
48
return obj
49
49
50
50
_HEAPTYPE = 1 << 9
51
+ _new_type = type (int .__new__ )
51
52
52
53
# Python code for object.__reduce_ex__ for protocols 0 and 1
53
54
@@ -57,6 +58,9 @@ def _reduce_ex(self, proto):
57
58
for base in cls .__mro__ :
58
59
if hasattr (base , '__flags__' ) and not base .__flags__ & _HEAPTYPE :
59
60
break
61
+ new = base .__new__
62
+ if isinstance (new , _new_type ) and new .__self__ is base :
63
+ break
60
64
else :
61
65
base = object # not really reachable
62
66
if base is object :
@@ -79,6 +83,10 @@ def _reduce_ex(self, proto):
79
83
except AttributeError :
80
84
dict = None
81
85
else :
86
+ if (type (self ).__getstate__ is object .__getstate__ and
87
+ getattr (self , "__slots__" , None )):
88
+ raise TypeError ("a class that defines __slots__ without "
89
+ "defining __getstate__ cannot be pickled" )
82
90
dict = getstate ()
83
91
if dict :
84
92
return _reconstructor , args , dict
0 commit comments