Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.11] gh-105497: [Enum] Fix Flag inversion when alias/mask members exist. (GH-105542) #105571

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1446,12 +1446,11 @@ def _missing_(cls, value):
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):
Expand Down Expand Up @@ -1527,8 +1526,8 @@ def __invert__(self):
# 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_
Expand Down
39 changes: 39 additions & 0 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2913,6 +2913,33 @@ class Color(Flag):
WHITE = RED|GREEN|BLUE
BLANCO = RED|GREEN|BLUE

class Complete(Flag):
A = 0x01
B = 0x02

class Partial(Flag):
A = 0x01
B = 0x02
MASK = 0xff

class CompleteInt(IntFlag):
A = 0x01
B = 0x02

class PartialInt(IntFlag):
A = 0x01
B = 0x02
MASK = 0xff

class CompleteIntStrict(IntFlag, boundary=STRICT):
A = 0x01
B = 0x02

class PartialIntStrict(IntFlag, boundary=STRICT):
A = 0x01
B = 0x02
MASK = 0xff

def test_or(self):
Perm = self.Perm
for i in Perm:
Expand Down Expand Up @@ -2971,6 +2998,18 @@ def test_invert(self):
Open = self.Open
self.assertIs(Open.WO & ~Open.WO, Open.RO)
self.assertIs((Open.WO|Open.CE) & ~Open.WO, Open.CE)
Complete = self.Complete
self.assertIs(~Complete.A, Complete.B)
Partial = self.Partial
self.assertIs(~Partial.A, Partial.B)
CompleteInt = self.CompleteInt
self.assertIs(~CompleteInt.A, CompleteInt.B)
PartialInt = self.PartialInt
self.assertIs(~PartialInt.A, PartialInt(254))
CompleteIntStrict = self.CompleteIntStrict
self.assertIs(~CompleteIntStrict.A, CompleteIntStrict.B)
PartialIntStrict = self.PartialIntStrict
self.assertIs(~PartialIntStrict.A, PartialIntStrict.B)

def test_bool(self):
Perm = self.Perm
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix flag inversion when alias/mask members exist.