@@ -456,23 +456,6 @@ def __new__(metacls, cls, bases, classdict, *, boundary=None, _simple=False, **k
456456 classdict ['_all_bits_' ] = 2 ** ((flag_mask ).bit_length ()) - 1
457457 classdict ['_inverted_' ] = None
458458 #
459- # If a custom type is mixed into the Enum, and it does not know how
460- # to pickle itself, pickle.dumps will succeed but pickle.loads will
461- # fail. Rather than have the error show up later and possibly far
462- # from the source, sabotage the pickle protocol for this class so
463- # that pickle.dumps also fails.
464- #
465- # However, if the new class implements its own __reduce_ex__, do not
466- # sabotage -- it's on them to make sure it works correctly. We use
467- # __reduce_ex__ instead of any of the others as it is preferred by
468- # pickle over __reduce__, and it handles all pickle protocols.
469- if '__reduce_ex__' not in classdict :
470- if member_type is not object :
471- methods = ('__getnewargs_ex__' , '__getnewargs__' ,
472- '__reduce_ex__' , '__reduce__' )
473- if not any (m in member_type .__dict__ for m in methods ):
474- _make_class_unpicklable (classdict )
475- #
476459 # create a default docstring if one has not been provided
477460 if '__doc__' not in classdict :
478461 classdict ['__doc__' ] = 'An enumeration.'
@@ -792,7 +775,7 @@ def _convert_(cls, name, module, filter, source=None, *, boundary=None):
792775 body ['__module__' ] = module
793776 tmp_cls = type (name , (object , ), body )
794777 cls = _simple_enum (etype = cls , boundary = boundary or KEEP )(tmp_cls )
795- cls .__reduce_ex__ = _reduce_ex_by_name
778+ cls .__reduce_ex__ = _reduce_ex_by_global_name
796779 global_enum (cls )
797780 module_globals [name ] = cls
798781 return cls
@@ -1030,7 +1013,7 @@ def __hash__(self):
10301013 return hash (self ._name_ )
10311014
10321015 def __reduce_ex__ (self , proto ):
1033- return self . __class__ , (self ._value_ , )
1016+ return getattr , (self .__class__ , self . _name_ )
10341017
10351018 # enum.property is used to provide access to the `name` and
10361019 # `value` attributes of enum members while keeping some measure of
@@ -1091,7 +1074,7 @@ def _generate_next_value_(name, start, count, last_values):
10911074 return name .lower ()
10921075
10931076
1094- def _reduce_ex_by_name (self , proto ):
1077+ def _reduce_ex_by_global_name (self , proto ):
10951078 return self .name
10961079
10971080class FlagBoundary (StrEnum ):
@@ -1795,6 +1778,6 @@ def _old_convert_(etype, name, module, filter, source=None, *, boundary=None):
17951778 # unless some values aren't comparable, in which case sort by name
17961779 members .sort (key = lambda t : t [0 ])
17971780 cls = etype (name , members , module = module , boundary = boundary or KEEP )
1798- cls .__reduce_ex__ = _reduce_ex_by_name
1781+ cls .__reduce_ex__ = _reduce_ex_by_global_name
17991782 cls .__repr__ = global_enum_repr
18001783 return cls
0 commit comments