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

For example:

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

    ~Flag.MASK is Flag(0)
This commit is contained in:
Ethan Furman 2023-07-11 04:35:54 -07:00 committed by GitHub
parent af5cf1e751
commit 95b7426f45
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 86 additions and 61 deletions

View file

@ -1515,14 +1515,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__