diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-07-05 11:39:59 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-05 11:39:59 (GMT) |
commit | 74d84cf84d18e8cf69ffef0d7955f80fbc47220a (patch) | |
tree | 184ffe0cda35a537005535bc119c730c6efa216e /Lib/enum.py | |
parent | da672b2d245fb439f4ff895636bf284e352fa631 (diff) | |
download | cpython-74d84cf84d18e8cf69ffef0d7955f80fbc47220a.zip cpython-74d84cf84d18e8cf69ffef0d7955f80fbc47220a.tar.gz cpython-74d84cf84d18e8cf69ffef0d7955f80fbc47220a.tar.bz2 |
[3.12] gh-105497: [Enum] Fix Flag inversion when alias/mask members exist. (GH-105542) (#105572)
gh-105497: [Enum] Fix Flag inversion when alias/mask members exist. (GH-105542)
When inverting a Flag member (or boundary STRICT), only consider other canonical flags; when inverting an IntFlag member (or boundary KEEP), also consider aliases.
(cherry picked from commit 59f009e5898a006cdc8f5249be589de6edfe5cd0)
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
Diffstat (limited to 'Lib/enum.py')
-rw-r--r-- | Lib/enum.py | 15 |
1 files changed, 7 insertions, 8 deletions
diff --git a/Lib/enum.py b/Lib/enum.py index 62df304..92a9632 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -1463,12 +1463,11 @@ class Flag(Enum, boundary=STRICT): else: pseudo_member._name_ = None # use setdefault in case another thread already created a composite - # with this value, but only if all members are known - # note: zero is a special case -- add it - if not unknown: - pseudo_member = cls._value2member_map_.setdefault(value, pseudo_member) - if neg_value is not None: - cls._value2member_map_[neg_value] = pseudo_member + # with this value + # note: zero is a special case -- always add it + pseudo_member = cls._value2member_map_.setdefault(value, pseudo_member) + if neg_value is not None: + cls._value2member_map_[neg_value] = pseudo_member return pseudo_member def __contains__(self, other): @@ -1544,8 +1543,8 @@ class Flag(Enum, boundary=STRICT): # use all bits self._inverted_ = self.__class__(~self._value_) else: - # calculate flags not in this member - self._inverted_ = self.__class__(self._flag_mask_ ^ self._value_) + # use canonical bits (i.e. calculate flags not in this member) + self._inverted_ = self.__class__(self._singles_mask_ ^ self._value_) if isinstance(self._inverted_, self.__class__): self._inverted_._inverted_ = self return self._inverted_ |