mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
[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:
parent
2eb9fe92b4
commit
6968f9e4d3
3 changed files with 86 additions and 61 deletions
|
@ -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__
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue