gh-93820: Pickle enum.Flag by name (GH-93891)

This commit is contained in:
Serhiy Storchaka 2022-06-26 10:54:00 +03:00 committed by GitHub
parent c834c02569
commit 536985814a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 3 deletions

View file

@ -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