@@ -115,14 +115,21 @@ def __new__(metacls, cls, bases, classdict):
115115 # Reverse value->name map for hashable values.
116116 enum_class ._value2member_map_ = {}
117117
118- # check for a supported pickle protocols, and if not present sabotage
119- # pickling, since it won't work anyway.
120- # if new class implements its own __reduce_ex__, do not sabotage
121- if classdict .get ('__reduce_ex__' ) is None :
118+ # If a custom type is mixed into the Enum, and it does not know how
119+ # to pickle itself, pickle.dumps will succeed but pickle.loads will
120+ # fail. Rather than have the error show up later and possibly far
121+ # from the source, sabotage the pickle protocol for this class so
122+ # that pickle.dumps also fails.
123+ #
124+ # However, if the new class implements its own __reduce_ex__, do not
125+ # sabotage -- it's on them to make sure it works correctly. We use
126+ # __reduce_ex__ instead of any of the others as it is preferred by
127+ # pickle over __reduce__, and it handles all pickle protocols.
128+ if '__reduce_ex__' not in classdict :
122129 if member_type is not object :
123130 methods = ('__getnewargs_ex__' , '__getnewargs__' ,
124131 '__reduce_ex__' , '__reduce__' )
125- if not any (map ( member_type .__dict__ . get , methods ) ):
132+ if not any (m in member_type .__dict__ for m in methods ):
126133 _make_class_unpicklable (enum_class )
127134
128135 # instantiate them, checking for duplicates as we go
@@ -193,14 +200,22 @@ def __call__(cls, value, names=None, *, module=None, qualname=None, type=None):
193200 to an enumeration member (i.e. Color(3)) and for the functional API
194201 (i.e. Color = Enum('Color', names='red green blue')).
195202
196- When used for the functional API: `module`, if set, will be stored in
197- the new class' __module__ attribute; `qualname`, if set, will be stored
198- in the new class' __qualname__ attribute; `type`, if set, will be mixed
199- in as the first base class.
203+ When used for the functional API:
200204
201- Note: if `module` is not set this routine will attempt to discover the
202- calling module by walking the frame stack; if this is unsuccessful
203- the resulting class will not be pickleable.
205+ `value` will be the name of the new class.
206+
207+ `names` should be either a string of white-space/comma delimited names
208+ (values will start at 1), or an iterator/mapping of name, value pairs.
209+
210+ `module` should be set to the module this class is being created in;
211+ if it is not set, an attempt to find that module will be made, but if
212+ it fails the class will not be picklable.
213+
214+ `qualname` should be set to the actual location this class can be found
215+ at in its module; by default it is set to the global scope. If this is
216+ not correct, unpickling will fail in some circumstances.
217+
218+ `type`, if set, will be mixed in as the first base class.
204219
205220 """
206221 if names is None : # simple value lookup
0 commit comments