@@ -456,23 +456,6 @@ def __new__(metacls, cls, bases, classdict, *, boundary=None, _simple=False, **k
456
456
classdict ['_all_bits_' ] = 2 ** ((flag_mask ).bit_length ()) - 1
457
457
classdict ['_inverted_' ] = None
458
458
#
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
- #
476
459
# create a default docstring if one has not been provided
477
460
if '__doc__' not in classdict :
478
461
classdict ['__doc__' ] = 'An enumeration.'
@@ -792,7 +775,7 @@ def _convert_(cls, name, module, filter, source=None, *, boundary=None):
792
775
body ['__module__' ] = module
793
776
tmp_cls = type (name , (object , ), body )
794
777
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
796
779
global_enum (cls )
797
780
module_globals [name ] = cls
798
781
return cls
@@ -1030,7 +1013,7 @@ def __hash__(self):
1030
1013
return hash (self ._name_ )
1031
1014
1032
1015
def __reduce_ex__ (self , proto ):
1033
- return self . __class__ , (self ._value_ , )
1016
+ return getattr , (self .__class__ , self . _name_ )
1034
1017
1035
1018
# enum.property is used to provide access to the `name` and
1036
1019
# `value` attributes of enum members while keeping some measure of
@@ -1091,7 +1074,7 @@ def _generate_next_value_(name, start, count, last_values):
1091
1074
return name .lower ()
1092
1075
1093
1076
1094
- def _reduce_ex_by_name (self , proto ):
1077
+ def _reduce_ex_by_global_name (self , proto ):
1095
1078
return self .name
1096
1079
1097
1080
class FlagBoundary (StrEnum ):
@@ -1795,6 +1778,6 @@ def _old_convert_(etype, name, module, filter, source=None, *, boundary=None):
1795
1778
# unless some values aren't comparable, in which case sort by name
1796
1779
members .sort (key = lambda t : t [0 ])
1797
1780
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
1799
1782
cls .__repr__ = global_enum_repr
1800
1783
return cls
0 commit comments