Skip to content

Commit 0814335

Browse files
pythongh-93820: Pickle enum.Flag by name
1 parent 05b32c1 commit 0814335

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

Lib/enum.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,16 @@ class Flag(Enum, boundary=STRICT):
13701370
"""
13711371

13721372
def __reduce_ex__(self, proto):
1373-
return self.__class__, (self._value_, )
1373+
for m in self:
1374+
rest = self._value_ & ~m._value_
1375+
if rest:
1376+
return _or_, (m, self.__class__(rest))
1377+
else:
1378+
break
1379+
if self._name_ is None:
1380+
return self.__class__, (self._value_,)
1381+
else:
1382+
return getattr, (self.__class__, self._name_)
13741383

13751384
_numeric_repr_ = repr
13761385

Lib/test/test_enum.py

+42-2
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,27 @@ class FloatStooges(float, Enum):
6565
class FlagStooges(Flag):
6666
LARRY = 1
6767
CURLY = 2
68-
MOE = 3
68+
MOE = 4
6969
except Exception as exc:
7070
FlagStooges = exc
7171

72+
class FlagStoogesWithZero(Flag):
73+
NOFLAG = 0
74+
LARRY = 1
75+
CURLY = 2
76+
MOE = 4
77+
78+
class IntFlagStooges(IntFlag):
79+
LARRY = 1
80+
CURLY = 2
81+
MOE = 4
82+
83+
class IntFlagStoogesWithZero(IntFlag):
84+
NOFLAG = 0
85+
LARRY = 1
86+
CURLY = 2
87+
MOE = 4
88+
7289
# for pickle test and subclass tests
7390
class Name(StrEnum):
7491
BDFL = 'Guido van Rossum'
@@ -2956,9 +2973,32 @@ def test_programatic_function_from_dict(self):
29562973
def test_pickle(self):
29572974
if isinstance(FlagStooges, Exception):
29582975
raise FlagStooges
2959-
test_pickle_dump_load(self.assertIs, FlagStooges.CURLY|FlagStooges.MOE)
2976+
test_pickle_dump_load(self.assertIs, FlagStooges.CURLY)
2977+
test_pickle_dump_load(self.assertEqual,
2978+
FlagStooges.CURLY|FlagStooges.MOE)
2979+
test_pickle_dump_load(self.assertEqual,
2980+
FlagStooges.CURLY&~FlagStooges.CURLY)
29602981
test_pickle_dump_load(self.assertIs, FlagStooges)
29612982

2983+
test_pickle_dump_load(self.assertIs, FlagStoogesWithZero.CURLY)
2984+
test_pickle_dump_load(self.assertEqual,
2985+
FlagStoogesWithZero.CURLY|FlagStoogesWithZero.MOE)
2986+
test_pickle_dump_load(self.assertIs, FlagStoogesWithZero.NOFLAG)
2987+
2988+
test_pickle_dump_load(self.assertIs, IntFlagStooges.CURLY)
2989+
test_pickle_dump_load(self.assertEqual,
2990+
IntFlagStooges.CURLY|IntFlagStooges.MOE)
2991+
test_pickle_dump_load(self.assertEqual,
2992+
IntFlagStooges.CURLY|IntFlagStooges.MOE|0x30)
2993+
test_pickle_dump_load(self.assertEqual, IntFlagStooges(0))
2994+
test_pickle_dump_load(self.assertEqual, IntFlagStooges(0x30))
2995+
test_pickle_dump_load(self.assertIs, IntFlagStooges)
2996+
2997+
test_pickle_dump_load(self.assertIs, IntFlagStoogesWithZero.CURLY)
2998+
test_pickle_dump_load(self.assertEqual,
2999+
IntFlagStoogesWithZero.CURLY|IntFlagStoogesWithZero.MOE)
3000+
test_pickle_dump_load(self.assertIs, IntFlagStoogesWithZero.NOFLAG)
3001+
29623002
@unittest.skipIf(
29633003
python_version >= (3, 12),
29643004
'__contains__ now returns True/False for all inputs',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Pickle :class:`enum.Flag` by name.

0 commit comments

Comments
 (0)