diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2022-06-26 07:54:00 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-26 07:54:00 (GMT) |
commit | 536985814a7116f14c9bc90aa1b3e3d36d5b2367 (patch) | |
tree | 9efa22e13a0b78f6d477d94de36ffe612eedeb48 /Lib/enum.py | |
parent | c834c025695f598a0df3aba980257326a088044a (diff) | |
download | cpython-536985814a7116f14c9bc90aa1b3e3d36d5b2367.zip cpython-536985814a7116f14c9bc90aa1b3e3d36d5b2367.tar.gz cpython-536985814a7116f14c9bc90aa1b3e3d36d5b2367.tar.bz2 |
gh-93820: Pickle enum.Flag by name (GH-93891)
Diffstat (limited to 'Lib/enum.py')
-rw-r--r-- | Lib/enum.py | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/Lib/enum.py b/Lib/enum.py index 15c8d88..69216c9 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -1273,7 +1273,21 @@ class Flag(Enum, boundary=STRICT): """ def __reduce_ex__(self, proto): - return self.__class__, (self._value_, ) + cls = self.__class__ + unknown = self._value_ & ~cls._flag_mask_ + member_value = self._value_ & cls._flag_mask_ + if unknown and member_value: + return _or_, (cls(member_value), unknown) + for val in _iter_bits_lsb(member_value): + rest = member_value & ~val + if rest: + return _or_, (cls(rest), cls._value2member_map_.get(val)) + else: + break + if self._name_ is None: + return cls, (self._value_,) + else: + return getattr, (cls, self._name_) _numeric_repr_ = repr |