[3.12] gh-105497: [Enum] Fix flag mask inversion when unnamed flags exist (GH-106468) (#106620)

gh-105497: [Enum] Fix flag mask inversion when unnamed flags exist (GH-106468)

For example:

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

    ~Flag.MASK is Flag(0)
(cherry picked from commit 95b7426f45)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
This commit is contained in:
Miss Islington (bot) 2023-07-11 05:09:41 -07:00 committed by GitHub
parent 2eb9fe92b4
commit 6968f9e4d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 86 additions and 61 deletions

View file

@ -1539,14 +1539,10 @@ class Flag(Enum, boundary=STRICT):
def __invert__(self):
if self._inverted_ is None:
if self._boundary_ is KEEP:
# use all bits
if self._boundary_ in (EJECT, KEEP):
self._inverted_ = self.__class__(~self._value_)
else:
# 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
self._inverted_ = self.__class__(self._singles_mask_ & ~self._value_)
return self._inverted_
__rand__ = __and__