Skip to content

Commit

Permalink
pythongh-93820: Pickle enum.Flag by name
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka committed Jun 16, 2022
1 parent 05b32c1 commit 0814335
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
11 changes: 10 additions & 1 deletion Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,16 @@ class Flag(Enum, boundary=STRICT):
"""

def __reduce_ex__(self, proto):
return self.__class__, (self._value_, )
for m in self:
rest = self._value_ & ~m._value_
if rest:
return _or_, (m, self.__class__(rest))
else:
break
if self._name_ is None:
return self.__class__, (self._value_,)
else:
return getattr, (self.__class__, self._name_)

_numeric_repr_ = repr

Expand Down
44 changes: 42 additions & 2 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,27 @@ class FloatStooges(float, Enum):
class FlagStooges(Flag):
LARRY = 1
CURLY = 2
MOE = 3
MOE = 4
except Exception as exc:
FlagStooges = exc

class FlagStoogesWithZero(Flag):
NOFLAG = 0
LARRY = 1
CURLY = 2
MOE = 4

class IntFlagStooges(IntFlag):
LARRY = 1
CURLY = 2
MOE = 4

class IntFlagStoogesWithZero(IntFlag):
NOFLAG = 0
LARRY = 1
CURLY = 2
MOE = 4

# for pickle test and subclass tests
class Name(StrEnum):
BDFL = 'Guido van Rossum'
Expand Down Expand Up @@ -2956,9 +2973,32 @@ def test_programatic_function_from_dict(self):
def test_pickle(self):
if isinstance(FlagStooges, Exception):
raise FlagStooges
test_pickle_dump_load(self.assertIs, FlagStooges.CURLY|FlagStooges.MOE)
test_pickle_dump_load(self.assertIs, FlagStooges.CURLY)
test_pickle_dump_load(self.assertEqual,
FlagStooges.CURLY|FlagStooges.MOE)
test_pickle_dump_load(self.assertEqual,
FlagStooges.CURLY&~FlagStooges.CURLY)
test_pickle_dump_load(self.assertIs, FlagStooges)

test_pickle_dump_load(self.assertIs, FlagStoogesWithZero.CURLY)
test_pickle_dump_load(self.assertEqual,
FlagStoogesWithZero.CURLY|FlagStoogesWithZero.MOE)
test_pickle_dump_load(self.assertIs, FlagStoogesWithZero.NOFLAG)

test_pickle_dump_load(self.assertIs, IntFlagStooges.CURLY)
test_pickle_dump_load(self.assertEqual,
IntFlagStooges.CURLY|IntFlagStooges.MOE)
test_pickle_dump_load(self.assertEqual,
IntFlagStooges.CURLY|IntFlagStooges.MOE|0x30)
test_pickle_dump_load(self.assertEqual, IntFlagStooges(0))
test_pickle_dump_load(self.assertEqual, IntFlagStooges(0x30))
test_pickle_dump_load(self.assertIs, IntFlagStooges)

test_pickle_dump_load(self.assertIs, IntFlagStoogesWithZero.CURLY)
test_pickle_dump_load(self.assertEqual,
IntFlagStoogesWithZero.CURLY|IntFlagStoogesWithZero.MOE)
test_pickle_dump_load(self.assertIs, IntFlagStoogesWithZero.NOFLAG)

@unittest.skipIf(
python_version >= (3, 12),
'__contains__ now returns True/False for all inputs',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pickle :class:`enum.Flag` by name.

0 comments on commit 0814335

Please sign in to comment.