2013-06-15 07:55:46 +08:00
|
|
|
import sys
|
2013-09-25 22:14:41 +08:00
|
|
|
from types import MappingProxyType, DynamicClassAttribute
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
from builtins import property as _bltin_property, bin as _bltin_bin
|
2013-06-15 07:55:46 +08:00
|
|
|
|
2015-09-18 13:03:52 +08:00
|
|
|
|
2016-09-11 14:36:59 +08:00
|
|
|
__all__ = [
|
2021-03-31 12:17:26 +08:00
|
|
|
'EnumType', 'EnumMeta',
|
2020-09-22 08:23:13 +08:00
|
|
|
'Enum', 'IntEnum', 'StrEnum', 'Flag', 'IntFlag',
|
2016-09-11 14:36:59 +08:00
|
|
|
'auto', 'unique',
|
2021-01-13 15:47:57 +08:00
|
|
|
'property',
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
'FlagBoundary', 'STRICT', 'CONFORM', 'EJECT', 'KEEP',
|
2021-03-31 12:17:26 +08:00
|
|
|
'global_flag_repr', 'global_enum_repr', 'global_enum',
|
2016-09-11 14:36:59 +08:00
|
|
|
]
|
2013-06-15 07:55:46 +08:00
|
|
|
|
|
|
|
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
# Dummy value for Enum and Flag as there are explicit checks for them
|
|
|
|
# before they have been created.
|
2021-03-31 12:17:26 +08:00
|
|
|
# This is also why there are checks in EnumType like `if Enum is not None`
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
Enum = Flag = EJECT = None
|
|
|
|
|
2013-09-16 03:34:36 +08:00
|
|
|
def _is_descriptor(obj):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Returns True if obj is a descriptor, False otherwise.
|
|
|
|
"""
|
2013-09-16 03:34:36 +08:00
|
|
|
return (
|
|
|
|
hasattr(obj, '__get__') or
|
|
|
|
hasattr(obj, '__set__') or
|
2020-12-09 04:26:56 +08:00
|
|
|
hasattr(obj, '__delete__')
|
|
|
|
)
|
2013-09-16 03:34:36 +08:00
|
|
|
|
2013-06-15 07:55:46 +08:00
|
|
|
def _is_dunder(name):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Returns True if a __dunder__ name, False otherwise.
|
|
|
|
"""
|
|
|
|
return (
|
|
|
|
len(name) > 4 and
|
2019-03-04 06:09:11 +08:00
|
|
|
name[:2] == name[-2:] == '__' and
|
|
|
|
name[2] != '_' and
|
2020-12-09 04:26:56 +08:00
|
|
|
name[-3] != '_'
|
|
|
|
)
|
2013-06-15 07:55:46 +08:00
|
|
|
|
|
|
|
def _is_sunder(name):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Returns True if a _sunder_ name, False otherwise.
|
|
|
|
"""
|
|
|
|
return (
|
|
|
|
len(name) > 2 and
|
2019-03-04 06:09:11 +08:00
|
|
|
name[0] == name[-1] == '_' and
|
2013-06-15 07:55:46 +08:00
|
|
|
name[1:2] != '_' and
|
2020-12-09 04:26:56 +08:00
|
|
|
name[-2:-1] != '_'
|
|
|
|
)
|
2013-06-15 07:55:46 +08:00
|
|
|
|
2020-12-10 09:12:11 +08:00
|
|
|
def _is_private(cls_name, name):
|
|
|
|
# do not use `re` as `re` imports `enum`
|
|
|
|
pattern = '_%s__' % (cls_name, )
|
2021-04-15 21:58:33 +08:00
|
|
|
pat_len = len(pattern)
|
2020-12-10 09:12:11 +08:00
|
|
|
if (
|
2021-04-15 21:58:33 +08:00
|
|
|
len(name) > pat_len
|
2020-12-10 09:12:11 +08:00
|
|
|
and name.startswith(pattern)
|
2021-04-15 21:58:33 +08:00
|
|
|
and name[pat_len:pat_len+1] != ['_']
|
2020-12-10 09:12:11 +08:00
|
|
|
and (name[-1] != '_' or name[-2] != '_')
|
|
|
|
):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
def _is_single_bit(num):
|
|
|
|
"""
|
|
|
|
True if only one bit set in num (should be an int)
|
|
|
|
"""
|
|
|
|
if num == 0:
|
|
|
|
return False
|
|
|
|
num &= num - 1
|
|
|
|
return num == 0
|
|
|
|
|
2021-01-13 15:47:57 +08:00
|
|
|
def _make_class_unpicklable(obj):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
2021-01-13 15:47:57 +08:00
|
|
|
Make the given obj un-picklable.
|
|
|
|
|
|
|
|
obj should be either a dictionary, on an Enum
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
2014-02-09 03:36:27 +08:00
|
|
|
def _break_on_call_reduce(self, proto):
|
2013-06-15 07:55:46 +08:00
|
|
|
raise TypeError('%r cannot be pickled' % self)
|
2021-01-13 15:47:57 +08:00
|
|
|
if isinstance(obj, dict):
|
|
|
|
obj['__reduce_ex__'] = _break_on_call_reduce
|
|
|
|
obj['__module__'] = '<unknown>'
|
|
|
|
else:
|
|
|
|
setattr(obj, '__reduce_ex__', _break_on_call_reduce)
|
|
|
|
setattr(obj, '__module__', '<unknown>')
|
2013-06-15 07:55:46 +08:00
|
|
|
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
def _iter_bits_lsb(num):
|
|
|
|
while num:
|
|
|
|
b = num & (~num + 1)
|
|
|
|
yield b
|
|
|
|
num ^= b
|
|
|
|
|
|
|
|
def bin(num, max_bits=None):
|
|
|
|
"""
|
|
|
|
Like built-in bin(), except negative values are represented in
|
|
|
|
twos-compliment, and the leading bit always indicates sign
|
|
|
|
(0=positive, 1=negative).
|
|
|
|
|
|
|
|
>>> bin(10)
|
|
|
|
'0b0 1010'
|
|
|
|
>>> bin(~10) # ~10 is -11
|
|
|
|
'0b1 0101'
|
|
|
|
"""
|
|
|
|
|
|
|
|
ceiling = 2 ** (num).bit_length()
|
|
|
|
if num >= 0:
|
|
|
|
s = _bltin_bin(num + ceiling).replace('1', '0', 1)
|
|
|
|
else:
|
|
|
|
s = _bltin_bin(~num ^ (ceiling - 1) + ceiling)
|
|
|
|
sign = s[:3]
|
|
|
|
digits = s[3:]
|
|
|
|
if max_bits is not None:
|
|
|
|
if len(digits) < max_bits:
|
|
|
|
digits = (sign[-1] * max_bits + digits)[-max_bits:]
|
|
|
|
return "%s %s" % (sign, digits)
|
|
|
|
|
|
|
|
|
2016-09-19 04:15:41 +08:00
|
|
|
_auto_null = object()
|
2016-09-11 14:36:59 +08:00
|
|
|
class auto:
|
|
|
|
"""
|
|
|
|
Instances are replaced with an appropriate value in Enum class suites.
|
|
|
|
"""
|
2016-09-19 04:15:41 +08:00
|
|
|
value = _auto_null
|
2016-09-11 14:36:59 +08:00
|
|
|
|
2021-01-13 15:47:57 +08:00
|
|
|
class property(DynamicClassAttribute):
|
|
|
|
"""
|
|
|
|
This is a descriptor, used to define attributes that act differently
|
|
|
|
when accessed through an enum member and through an enum class.
|
|
|
|
Instance access is the same as property(), but access to an attribute
|
|
|
|
through the enum class will instead look in the class' _member_map_ for
|
|
|
|
a corresponding enum member.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __get__(self, instance, ownerclass=None):
|
|
|
|
if instance is None:
|
|
|
|
try:
|
|
|
|
return ownerclass._member_map_[self.name]
|
|
|
|
except KeyError:
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
raise AttributeError(
|
2021-02-09 09:32:38 +08:00
|
|
|
'%s: no class attribute %r' % (ownerclass.__name__, self.name)
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
)
|
2021-01-13 15:47:57 +08:00
|
|
|
else:
|
|
|
|
if self.fget is None:
|
2021-02-09 09:32:38 +08:00
|
|
|
# check for member
|
|
|
|
if self.name in ownerclass._member_map_:
|
|
|
|
import warnings
|
|
|
|
warnings.warn(
|
|
|
|
"accessing one member from another is not supported, "
|
2021-03-04 01:54:30 +08:00
|
|
|
" and will be disabled in 3.12",
|
2021-02-09 09:32:38 +08:00
|
|
|
DeprecationWarning,
|
|
|
|
stacklevel=2,
|
|
|
|
)
|
|
|
|
return ownerclass._member_map_[self.name]
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
raise AttributeError(
|
2021-02-09 09:32:38 +08:00
|
|
|
'%s: no instance attribute %r' % (ownerclass.__name__, self.name)
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
)
|
2021-01-13 15:47:57 +08:00
|
|
|
else:
|
|
|
|
return self.fget(instance)
|
|
|
|
|
|
|
|
def __set__(self, instance, value):
|
|
|
|
if self.fset is None:
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
raise AttributeError(
|
2021-02-09 09:32:38 +08:00
|
|
|
"%s: cannot set instance attribute %r" % (self.clsname, self.name)
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
)
|
2021-01-13 15:47:57 +08:00
|
|
|
else:
|
|
|
|
return self.fset(instance, value)
|
|
|
|
|
|
|
|
def __delete__(self, instance):
|
|
|
|
if self.fdel is None:
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
raise AttributeError(
|
2021-02-09 09:32:38 +08:00
|
|
|
"%s: cannot delete instance attribute %r" % (self.clsname, self.name)
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
)
|
2021-01-13 15:47:57 +08:00
|
|
|
else:
|
|
|
|
return self.fdel(instance)
|
|
|
|
|
|
|
|
def __set_name__(self, ownerclass, name):
|
|
|
|
self.name = name
|
|
|
|
self.clsname = ownerclass.__name__
|
|
|
|
|
|
|
|
|
|
|
|
class _proto_member:
|
|
|
|
"""
|
|
|
|
intermediate step for enum members between class execution and final creation
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
def __set_name__(self, enum_class, member_name):
|
|
|
|
"""
|
|
|
|
convert each quasi-member into an instance of the new enum class
|
|
|
|
"""
|
|
|
|
# first step: remove ourself from enum_class
|
|
|
|
delattr(enum_class, member_name)
|
|
|
|
# second step: create member based on enum_class
|
|
|
|
value = self.value
|
|
|
|
if not isinstance(value, tuple):
|
|
|
|
args = (value, )
|
|
|
|
else:
|
|
|
|
args = value
|
|
|
|
if enum_class._member_type_ is tuple: # special case for tuple enums
|
|
|
|
args = (args, ) # wrap it one more time
|
|
|
|
if not enum_class._use_args_:
|
|
|
|
enum_member = enum_class._new_member_(enum_class)
|
|
|
|
if not hasattr(enum_member, '_value_'):
|
|
|
|
enum_member._value_ = value
|
|
|
|
else:
|
|
|
|
enum_member = enum_class._new_member_(enum_class, *args)
|
|
|
|
if not hasattr(enum_member, '_value_'):
|
|
|
|
if enum_class._member_type_ is object:
|
|
|
|
enum_member._value_ = value
|
|
|
|
else:
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
try:
|
|
|
|
enum_member._value_ = enum_class._member_type_(*args)
|
|
|
|
except Exception as exc:
|
|
|
|
raise TypeError(
|
|
|
|
'_value_ not set in __new__, unable to create it'
|
|
|
|
) from None
|
2021-01-13 15:47:57 +08:00
|
|
|
value = enum_member._value_
|
|
|
|
enum_member._name_ = member_name
|
|
|
|
enum_member.__objclass__ = enum_class
|
|
|
|
enum_member.__init__(*args)
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
enum_member._sort_order_ = len(enum_class._member_names_)
|
2021-01-13 15:47:57 +08:00
|
|
|
# If another member with the same value was already defined, the
|
|
|
|
# new member becomes an alias to the existing one.
|
|
|
|
for name, canonical_member in enum_class._member_map_.items():
|
|
|
|
if canonical_member._value_ == enum_member._value_:
|
|
|
|
enum_member = canonical_member
|
|
|
|
break
|
|
|
|
else:
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
# this could still be an alias if the value is multi-bit and the
|
|
|
|
# class is a flag class
|
|
|
|
if (
|
|
|
|
Flag is None
|
|
|
|
or not issubclass(enum_class, Flag)
|
|
|
|
):
|
|
|
|
# no other instances found, record this member in _member_names_
|
|
|
|
enum_class._member_names_.append(member_name)
|
|
|
|
elif (
|
|
|
|
Flag is not None
|
|
|
|
and issubclass(enum_class, Flag)
|
|
|
|
and _is_single_bit(value)
|
|
|
|
):
|
|
|
|
# no other instances found, record this member in _member_names_
|
|
|
|
enum_class._member_names_.append(member_name)
|
2021-01-13 15:47:57 +08:00
|
|
|
# get redirect in place before adding to _member_map_
|
|
|
|
# but check for other instances in parent classes first
|
|
|
|
need_override = False
|
|
|
|
descriptor = None
|
|
|
|
for base in enum_class.__mro__[1:]:
|
|
|
|
descriptor = base.__dict__.get(member_name)
|
|
|
|
if descriptor is not None:
|
|
|
|
if isinstance(descriptor, (property, DynamicClassAttribute)):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
need_override = True
|
|
|
|
# keep looking for an enum.property
|
|
|
|
if descriptor and not need_override:
|
|
|
|
# previous enum.property found, no further action needed
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
redirect = property()
|
|
|
|
redirect.__set_name__(enum_class, member_name)
|
|
|
|
if descriptor and need_override:
|
|
|
|
# previous enum.property found, but some other inherited attribute
|
|
|
|
# is in the way; copy fget, fset, fdel to this one
|
|
|
|
redirect.fget = descriptor.fget
|
|
|
|
redirect.fset = descriptor.fset
|
|
|
|
redirect.fdel = descriptor.fdel
|
|
|
|
setattr(enum_class, member_name, redirect)
|
|
|
|
# now add to _member_map_ (even aliases)
|
|
|
|
enum_class._member_map_[member_name] = enum_member
|
|
|
|
try:
|
|
|
|
# This may fail if value is not hashable. We can't add the value
|
|
|
|
# to the map, and by-value lookups for this value will be
|
|
|
|
# linear.
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
enum_class._value2member_map_.setdefault(value, enum_member)
|
2021-01-13 15:47:57 +08:00
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
2013-09-16 03:34:36 +08:00
|
|
|
|
2013-06-15 07:55:46 +08:00
|
|
|
class _EnumDict(dict):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Track enum member order and ensure member names are not reused.
|
2013-06-15 07:55:46 +08:00
|
|
|
|
2021-03-31 12:17:26 +08:00
|
|
|
EnumType will use the names found in self._member_names as the
|
2013-06-15 07:55:46 +08:00
|
|
|
enumeration member names.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self._member_names = []
|
2016-09-11 14:36:59 +08:00
|
|
|
self._last_values = []
|
2018-01-22 23:56:37 +08:00
|
|
|
self._ignore = []
|
2020-04-29 01:20:55 +08:00
|
|
|
self._auto_called = False
|
2013-06-15 07:55:46 +08:00
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Changes anything not dundered or not a descriptor.
|
2013-06-15 07:55:46 +08:00
|
|
|
|
|
|
|
If an enum member name is used twice, an error is raised; duplicate
|
|
|
|
values are not checked for.
|
|
|
|
|
|
|
|
Single underscore (sunder) names are reserved.
|
|
|
|
"""
|
2020-12-10 09:12:11 +08:00
|
|
|
if _is_private(self._cls_name, key):
|
|
|
|
# do nothing, name will be a normal attribute
|
|
|
|
pass
|
|
|
|
elif _is_sunder(key):
|
2016-08-31 15:12:15 +08:00
|
|
|
if key not in (
|
2021-04-15 21:49:54 +08:00
|
|
|
'_order_',
|
2018-01-22 23:56:37 +08:00
|
|
|
'_generate_next_value_', '_missing_', '_ignore_',
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
'_iter_member_', '_iter_member_by_value_', '_iter_member_by_def_',
|
2016-08-31 15:12:15 +08:00
|
|
|
):
|
2020-12-09 04:26:56 +08:00
|
|
|
raise ValueError(
|
|
|
|
'_sunder_ names, such as %r, are reserved for future Enum use'
|
|
|
|
% (key, )
|
|
|
|
)
|
2016-09-11 14:36:59 +08:00
|
|
|
if key == '_generate_next_value_':
|
2020-04-29 01:20:55 +08:00
|
|
|
# check if members already defined as auto()
|
|
|
|
if self._auto_called:
|
|
|
|
raise TypeError("_generate_next_value_ must be defined before members")
|
2021-03-31 12:17:26 +08:00
|
|
|
_gnv = value.__func__ if isinstance(value, staticmethod) else value
|
|
|
|
setattr(self, '_generate_next_value', _gnv)
|
2018-01-22 23:56:37 +08:00
|
|
|
elif key == '_ignore_':
|
|
|
|
if isinstance(value, str):
|
|
|
|
value = value.replace(',',' ').split()
|
|
|
|
else:
|
|
|
|
value = list(value)
|
|
|
|
self._ignore = value
|
|
|
|
already = set(value) & set(self._member_names)
|
|
|
|
if already:
|
2020-12-09 04:26:56 +08:00
|
|
|
raise ValueError(
|
|
|
|
'_ignore_ cannot specify already set names: %r'
|
|
|
|
% (already, )
|
|
|
|
)
|
2013-09-16 03:34:36 +08:00
|
|
|
elif _is_dunder(key):
|
2016-08-20 22:19:31 +08:00
|
|
|
if key == '__order__':
|
|
|
|
key = '_order_'
|
2013-09-16 03:34:36 +08:00
|
|
|
elif key in self._member_names:
|
|
|
|
# descriptor overwriting an enum?
|
2020-12-11 05:07:00 +08:00
|
|
|
raise TypeError('%r already defined as: %r' % (key, self[key]))
|
2018-01-22 23:56:37 +08:00
|
|
|
elif key in self._ignore:
|
|
|
|
pass
|
2013-09-16 03:34:36 +08:00
|
|
|
elif not _is_descriptor(value):
|
|
|
|
if key in self:
|
|
|
|
# enum overwriting a descriptor?
|
2016-08-31 15:12:15 +08:00
|
|
|
raise TypeError('%r already defined as: %r' % (key, self[key]))
|
2016-09-11 14:36:59 +08:00
|
|
|
if isinstance(value, auto):
|
2016-09-19 04:15:41 +08:00
|
|
|
if value.value == _auto_null:
|
2020-12-09 04:26:56 +08:00
|
|
|
value.value = self._generate_next_value(
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
key, 1, len(self._member_names), self._last_values[:],
|
2020-12-09 04:26:56 +08:00
|
|
|
)
|
2020-09-17 03:37:54 +08:00
|
|
|
self._auto_called = True
|
2016-09-19 04:15:41 +08:00
|
|
|
value = value.value
|
2013-06-15 07:55:46 +08:00
|
|
|
self._member_names.append(key)
|
2016-09-11 14:36:59 +08:00
|
|
|
self._last_values.append(value)
|
2013-06-15 07:55:46 +08:00
|
|
|
super().__setitem__(key, value)
|
|
|
|
|
2020-12-11 05:07:00 +08:00
|
|
|
def update(self, members, **more_members):
|
|
|
|
try:
|
|
|
|
for name in members.keys():
|
|
|
|
self[name] = members[name]
|
|
|
|
except AttributeError:
|
|
|
|
for name, value in members:
|
|
|
|
self[name] = value
|
|
|
|
for name, value in more_members.items():
|
|
|
|
self[name] = value
|
|
|
|
|
2013-06-15 07:55:46 +08:00
|
|
|
|
2021-03-31 12:17:26 +08:00
|
|
|
class EnumType(type):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Metaclass for Enum
|
|
|
|
"""
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
|
2013-06-15 07:55:46 +08:00
|
|
|
@classmethod
|
2020-12-25 02:05:02 +08:00
|
|
|
def __prepare__(metacls, cls, bases, **kwds):
|
2020-09-16 22:11:57 +08:00
|
|
|
# check that previous enum members do not exist
|
|
|
|
metacls._check_for_existing_members(cls, bases)
|
2016-08-31 15:12:15 +08:00
|
|
|
# create the namespace dict
|
|
|
|
enum_dict = _EnumDict()
|
2020-12-10 09:12:11 +08:00
|
|
|
enum_dict._cls_name = cls
|
2016-08-31 15:12:15 +08:00
|
|
|
# inherit previous flags and _generate_next_value_ function
|
2020-09-16 22:11:57 +08:00
|
|
|
member_type, first_enum = metacls._get_mixins_(cls, bases)
|
2016-08-31 15:12:15 +08:00
|
|
|
if first_enum is not None:
|
2020-12-09 04:26:56 +08:00
|
|
|
enum_dict['_generate_next_value_'] = getattr(
|
|
|
|
first_enum, '_generate_next_value_', None,
|
|
|
|
)
|
2016-08-31 15:12:15 +08:00
|
|
|
return enum_dict
|
2016-08-20 15:00:52 +08:00
|
|
|
|
2021-04-22 01:20:44 +08:00
|
|
|
def __new__(metacls, cls, bases, classdict, *, boundary=None, _simple=False, **kwds):
|
2013-06-15 07:55:46 +08:00
|
|
|
# an Enum class is final once enumeration items have been defined; it
|
|
|
|
# cannot be mixed with other types (int, float, etc.) if it has an
|
|
|
|
# inherited __new__ unless a new __new__ is defined (or the resulting
|
|
|
|
# class will fail).
|
2018-01-22 23:56:37 +08:00
|
|
|
#
|
|
|
|
# remove any keys listed in _ignore_
|
2021-04-22 01:20:44 +08:00
|
|
|
if _simple:
|
|
|
|
return super().__new__(metacls, cls, bases, classdict, **kwds)
|
2018-01-22 23:56:37 +08:00
|
|
|
classdict.setdefault('_ignore_', []).append('_ignore_')
|
|
|
|
ignore = classdict['_ignore_']
|
|
|
|
for key in ignore:
|
|
|
|
classdict.pop(key, None)
|
2021-01-13 15:47:57 +08:00
|
|
|
#
|
|
|
|
# grab member names
|
|
|
|
member_names = classdict._member_names
|
|
|
|
#
|
2013-06-15 07:55:46 +08:00
|
|
|
# check for illegal enum names (any others?)
|
2021-01-13 15:47:57 +08:00
|
|
|
invalid_names = set(member_names) & {'mro', ''}
|
2013-06-15 07:55:46 +08:00
|
|
|
if invalid_names:
|
|
|
|
raise ValueError('Invalid enum member name: {0}'.format(
|
|
|
|
','.join(invalid_names)))
|
2021-01-13 15:47:57 +08:00
|
|
|
#
|
|
|
|
# adjust the sunders
|
|
|
|
_order_ = classdict.pop('_order_', None)
|
|
|
|
# convert to normal dict
|
|
|
|
classdict = dict(classdict.items())
|
|
|
|
#
|
|
|
|
# data type of member and the controlling Enum class
|
|
|
|
member_type, first_enum = metacls._get_mixins_(cls, bases)
|
|
|
|
__new__, save_new, use_args = metacls._find_new_(
|
|
|
|
classdict, member_type, first_enum,
|
|
|
|
)
|
|
|
|
classdict['_new_member_'] = __new__
|
|
|
|
classdict['_use_args_'] = use_args
|
|
|
|
#
|
|
|
|
# convert future enum members into temporary _proto_members
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
# and record integer values in case this will be a Flag
|
|
|
|
flag_mask = 0
|
2021-01-13 15:47:57 +08:00
|
|
|
for name in member_names:
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
value = classdict[name]
|
|
|
|
if isinstance(value, int):
|
|
|
|
flag_mask |= value
|
|
|
|
classdict[name] = _proto_member(value)
|
2021-01-13 15:47:57 +08:00
|
|
|
#
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
# house-keeping structures
|
2021-01-13 15:47:57 +08:00
|
|
|
classdict['_member_names_'] = []
|
|
|
|
classdict['_member_map_'] = {}
|
|
|
|
classdict['_value2member_map_'] = {}
|
|
|
|
classdict['_member_type_'] = member_type
|
|
|
|
#
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
# Flag structures (will be removed if final class is not a Flag
|
|
|
|
classdict['_boundary_'] = (
|
|
|
|
boundary
|
|
|
|
or getattr(first_enum, '_boundary_', None)
|
|
|
|
)
|
|
|
|
classdict['_flag_mask_'] = flag_mask
|
|
|
|
classdict['_all_bits_'] = 2 ** ((flag_mask).bit_length()) - 1
|
|
|
|
classdict['_inverted_'] = None
|
|
|
|
#
|
2014-03-04 04:42:52 +08:00
|
|
|
# If a custom type is mixed into the Enum, and it does not know how
|
|
|
|
# to pickle itself, pickle.dumps will succeed but pickle.loads will
|
|
|
|
# fail. Rather than have the error show up later and possibly far
|
|
|
|
# from the source, sabotage the pickle protocol for this class so
|
|
|
|
# that pickle.dumps also fails.
|
|
|
|
#
|
|
|
|
# However, if the new class implements its own __reduce_ex__, do not
|
|
|
|
# sabotage -- it's on them to make sure it works correctly. We use
|
|
|
|
# __reduce_ex__ instead of any of the others as it is preferred by
|
|
|
|
# pickle over __reduce__, and it handles all pickle protocols.
|
|
|
|
if '__reduce_ex__' not in classdict:
|
2014-02-19 04:37:12 +08:00
|
|
|
if member_type is not object:
|
|
|
|
methods = ('__getnewargs_ex__', '__getnewargs__',
|
|
|
|
'__reduce_ex__', '__reduce__')
|
2014-03-04 04:42:52 +08:00
|
|
|
if not any(m in member_type.__dict__ for m in methods):
|
2021-01-13 15:47:57 +08:00
|
|
|
_make_class_unpicklable(classdict)
|
|
|
|
#
|
|
|
|
# create a default docstring if one has not been provided
|
|
|
|
if '__doc__' not in classdict:
|
|
|
|
classdict['__doc__'] = 'An enumeration.'
|
|
|
|
try:
|
|
|
|
exc = None
|
|
|
|
enum_class = super().__new__(metacls, cls, bases, classdict, **kwds)
|
|
|
|
except RuntimeError as e:
|
|
|
|
# any exceptions raised by member.__new__ will get converted to a
|
|
|
|
# RuntimeError, so get that original exception back and raise it instead
|
|
|
|
exc = e.__cause__ or e
|
|
|
|
if exc is not None:
|
|
|
|
raise exc
|
|
|
|
#
|
2013-06-15 07:55:46 +08:00
|
|
|
# double check that repr and friends are not the mixin's or various
|
|
|
|
# things break (such as pickle)
|
2020-09-16 07:28:25 +08:00
|
|
|
# however, if the method is defined in the Enum itself, don't replace
|
|
|
|
# it
|
2014-02-19 04:37:12 +08:00
|
|
|
for name in ('__repr__', '__str__', '__format__', '__reduce_ex__'):
|
2020-09-16 07:28:25 +08:00
|
|
|
if name in classdict:
|
|
|
|
continue
|
2013-06-15 07:55:46 +08:00
|
|
|
class_method = getattr(enum_class, name)
|
|
|
|
obj_method = getattr(member_type, name, None)
|
|
|
|
enum_method = getattr(first_enum, name, None)
|
|
|
|
if obj_method is not None and obj_method is class_method:
|
|
|
|
setattr(enum_class, name, enum_method)
|
2021-01-13 15:47:57 +08:00
|
|
|
#
|
2013-06-15 07:55:46 +08:00
|
|
|
# replace any other __new__ with our own (as long as Enum is not None,
|
|
|
|
# anyway) -- again, this is to support pickle
|
|
|
|
if Enum is not None:
|
|
|
|
# if the user defined their own __new__, save it before it gets
|
|
|
|
# clobbered in case they subclass later
|
|
|
|
if save_new:
|
|
|
|
enum_class.__new_member__ = __new__
|
|
|
|
enum_class.__new__ = Enum.__new__
|
2021-01-13 15:47:57 +08:00
|
|
|
#
|
2016-08-20 22:19:31 +08:00
|
|
|
# py3 support for definition order (helps keep py2/py3 code in sync)
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
#
|
|
|
|
# _order_ checking is spread out into three/four steps
|
|
|
|
# - if enum_class is a Flag:
|
|
|
|
# - remove any non-single-bit flags from _order_
|
|
|
|
# - remove any aliases from _order_
|
|
|
|
# - check that _order_ and _member_names_ match
|
|
|
|
#
|
|
|
|
# step 1: ensure we have a list
|
2016-08-20 22:19:31 +08:00
|
|
|
if _order_ is not None:
|
|
|
|
if isinstance(_order_, str):
|
|
|
|
_order_ = _order_.replace(',', ' ').split()
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
#
|
|
|
|
# remove Flag structures if final class is not a Flag
|
|
|
|
if (
|
|
|
|
Flag is None and cls != 'Flag'
|
|
|
|
or Flag is not None and not issubclass(enum_class, Flag)
|
|
|
|
):
|
|
|
|
delattr(enum_class, '_boundary_')
|
|
|
|
delattr(enum_class, '_flag_mask_')
|
|
|
|
delattr(enum_class, '_all_bits_')
|
|
|
|
delattr(enum_class, '_inverted_')
|
|
|
|
elif Flag is not None and issubclass(enum_class, Flag):
|
|
|
|
# ensure _all_bits_ is correct and there are no missing flags
|
|
|
|
single_bit_total = 0
|
|
|
|
multi_bit_total = 0
|
|
|
|
for flag in enum_class._member_map_.values():
|
|
|
|
flag_value = flag._value_
|
|
|
|
if _is_single_bit(flag_value):
|
|
|
|
single_bit_total |= flag_value
|
|
|
|
else:
|
|
|
|
# multi-bit flags are considered aliases
|
|
|
|
multi_bit_total |= flag_value
|
|
|
|
if enum_class._boundary_ is not KEEP:
|
|
|
|
missed = list(_iter_bits_lsb(multi_bit_total & ~single_bit_total))
|
|
|
|
if missed:
|
|
|
|
raise TypeError(
|
|
|
|
'invalid Flag %r -- missing values: %s'
|
|
|
|
% (cls, ', '.join((str(i) for i in missed)))
|
|
|
|
)
|
|
|
|
enum_class._flag_mask_ = single_bit_total
|
|
|
|
#
|
|
|
|
# set correct __iter__
|
|
|
|
member_list = [m._value_ for m in enum_class]
|
|
|
|
if member_list != sorted(member_list):
|
|
|
|
enum_class._iter_member_ = enum_class._iter_member_by_def_
|
|
|
|
if _order_:
|
|
|
|
# _order_ step 2: remove any items from _order_ that are not single-bit
|
|
|
|
_order_ = [
|
|
|
|
o
|
|
|
|
for o in _order_
|
|
|
|
if o not in enum_class._member_map_ or _is_single_bit(enum_class[o]._value_)
|
|
|
|
]
|
|
|
|
#
|
|
|
|
if _order_:
|
|
|
|
# _order_ step 3: remove aliases from _order_
|
|
|
|
_order_ = [
|
|
|
|
o
|
|
|
|
for o in _order_
|
|
|
|
if (
|
|
|
|
o not in enum_class._member_map_
|
|
|
|
or
|
|
|
|
(o in enum_class._member_map_ and o in enum_class._member_names_)
|
|
|
|
)]
|
|
|
|
# _order_ step 4: verify that _order_ and _member_names_ match
|
2016-08-20 22:19:31 +08:00
|
|
|
if _order_ != enum_class._member_names_:
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
raise TypeError(
|
|
|
|
'member order does not match _order_:\n%r\n%r'
|
|
|
|
% (enum_class._member_names_, _order_)
|
|
|
|
)
|
2021-01-13 15:47:57 +08:00
|
|
|
#
|
2013-06-15 07:55:46 +08:00
|
|
|
return enum_class
|
|
|
|
|
2016-04-14 14:52:09 +08:00
|
|
|
def __bool__(self):
|
|
|
|
"""
|
|
|
|
classes/types should always be True.
|
|
|
|
"""
|
|
|
|
return True
|
|
|
|
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
def __call__(cls, value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Either returns an existing member, or creates a new enum class.
|
2013-06-15 07:55:46 +08:00
|
|
|
|
|
|
|
This method is used both when an enum class is given a value to match
|
|
|
|
to an enumeration member (i.e. Color(3)) and for the functional API
|
2016-11-22 01:22:05 +08:00
|
|
|
(i.e. Color = Enum('Color', names='RED GREEN BLUE')).
|
2013-06-15 07:55:46 +08:00
|
|
|
|
2014-03-04 04:42:52 +08:00
|
|
|
When used for the functional API:
|
2013-06-15 07:55:46 +08:00
|
|
|
|
2014-03-04 04:42:52 +08:00
|
|
|
`value` will be the name of the new class.
|
|
|
|
|
|
|
|
`names` should be either a string of white-space/comma delimited names
|
2014-09-17 11:35:55 +08:00
|
|
|
(values will start at `start`), or an iterator/mapping of name, value pairs.
|
2014-03-04 04:42:52 +08:00
|
|
|
|
|
|
|
`module` should be set to the module this class is being created in;
|
|
|
|
if it is not set, an attempt to find that module will be made, but if
|
|
|
|
it fails the class will not be picklable.
|
|
|
|
|
|
|
|
`qualname` should be set to the actual location this class can be found
|
|
|
|
at in its module; by default it is set to the global scope. If this is
|
|
|
|
not correct, unpickling will fail in some circumstances.
|
|
|
|
|
|
|
|
`type`, if set, will be mixed in as the first base class.
|
2013-06-15 07:55:46 +08:00
|
|
|
"""
|
|
|
|
if names is None: # simple value lookup
|
|
|
|
return cls.__new__(cls, value)
|
|
|
|
# otherwise, functional API: we're creating a new Enum type
|
2020-12-09 04:26:56 +08:00
|
|
|
return cls._create_(
|
|
|
|
value,
|
|
|
|
names,
|
|
|
|
module=module,
|
|
|
|
qualname=qualname,
|
|
|
|
type=type,
|
|
|
|
start=start,
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
boundary=boundary,
|
2020-12-09 04:26:56 +08:00
|
|
|
)
|
2013-06-15 07:55:46 +08:00
|
|
|
|
|
|
|
def __contains__(cls, member):
|
2018-09-11 02:21:04 +08:00
|
|
|
if not isinstance(member, Enum):
|
|
|
|
raise TypeError(
|
|
|
|
"unsupported operand type(s) for 'in': '%s' and '%s'" % (
|
|
|
|
type(member).__qualname__, cls.__class__.__qualname__))
|
2014-09-17 08:31:23 +08:00
|
|
|
return isinstance(member, cls) and member._name_ in cls._member_map_
|
2013-06-15 07:55:46 +08:00
|
|
|
|
2013-09-23 07:18:19 +08:00
|
|
|
def __delattr__(cls, attr):
|
|
|
|
# nicer error message when someone tries to delete an attribute
|
|
|
|
# (see issue19025).
|
|
|
|
if attr in cls._member_map_:
|
2020-12-09 04:26:56 +08:00
|
|
|
raise AttributeError("%s: cannot delete Enum member %r." % (cls.__name__, attr))
|
2013-09-23 07:18:19 +08:00
|
|
|
super().__delattr__(attr)
|
|
|
|
|
2013-08-12 21:51:41 +08:00
|
|
|
def __dir__(self):
|
2020-12-09 04:26:56 +08:00
|
|
|
return (
|
|
|
|
['__class__', '__doc__', '__members__', '__module__']
|
|
|
|
+ self._member_names_
|
|
|
|
)
|
2013-08-12 21:51:41 +08:00
|
|
|
|
2013-06-15 07:55:46 +08:00
|
|
|
def __getattr__(cls, name):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Return the enum member matching `name`
|
2013-06-15 07:55:46 +08:00
|
|
|
|
|
|
|
We use __getattr__ instead of descriptors or inserting into the enum
|
|
|
|
class' __dict__ in order to support `name` and `value` being both
|
|
|
|
properties for enum members (which live in the class' __dict__) and
|
|
|
|
enum members themselves.
|
|
|
|
"""
|
|
|
|
if _is_dunder(name):
|
|
|
|
raise AttributeError(name)
|
|
|
|
try:
|
2013-07-20 10:47:21 +08:00
|
|
|
return cls._member_map_[name]
|
2013-06-15 07:55:46 +08:00
|
|
|
except KeyError:
|
|
|
|
raise AttributeError(name) from None
|
|
|
|
|
|
|
|
def __getitem__(cls, name):
|
2013-07-20 10:47:21 +08:00
|
|
|
return cls._member_map_[name]
|
2013-06-15 07:55:46 +08:00
|
|
|
|
|
|
|
def __iter__(cls):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Returns members in definition order.
|
|
|
|
"""
|
2013-07-20 10:47:21 +08:00
|
|
|
return (cls._member_map_[name] for name in cls._member_names_)
|
2013-06-15 07:55:46 +08:00
|
|
|
|
|
|
|
def __len__(cls):
|
2013-07-20 10:47:21 +08:00
|
|
|
return len(cls._member_names_)
|
2013-06-15 07:55:46 +08:00
|
|
|
|
2021-01-13 15:47:57 +08:00
|
|
|
@_bltin_property
|
2013-09-15 09:11:24 +08:00
|
|
|
def __members__(cls):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Returns a mapping of member name->value.
|
2013-09-15 09:11:24 +08:00
|
|
|
|
|
|
|
This mapping lists all enum members, including aliases. Note that this
|
|
|
|
is a read-only view of the internal mapping.
|
|
|
|
"""
|
|
|
|
return MappingProxyType(cls._member_map_)
|
|
|
|
|
2013-06-15 07:55:46 +08:00
|
|
|
def __repr__(cls):
|
|
|
|
return "<enum %r>" % cls.__name__
|
|
|
|
|
2013-09-15 09:11:24 +08:00
|
|
|
def __reversed__(cls):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Returns members in reverse definition order.
|
|
|
|
"""
|
2013-09-15 09:11:24 +08:00
|
|
|
return (cls._member_map_[name] for name in reversed(cls._member_names_))
|
|
|
|
|
2013-09-06 22:16:48 +08:00
|
|
|
def __setattr__(cls, name, value):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Block attempts to reassign Enum members.
|
2013-09-06 22:16:48 +08:00
|
|
|
|
|
|
|
A simple assignment to the class namespace only changes one of the
|
|
|
|
several possible ways to get an Enum member from the Enum class,
|
|
|
|
resulting in an inconsistent Enumeration.
|
|
|
|
"""
|
|
|
|
member_map = cls.__dict__.get('_member_map_', {})
|
|
|
|
if name in member_map:
|
2021-04-22 01:20:44 +08:00
|
|
|
raise AttributeError('Cannot reassign member %r.' % (name, ))
|
2013-09-06 22:16:48 +08:00
|
|
|
super().__setattr__(name, value)
|
|
|
|
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
def _create_(cls, class_name, names, *, module=None, qualname=None, type=None, start=1, boundary=None):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Convenience method to create a new Enum class.
|
2013-06-15 07:55:46 +08:00
|
|
|
|
|
|
|
`names` can be:
|
|
|
|
|
|
|
|
* A string containing member names, separated either with spaces or
|
2014-09-17 11:35:55 +08:00
|
|
|
commas. Values are incremented by 1 from `start`.
|
|
|
|
* An iterable of member names. Values are incremented by 1 from `start`.
|
2013-06-15 07:55:46 +08:00
|
|
|
* An iterable of (member name, value) pairs.
|
2014-09-17 11:35:55 +08:00
|
|
|
* A mapping of member name -> value pairs.
|
2013-06-15 07:55:46 +08:00
|
|
|
"""
|
|
|
|
metacls = cls.__class__
|
|
|
|
bases = (cls, ) if type is None else (type, cls)
|
2020-09-16 22:11:57 +08:00
|
|
|
_, first_enum = cls._get_mixins_(cls, bases)
|
2013-06-15 07:55:46 +08:00
|
|
|
classdict = metacls.__prepare__(class_name, bases)
|
|
|
|
|
|
|
|
# special processing needed for names?
|
|
|
|
if isinstance(names, str):
|
|
|
|
names = names.replace(',', ' ').split()
|
2017-06-22 00:52:32 +08:00
|
|
|
if isinstance(names, (tuple, list)) and names and isinstance(names[0], str):
|
2016-08-31 15:12:15 +08:00
|
|
|
original_names, names = names, []
|
2016-09-11 14:36:59 +08:00
|
|
|
last_values = []
|
2016-08-31 15:12:15 +08:00
|
|
|
for count, name in enumerate(original_names):
|
2016-09-11 14:36:59 +08:00
|
|
|
value = first_enum._generate_next_value_(name, start, count, last_values[:])
|
|
|
|
last_values.append(value)
|
|
|
|
names.append((name, value))
|
2013-06-15 07:55:46 +08:00
|
|
|
|
|
|
|
# Here, names is either an iterable of (name, value) or a mapping.
|
|
|
|
for item in names:
|
|
|
|
if isinstance(item, str):
|
|
|
|
member_name, member_value = item, names[item]
|
|
|
|
else:
|
|
|
|
member_name, member_value = item
|
|
|
|
classdict[member_name] = member_value
|
|
|
|
|
|
|
|
# TODO: replace the frame hack if a blessed way to know the calling
|
|
|
|
# module is ever developed
|
|
|
|
if module is None:
|
|
|
|
try:
|
|
|
|
module = sys._getframe(2).f_globals['__name__']
|
2019-11-20 05:34:03 +08:00
|
|
|
except (AttributeError, ValueError, KeyError):
|
2013-06-15 07:55:46 +08:00
|
|
|
pass
|
|
|
|
if module is None:
|
2021-01-13 15:47:57 +08:00
|
|
|
_make_class_unpicklable(classdict)
|
2013-06-15 07:55:46 +08:00
|
|
|
else:
|
2021-01-13 15:47:57 +08:00
|
|
|
classdict['__module__'] = module
|
2014-02-09 03:36:27 +08:00
|
|
|
if qualname is not None:
|
2021-01-13 15:47:57 +08:00
|
|
|
classdict['__qualname__'] = qualname
|
2013-06-15 07:55:46 +08:00
|
|
|
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
return metacls.__new__(metacls, class_name, bases, classdict, boundary=boundary)
|
2013-06-15 07:55:46 +08:00
|
|
|
|
2021-04-22 01:20:44 +08:00
|
|
|
def _convert_(cls, name, module, filter, source=None, *, boundary=None):
|
|
|
|
|
2018-09-13 01:28:53 +08:00
|
|
|
"""
|
|
|
|
Create a new Enum subclass that replaces a collection of global constants
|
|
|
|
"""
|
|
|
|
# convert all constants from source (or module) that pass filter() to
|
|
|
|
# a new Enum called name, and export the enum and its members back to
|
|
|
|
# module;
|
|
|
|
# also, replace the __reduce_ex__ method so unpickling works in
|
|
|
|
# previous Python versions
|
2021-03-31 12:17:26 +08:00
|
|
|
module_globals = sys.modules[module].__dict__
|
2018-09-13 01:28:53 +08:00
|
|
|
if source:
|
2021-03-31 12:17:26 +08:00
|
|
|
source = source.__dict__
|
2018-09-13 01:28:53 +08:00
|
|
|
else:
|
|
|
|
source = module_globals
|
|
|
|
# _value2member_map_ is populated in the same order every time
|
|
|
|
# for a consistent reverse mapping of number to name when there
|
|
|
|
# are multiple names for the same number.
|
|
|
|
members = [
|
|
|
|
(name, value)
|
|
|
|
for name, value in source.items()
|
|
|
|
if filter(name)]
|
|
|
|
try:
|
|
|
|
# sort by value
|
|
|
|
members.sort(key=lambda t: (t[1], t[0]))
|
|
|
|
except TypeError:
|
|
|
|
# unless some values aren't comparable, in which case sort by name
|
|
|
|
members.sort(key=lambda t: t[0])
|
2021-04-22 01:20:44 +08:00
|
|
|
body = {t[0]: t[1] for t in members}
|
|
|
|
body['__module__'] = module
|
|
|
|
tmp_cls = type(name, (object, ), body)
|
|
|
|
cls = _simple_enum(etype=cls, boundary=boundary or KEEP)(tmp_cls)
|
2018-09-13 01:28:53 +08:00
|
|
|
cls.__reduce_ex__ = _reduce_ex_by_name
|
2021-03-31 12:17:26 +08:00
|
|
|
global_enum(cls)
|
2018-09-13 01:28:53 +08:00
|
|
|
module_globals[name] = cls
|
|
|
|
return cls
|
|
|
|
|
2013-06-15 07:55:46 +08:00
|
|
|
@staticmethod
|
2020-09-16 22:11:57 +08:00
|
|
|
def _check_for_existing_members(class_name, bases):
|
|
|
|
for chain in bases:
|
|
|
|
for base in chain.__mro__:
|
|
|
|
if issubclass(base, Enum) and base._member_names_:
|
2020-12-09 04:26:56 +08:00
|
|
|
raise TypeError(
|
|
|
|
"%s: cannot extend enumeration %r"
|
|
|
|
% (class_name, base.__name__)
|
|
|
|
)
|
2020-09-16 22:11:57 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _get_mixins_(class_name, bases):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Returns the type for creating enum members, and the first inherited
|
2013-06-15 07:55:46 +08:00
|
|
|
enum class.
|
|
|
|
|
|
|
|
bases: the tuple of bases that was given to __new__
|
|
|
|
"""
|
|
|
|
if not bases:
|
|
|
|
return object, Enum
|
|
|
|
|
2018-09-22 10:03:09 +08:00
|
|
|
def _find_data_type(bases):
|
2020-09-16 06:56:26 +08:00
|
|
|
data_types = []
|
2018-09-22 10:03:09 +08:00
|
|
|
for chain in bases:
|
2020-09-16 06:56:26 +08:00
|
|
|
candidate = None
|
2018-09-22 10:03:09 +08:00
|
|
|
for base in chain.__mro__:
|
|
|
|
if base is object:
|
|
|
|
continue
|
2020-12-07 16:17:31 +08:00
|
|
|
elif issubclass(base, Enum):
|
|
|
|
if base._member_type_ is not object:
|
|
|
|
data_types.append(base._member_type_)
|
|
|
|
break
|
2018-09-22 10:03:09 +08:00
|
|
|
elif '__new__' in base.__dict__:
|
2018-10-06 14:29:36 +08:00
|
|
|
if issubclass(base, Enum):
|
2018-09-22 10:03:09 +08:00
|
|
|
continue
|
2020-09-16 06:56:26 +08:00
|
|
|
data_types.append(candidate or base)
|
|
|
|
break
|
2020-12-07 16:17:31 +08:00
|
|
|
else:
|
2020-09-16 06:56:26 +08:00
|
|
|
candidate = base
|
|
|
|
if len(data_types) > 1:
|
2020-09-16 22:11:57 +08:00
|
|
|
raise TypeError('%r: too many data types: %r' % (class_name, data_types))
|
2020-09-16 06:56:26 +08:00
|
|
|
elif data_types:
|
|
|
|
return data_types[0]
|
|
|
|
else:
|
|
|
|
return None
|
2018-09-22 10:03:09 +08:00
|
|
|
|
|
|
|
# ensure final parent class is an Enum derivative, find any concrete
|
|
|
|
# data type, and check that Enum has no members
|
|
|
|
first_enum = bases[-1]
|
|
|
|
if not issubclass(first_enum, Enum):
|
|
|
|
raise TypeError("new enumerations should be created as "
|
|
|
|
"`EnumName([mixin_type, ...] [data_type,] enum_type)`")
|
|
|
|
member_type = _find_data_type(bases) or object
|
|
|
|
if first_enum._member_names_:
|
|
|
|
raise TypeError("Cannot extend enumerations")
|
2013-06-15 07:55:46 +08:00
|
|
|
return member_type, first_enum
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _find_new_(classdict, member_type, first_enum):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Returns the __new__ to be used for creating the enum members.
|
2013-06-15 07:55:46 +08:00
|
|
|
|
|
|
|
classdict: the class dictionary given to __new__
|
|
|
|
member_type: the data type whose __new__ will be used by default
|
|
|
|
first_enum: enumeration to check for an overriding __new__
|
|
|
|
"""
|
|
|
|
# now find the correct __new__, checking to see of one was defined
|
|
|
|
# by the user; also check earlier enum classes in case a __new__ was
|
|
|
|
# saved as __new_member__
|
|
|
|
__new__ = classdict.get('__new__', None)
|
|
|
|
|
|
|
|
# should __new__ be saved as __new_member__ later?
|
2021-04-22 01:20:44 +08:00
|
|
|
save_new = first_enum is not None and __new__ is not None
|
2013-06-15 07:55:46 +08:00
|
|
|
|
|
|
|
if __new__ is None:
|
|
|
|
# check all possibles for __new_member__ before falling back to
|
|
|
|
# __new__
|
|
|
|
for method in ('__new_member__', '__new__'):
|
|
|
|
for possible in (member_type, first_enum):
|
|
|
|
target = getattr(possible, method, None)
|
|
|
|
if target not in {
|
|
|
|
None,
|
|
|
|
None.__new__,
|
|
|
|
object.__new__,
|
|
|
|
Enum.__new__,
|
|
|
|
}:
|
|
|
|
__new__ = target
|
|
|
|
break
|
|
|
|
if __new__ is not None:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
__new__ = object.__new__
|
|
|
|
|
|
|
|
# if a non-object.__new__ is used then whatever value/tuple was
|
|
|
|
# assigned to the enum member name will be passed to __new__ and to the
|
|
|
|
# new enum member's __init__
|
2021-04-22 01:20:44 +08:00
|
|
|
if first_enum is None or __new__ in (Enum.__new__, object.__new__):
|
2013-06-15 07:55:46 +08:00
|
|
|
use_args = False
|
|
|
|
else:
|
|
|
|
use_args = True
|
|
|
|
return __new__, save_new, use_args
|
2021-03-31 12:17:26 +08:00
|
|
|
EnumMeta = EnumType
|
2013-06-15 07:55:46 +08:00
|
|
|
|
|
|
|
|
2021-03-31 12:17:26 +08:00
|
|
|
class Enum(metaclass=EnumType):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Generic enumeration.
|
2013-06-15 07:55:46 +08:00
|
|
|
|
|
|
|
Derive from this class to define new enumerations.
|
|
|
|
"""
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
|
2013-06-15 07:55:46 +08:00
|
|
|
def __new__(cls, value):
|
|
|
|
# all enum instances are actually created during class construction
|
|
|
|
# without calling this method; this method is called by the metaclass'
|
|
|
|
# __call__ (i.e. Color(3) ), and by pickle
|
|
|
|
if type(value) is cls:
|
2016-11-22 01:22:05 +08:00
|
|
|
# For lookups like Color(Color.RED)
|
2013-06-15 07:55:46 +08:00
|
|
|
return value
|
|
|
|
# by-value search for a matching enum member
|
|
|
|
# see if it's in the reverse mapping (for hashable values)
|
2013-07-20 10:35:56 +08:00
|
|
|
try:
|
2018-12-27 02:45:33 +08:00
|
|
|
return cls._value2member_map_[value]
|
|
|
|
except KeyError:
|
|
|
|
# Not found, no need to do long O(n) search
|
|
|
|
pass
|
2013-07-20 10:35:56 +08:00
|
|
|
except TypeError:
|
|
|
|
# not there, now do long search -- O(n) behavior
|
2013-07-20 10:47:21 +08:00
|
|
|
for member in cls._member_map_.values():
|
2014-09-17 08:31:23 +08:00
|
|
|
if member._value_ == value:
|
2013-07-20 10:35:56 +08:00
|
|
|
return member
|
2016-08-31 15:12:15 +08:00
|
|
|
# still not found -- try _missing_ hook
|
2018-09-13 02:43:34 +08:00
|
|
|
try:
|
|
|
|
exc = None
|
|
|
|
result = cls._missing_(value)
|
|
|
|
except Exception as e:
|
|
|
|
exc = e
|
|
|
|
result = None
|
2021-04-12 23:51:20 +08:00
|
|
|
try:
|
|
|
|
if isinstance(result, cls):
|
|
|
|
return result
|
|
|
|
elif (
|
|
|
|
Flag is not None and issubclass(cls, Flag)
|
|
|
|
and cls._boundary_ is EJECT and isinstance(result, int)
|
|
|
|
):
|
|
|
|
return result
|
|
|
|
else:
|
|
|
|
ve_exc = ValueError("%r is not a valid %s" % (value, cls.__qualname__))
|
|
|
|
if result is None and exc is None:
|
|
|
|
raise ve_exc
|
|
|
|
elif exc is None:
|
|
|
|
exc = TypeError(
|
|
|
|
'error in %s._missing_: returned %r instead of None or a valid member'
|
|
|
|
% (cls.__name__, result)
|
|
|
|
)
|
|
|
|
if not isinstance(exc, ValueError):
|
|
|
|
exc.__context__ = ve_exc
|
|
|
|
raise exc
|
|
|
|
finally:
|
|
|
|
# ensure all variables that could hold an exception are destroyed
|
|
|
|
exc = None
|
|
|
|
ve_exc = None
|
2016-08-31 15:12:15 +08:00
|
|
|
|
2016-09-11 14:36:59 +08:00
|
|
|
def _generate_next_value_(name, start, count, last_values):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Generate the next value when not given.
|
|
|
|
|
|
|
|
name: the name of the member
|
|
|
|
start: the initial start value or None
|
|
|
|
count: the number of existing members
|
|
|
|
last_value: the last value assigned or None
|
|
|
|
"""
|
2016-09-11 14:36:59 +08:00
|
|
|
for last_value in reversed(last_values):
|
|
|
|
try:
|
|
|
|
return last_value + 1
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
else:
|
2016-08-31 15:12:15 +08:00
|
|
|
return start
|
2016-09-11 14:36:59 +08:00
|
|
|
|
2016-08-31 15:12:15 +08:00
|
|
|
@classmethod
|
|
|
|
def _missing_(cls, value):
|
2020-09-17 01:26:50 +08:00
|
|
|
return None
|
2013-06-15 07:55:46 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
2021-03-31 12:17:26 +08:00
|
|
|
return "%s.%s" % ( self.__class__.__name__, self._name_)
|
2013-06-15 07:55:46 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
2021-03-31 12:17:26 +08:00
|
|
|
return "%s" % (self._name_, )
|
2013-06-15 07:55:46 +08:00
|
|
|
|
2013-08-12 21:51:41 +08:00
|
|
|
def __dir__(self):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Returns all members and all public methods
|
|
|
|
"""
|
2014-10-14 23:58:32 +08:00
|
|
|
added_behavior = [
|
|
|
|
m
|
|
|
|
for cls in self.__class__.mro()
|
|
|
|
for m in cls.__dict__
|
2015-03-11 23:43:12 +08:00
|
|
|
if m[0] != '_' and m not in self._member_map_
|
2020-09-21 21:11:06 +08:00
|
|
|
] + [m for m in self.__dict__ if m[0] != '_']
|
2014-10-22 04:40:35 +08:00
|
|
|
return (['__class__', '__doc__', '__module__'] + added_behavior)
|
2013-08-12 21:51:41 +08:00
|
|
|
|
2013-09-01 10:17:41 +08:00
|
|
|
def __format__(self, format_spec):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Returns format using actual value type unless __str__ has been overridden.
|
|
|
|
"""
|
2013-09-01 10:17:41 +08:00
|
|
|
# mixed-in Enums should use the mixed-in type's __format__, otherwise
|
|
|
|
# we can get strange results with the Enum name showing up instead of
|
|
|
|
# the value
|
|
|
|
|
2019-07-05 02:28:37 +08:00
|
|
|
# pure Enum branch, or branch with __str__ explicitly overridden
|
2020-12-09 03:14:10 +08:00
|
|
|
str_overridden = type(self).__str__ not in (Enum.__str__, Flag.__str__)
|
2019-07-05 02:28:37 +08:00
|
|
|
if self._member_type_ is object or str_overridden:
|
2013-09-01 10:17:41 +08:00
|
|
|
cls = str
|
|
|
|
val = str(self)
|
|
|
|
# mix-in branch
|
|
|
|
else:
|
|
|
|
cls = self._member_type_
|
2014-09-17 08:31:23 +08:00
|
|
|
val = self._value_
|
2013-09-01 10:17:41 +08:00
|
|
|
return cls.__format__(val, format_spec)
|
|
|
|
|
2013-06-15 07:55:46 +08:00
|
|
|
def __hash__(self):
|
2013-07-20 10:47:21 +08:00
|
|
|
return hash(self._name_)
|
2013-06-15 07:55:46 +08:00
|
|
|
|
2014-02-09 03:36:27 +08:00
|
|
|
def __reduce_ex__(self, proto):
|
2014-02-19 04:37:12 +08:00
|
|
|
return self.__class__, (self._value_, )
|
2014-02-09 03:36:27 +08:00
|
|
|
|
2021-01-13 15:47:57 +08:00
|
|
|
# enum.property is used to provide access to the `name` and
|
|
|
|
# `value` attributes of enum members while keeping some measure of
|
2013-06-15 07:55:46 +08:00
|
|
|
# protection from modification, while still allowing for an enumeration
|
|
|
|
# to have members named `name` and `value`. This works because enumeration
|
2021-01-13 15:47:57 +08:00
|
|
|
# members are not set directly on the enum class; they are kept in a
|
|
|
|
# separate structure, _member_map_, which is where enum.property looks for
|
|
|
|
# them
|
2013-06-15 07:55:46 +08:00
|
|
|
|
2021-01-13 15:47:57 +08:00
|
|
|
@property
|
2013-06-15 07:55:46 +08:00
|
|
|
def name(self):
|
2013-09-16 07:59:35 +08:00
|
|
|
"""The name of the Enum member."""
|
2013-07-20 10:47:21 +08:00
|
|
|
return self._name_
|
2013-06-15 07:55:46 +08:00
|
|
|
|
2021-01-13 15:47:57 +08:00
|
|
|
@property
|
2013-06-15 07:55:46 +08:00
|
|
|
def value(self):
|
2013-09-16 07:59:35 +08:00
|
|
|
"""The value of the Enum member."""
|
2013-07-20 10:47:21 +08:00
|
|
|
return self._value_
|
2013-06-15 07:55:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
class IntEnum(int, Enum):
|
2020-09-22 08:23:13 +08:00
|
|
|
"""
|
|
|
|
Enum where members are also (and must be) ints
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class StrEnum(str, Enum):
|
|
|
|
"""
|
|
|
|
Enum where members are also (and must be) strings
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __new__(cls, *values):
|
|
|
|
if len(values) > 3:
|
|
|
|
raise TypeError('too many arguments for str(): %r' % (values, ))
|
|
|
|
if len(values) == 1:
|
|
|
|
# it must be a string
|
|
|
|
if not isinstance(values[0], str):
|
|
|
|
raise TypeError('%r is not a string' % (values[0], ))
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
if len(values) >= 2:
|
2020-09-22 08:23:13 +08:00
|
|
|
# check that encoding argument is a string
|
|
|
|
if not isinstance(values[1], str):
|
|
|
|
raise TypeError('encoding must be a string, not %r' % (values[1], ))
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
if len(values) == 3:
|
|
|
|
# check that errors argument is a string
|
|
|
|
if not isinstance(values[2], str):
|
|
|
|
raise TypeError('errors must be a string, not %r' % (values[2]))
|
2020-09-22 08:23:13 +08:00
|
|
|
value = str(*values)
|
|
|
|
member = str.__new__(cls, value)
|
|
|
|
member._value_ = value
|
|
|
|
return member
|
2013-07-19 08:05:39 +08:00
|
|
|
|
2020-09-23 04:00:07 +08:00
|
|
|
__str__ = str.__str__
|
|
|
|
|
2020-12-11 04:20:06 +08:00
|
|
|
def _generate_next_value_(name, start, count, last_values):
|
|
|
|
"""
|
|
|
|
Return the lower-cased version of the member name.
|
|
|
|
"""
|
|
|
|
return name.lower()
|
|
|
|
|
2013-07-19 08:05:39 +08:00
|
|
|
|
2015-03-19 08:27:57 +08:00
|
|
|
def _reduce_ex_by_name(self, proto):
|
|
|
|
return self.name
|
|
|
|
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
class FlagBoundary(StrEnum):
|
|
|
|
"""
|
|
|
|
control how out of range values are handled
|
|
|
|
"strict" -> error is raised [default for Flag]
|
|
|
|
"conform" -> extra bits are discarded
|
|
|
|
"eject" -> lose flag status [default for IntFlag]
|
|
|
|
"keep" -> keep flag status and all bits
|
|
|
|
"""
|
|
|
|
STRICT = auto()
|
|
|
|
CONFORM = auto()
|
|
|
|
EJECT = auto()
|
|
|
|
KEEP = auto()
|
|
|
|
STRICT, CONFORM, EJECT, KEEP = FlagBoundary
|
|
|
|
|
|
|
|
|
|
|
|
class Flag(Enum, boundary=STRICT):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Support for flags
|
|
|
|
"""
|
2016-09-11 14:36:59 +08:00
|
|
|
|
|
|
|
def _generate_next_value_(name, start, count, last_values):
|
2016-08-31 15:12:15 +08:00
|
|
|
"""
|
|
|
|
Generate the next value when not given.
|
|
|
|
|
|
|
|
name: the name of the member
|
2019-09-21 13:22:54 +08:00
|
|
|
start: the initial start value or None
|
2016-08-31 15:12:15 +08:00
|
|
|
count: the number of existing members
|
|
|
|
last_value: the last value assigned or None
|
|
|
|
"""
|
|
|
|
if not count:
|
|
|
|
return start if start is not None else 1
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
last_value = max(last_values)
|
|
|
|
try:
|
|
|
|
high_bit = _high_bit(last_value)
|
|
|
|
except Exception:
|
|
|
|
raise TypeError('Invalid Flag value: %r' % last_value) from None
|
2016-08-31 15:12:15 +08:00
|
|
|
return 2 ** (high_bit+1)
|
|
|
|
|
|
|
|
@classmethod
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
def _iter_member_by_value_(cls, value):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
Extract all members from the value in definition (i.e. increasing value) order.
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
for val in _iter_bits_lsb(value & cls._flag_mask_):
|
|
|
|
yield cls._value2member_map_.get(val)
|
|
|
|
|
|
|
|
_iter_member_ = _iter_member_by_value_
|
2016-08-31 15:12:15 +08:00
|
|
|
|
|
|
|
@classmethod
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
def _iter_member_by_def_(cls, value):
|
|
|
|
"""
|
|
|
|
Extract all members from the value in definition order.
|
|
|
|
"""
|
|
|
|
yield from sorted(
|
|
|
|
cls._iter_member_by_value_(value),
|
|
|
|
key=lambda m: m._sort_order_,
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _missing_(cls, value):
|
2016-09-19 04:15:41 +08:00
|
|
|
"""
|
2021-04-15 21:49:54 +08:00
|
|
|
Create a composite member containing all canonical members present in `value`.
|
|
|
|
|
|
|
|
If non-member values are present, result depends on `_boundary_` setting.
|
2016-09-19 04:15:41 +08:00
|
|
|
"""
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
if not isinstance(value, int):
|
|
|
|
raise ValueError(
|
|
|
|
"%r is not a valid %s" % (value, cls.__qualname__)
|
|
|
|
)
|
|
|
|
# check boundaries
|
|
|
|
# - value must be in range (e.g. -16 <-> +15, i.e. ~15 <-> 15)
|
|
|
|
# - value must not include any skipped flags (e.g. if bit 2 is not
|
|
|
|
# defined, then 0d10 is invalid)
|
|
|
|
flag_mask = cls._flag_mask_
|
|
|
|
all_bits = cls._all_bits_
|
|
|
|
neg_value = None
|
|
|
|
if (
|
|
|
|
not ~all_bits <= value <= all_bits
|
|
|
|
or value & (all_bits ^ flag_mask)
|
|
|
|
):
|
|
|
|
if cls._boundary_ is STRICT:
|
|
|
|
max_bits = max(value.bit_length(), flag_mask.bit_length())
|
|
|
|
raise ValueError(
|
|
|
|
"%s: invalid value: %r\n given %s\n allowed %s" % (
|
|
|
|
cls.__name__, value, bin(value, max_bits), bin(flag_mask, max_bits),
|
|
|
|
))
|
|
|
|
elif cls._boundary_ is CONFORM:
|
|
|
|
value = value & flag_mask
|
|
|
|
elif cls._boundary_ is EJECT:
|
|
|
|
return value
|
|
|
|
elif cls._boundary_ is KEEP:
|
|
|
|
if value < 0:
|
|
|
|
value = (
|
|
|
|
max(all_bits+1, 2**(value.bit_length()))
|
|
|
|
+ value
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
raise ValueError(
|
|
|
|
'unknown flag boundary: %r' % (cls._boundary_, )
|
|
|
|
)
|
|
|
|
if value < 0:
|
|
|
|
neg_value = value
|
|
|
|
value = all_bits + 1 + value
|
|
|
|
# get members and unknown
|
|
|
|
unknown = value & ~flag_mask
|
|
|
|
member_value = value & flag_mask
|
|
|
|
if unknown and cls._boundary_ is not KEEP:
|
|
|
|
raise ValueError(
|
|
|
|
'%s(%r) --> unknown values %r [%s]'
|
|
|
|
% (cls.__name__, value, unknown, bin(unknown))
|
|
|
|
)
|
|
|
|
# normal Flag?
|
|
|
|
__new__ = getattr(cls, '__new_member__', None)
|
|
|
|
if cls._member_type_ is object and not __new__:
|
2016-09-19 04:15:41 +08:00
|
|
|
# construct a singleton enum pseudo-member
|
2016-08-31 15:12:15 +08:00
|
|
|
pseudo_member = object.__new__(cls)
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
else:
|
|
|
|
pseudo_member = (__new__ or cls._member_type_.__new__)(cls, value)
|
2021-04-22 01:20:44 +08:00
|
|
|
if not hasattr(pseudo_member, '_value_'):
|
2016-08-31 15:12:15 +08:00
|
|
|
pseudo_member._value_ = value
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
if member_value:
|
|
|
|
pseudo_member._name_ = '|'.join([
|
|
|
|
m._name_ for m in cls._iter_member_(member_value)
|
|
|
|
])
|
|
|
|
if unknown:
|
|
|
|
pseudo_member._name_ += '|0x%x' % unknown
|
|
|
|
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:
|
2017-01-25 04:12:06 +08:00
|
|
|
pseudo_member = cls._value2member_map_.setdefault(value, pseudo_member)
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
if neg_value is not None:
|
|
|
|
cls._value2member_map_[neg_value] = pseudo_member
|
2016-08-31 15:12:15 +08:00
|
|
|
return pseudo_member
|
|
|
|
|
|
|
|
def __contains__(self, other):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Returns True if self has at least the same flags set as other.
|
|
|
|
"""
|
2016-08-31 15:12:15 +08:00
|
|
|
if not isinstance(other, self.__class__):
|
2018-09-11 02:21:04 +08:00
|
|
|
raise TypeError(
|
|
|
|
"unsupported operand type(s) for 'in': '%s' and '%s'" % (
|
|
|
|
type(other).__qualname__, self.__class__.__qualname__))
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
if other._value_ == 0 or self._value_ == 0:
|
|
|
|
return False
|
2016-08-31 15:12:15 +08:00
|
|
|
return other._value_ & self._value_ == other._value_
|
|
|
|
|
2020-09-17 04:01:00 +08:00
|
|
|
def __iter__(self):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
Returns flags in definition order.
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
yield from self._iter_member_(self._value_)
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return self._value_.bit_count()
|
2020-09-17 04:01:00 +08:00
|
|
|
|
2016-08-31 15:12:15 +08:00
|
|
|
def __repr__(self):
|
2021-03-31 12:17:26 +08:00
|
|
|
cls_name = self.__class__.__name__
|
|
|
|
if self._name_ is None:
|
|
|
|
return "0x%x" % (self._value_, )
|
|
|
|
if _is_single_bit(self._value_):
|
|
|
|
return '%s.%s' % (cls_name, self._name_)
|
|
|
|
if self._boundary_ is not FlagBoundary.KEEP:
|
|
|
|
return '%s.' % cls_name + ('|%s.' % cls_name).join(self.name.split('|'))
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
else:
|
2021-03-31 12:17:26 +08:00
|
|
|
name = []
|
|
|
|
for n in self._name_.split('|'):
|
|
|
|
if n.startswith('0'):
|
|
|
|
name.append(n)
|
|
|
|
else:
|
|
|
|
name.append('%s.%s' % (cls_name, n))
|
|
|
|
return '|'.join(name)
|
2016-08-31 15:12:15 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
cls = self.__class__
|
2021-03-31 12:17:26 +08:00
|
|
|
if self._name_ is None:
|
|
|
|
return '%s(%x)' % (cls.__name__, self._value_)
|
2016-08-31 15:12:15 +08:00
|
|
|
else:
|
2021-03-31 12:17:26 +08:00
|
|
|
return self._name_
|
2016-08-31 15:12:15 +08:00
|
|
|
|
2016-09-03 07:32:32 +08:00
|
|
|
def __bool__(self):
|
|
|
|
return bool(self._value_)
|
|
|
|
|
2016-08-31 15:12:15 +08:00
|
|
|
def __or__(self, other):
|
|
|
|
if not isinstance(other, self.__class__):
|
|
|
|
return NotImplemented
|
|
|
|
return self.__class__(self._value_ | other._value_)
|
|
|
|
|
|
|
|
def __and__(self, other):
|
|
|
|
if not isinstance(other, self.__class__):
|
|
|
|
return NotImplemented
|
|
|
|
return self.__class__(self._value_ & other._value_)
|
|
|
|
|
|
|
|
def __xor__(self, other):
|
|
|
|
if not isinstance(other, self.__class__):
|
|
|
|
return NotImplemented
|
|
|
|
return self.__class__(self._value_ ^ other._value_)
|
|
|
|
|
|
|
|
def __invert__(self):
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
if self._inverted_ is None:
|
|
|
|
if self._boundary_ is KEEP:
|
|
|
|
# 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_)
|
|
|
|
self._inverted_._inverted_ = self
|
|
|
|
return self._inverted_
|
2016-08-31 15:12:15 +08:00
|
|
|
|
|
|
|
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
class IntFlag(int, Flag, boundary=EJECT):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Support for integer-based Flags
|
|
|
|
"""
|
2016-08-31 15:12:15 +08:00
|
|
|
|
|
|
|
def __or__(self, other):
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
if isinstance(other, self.__class__):
|
|
|
|
other = other._value_
|
|
|
|
elif isinstance(other, int):
|
|
|
|
other = other
|
|
|
|
else:
|
2016-08-31 15:12:15 +08:00
|
|
|
return NotImplemented
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
value = self._value_
|
|
|
|
return self.__class__(value | other)
|
2016-08-31 15:12:15 +08:00
|
|
|
|
|
|
|
def __and__(self, other):
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
if isinstance(other, self.__class__):
|
|
|
|
other = other._value_
|
|
|
|
elif isinstance(other, int):
|
|
|
|
other = other
|
|
|
|
else:
|
2016-08-31 15:12:15 +08:00
|
|
|
return NotImplemented
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
value = self._value_
|
|
|
|
return self.__class__(value & other)
|
2016-08-31 15:12:15 +08:00
|
|
|
|
|
|
|
def __xor__(self, other):
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
if isinstance(other, self.__class__):
|
|
|
|
other = other._value_
|
|
|
|
elif isinstance(other, int):
|
|
|
|
other = other
|
|
|
|
else:
|
2016-08-31 15:12:15 +08:00
|
|
|
return NotImplemented
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
value = self._value_
|
|
|
|
return self.__class__(value ^ other)
|
2016-08-31 15:12:15 +08:00
|
|
|
|
|
|
|
__ror__ = __or__
|
|
|
|
__rand__ = __and__
|
|
|
|
__rxor__ = __xor__
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
__invert__ = Flag.__invert__
|
2016-08-31 15:12:15 +08:00
|
|
|
|
|
|
|
def _high_bit(value):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
returns index of highest bit, or -1 if value is zero or negative
|
|
|
|
"""
|
2016-09-19 04:15:41 +08:00
|
|
|
return value.bit_length() - 1
|
2016-08-31 15:12:15 +08:00
|
|
|
|
2013-07-19 08:05:39 +08:00
|
|
|
def unique(enumeration):
|
2020-12-09 04:26:56 +08:00
|
|
|
"""
|
|
|
|
Class decorator for enumerations ensuring unique member values.
|
|
|
|
"""
|
2013-07-19 08:05:39 +08:00
|
|
|
duplicates = []
|
|
|
|
for name, member in enumeration.__members__.items():
|
|
|
|
if name != member.name:
|
|
|
|
duplicates.append((name, member.name))
|
|
|
|
if duplicates:
|
|
|
|
alias_details = ', '.join(
|
|
|
|
["%s -> %s" % (alias, name) for (alias, name) in duplicates])
|
|
|
|
raise ValueError('duplicate values found in %r: %s' %
|
|
|
|
(enumeration, alias_details))
|
|
|
|
return enumeration
|
2016-09-19 04:15:41 +08:00
|
|
|
|
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags.
Iterating, repr(), and str() show members in definition order.
When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired:
>>> class Test(Flag, boundary=CONFORM):
... ONE = 1
... TWO = 2
...
>>> Test(5)
<Test.ONE: 1>
Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits.
Iteration is now in member definition order. If member definition order
matches increasing value order, then a more efficient method of flag
decomposition is used; otherwise, sort() is called on the results of
that method to get definition order.
``re`` module:
repr() has been modified to support as closely as possible its previous
output; the big difference is that inverted flags cannot be output as
before because the inversion operation now always returns the comparable
positive result; i.e.
re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG)
in both of the above terms, the ``value`` is 282.
re's tests have been updated to reflect the modifications to repr().
2021-01-26 06:26:19 +08:00
|
|
|
def _power_of_two(value):
|
|
|
|
if value < 1:
|
|
|
|
return False
|
|
|
|
return value == 2 ** _high_bit(value)
|
2021-03-31 12:17:26 +08:00
|
|
|
|
|
|
|
def global_enum_repr(self):
|
|
|
|
return '%s.%s' % (self.__class__.__module__, self._name_)
|
|
|
|
|
|
|
|
def global_flag_repr(self):
|
|
|
|
module = self.__class__.__module__
|
|
|
|
cls_name = self.__class__.__name__
|
|
|
|
if self._name_ is None:
|
|
|
|
return "%x" % (module, cls_name, self._value_)
|
|
|
|
if _is_single_bit(self):
|
|
|
|
return '%s.%s' % (module, self._name_)
|
|
|
|
if self._boundary_ is not FlagBoundary.KEEP:
|
|
|
|
return module + module.join(self.name.split('|'))
|
|
|
|
else:
|
|
|
|
name = []
|
|
|
|
for n in self._name_.split('|'):
|
|
|
|
if n.startswith('0'):
|
|
|
|
name.append(n)
|
|
|
|
else:
|
|
|
|
name.append('%s.%s' % (module, n))
|
|
|
|
return '|'.join(name)
|
|
|
|
|
|
|
|
|
|
|
|
def global_enum(cls):
|
|
|
|
"""
|
|
|
|
decorator that makes the repr() of an enum member reference its module
|
|
|
|
instead of its class; also exports all members to the enum's module's
|
|
|
|
global namespace
|
|
|
|
"""
|
|
|
|
if issubclass(cls, Flag):
|
|
|
|
cls.__repr__ = global_flag_repr
|
|
|
|
else:
|
|
|
|
cls.__repr__ = global_enum_repr
|
|
|
|
sys.modules[cls.__module__].__dict__.update(cls.__members__)
|
|
|
|
return cls
|
2021-04-22 01:20:44 +08:00
|
|
|
|
|
|
|
def _simple_enum(etype=Enum, *, boundary=None, use_args=None):
|
|
|
|
"""
|
|
|
|
Class decorator that converts a normal class into an :class:`Enum`. No
|
|
|
|
safety checks are done, and some advanced behavior (such as
|
|
|
|
:func:`__init_subclass__`) is not available. Enum creation can be faster
|
|
|
|
using :func:`simple_enum`.
|
|
|
|
|
|
|
|
>>> from enum import Enum, _simple_enum
|
|
|
|
>>> @_simple_enum(Enum)
|
|
|
|
... class Color:
|
|
|
|
... RED = auto()
|
|
|
|
... GREEN = auto()
|
|
|
|
... BLUE = auto()
|
|
|
|
>>> Color
|
|
|
|
<enum 'Color'>
|
|
|
|
"""
|
|
|
|
def convert_class(cls):
|
|
|
|
nonlocal use_args
|
|
|
|
cls_name = cls.__name__
|
|
|
|
if use_args is None:
|
|
|
|
use_args = etype._use_args_
|
|
|
|
__new__ = cls.__dict__.get('__new__')
|
|
|
|
if __new__ is not None:
|
|
|
|
new_member = __new__.__func__
|
|
|
|
else:
|
|
|
|
new_member = etype._member_type_.__new__
|
|
|
|
attrs = {}
|
|
|
|
body = {}
|
|
|
|
if __new__ is not None:
|
|
|
|
body['__new_member__'] = new_member
|
|
|
|
body['_new_member_'] = new_member
|
|
|
|
body['_use_args_'] = use_args
|
|
|
|
body['_generate_next_value_'] = gnv = etype._generate_next_value_
|
|
|
|
body['_member_names_'] = member_names = []
|
|
|
|
body['_member_map_'] = member_map = {}
|
|
|
|
body['_value2member_map_'] = value2member_map = {}
|
|
|
|
body['_member_type_'] = member_type = etype._member_type_
|
|
|
|
if issubclass(etype, Flag):
|
|
|
|
body['_boundary_'] = boundary or etype._boundary_
|
|
|
|
body['_flag_mask_'] = None
|
|
|
|
body['_all_bits_'] = None
|
|
|
|
body['_inverted_'] = None
|
|
|
|
for name, obj in cls.__dict__.items():
|
|
|
|
if name in ('__dict__', '__weakref__'):
|
|
|
|
continue
|
|
|
|
if _is_dunder(name) or _is_private(cls_name, name) or _is_sunder(name) or _is_descriptor(obj):
|
|
|
|
body[name] = obj
|
|
|
|
else:
|
|
|
|
attrs[name] = obj
|
|
|
|
if cls.__dict__.get('__doc__') is None:
|
|
|
|
body['__doc__'] = 'An enumeration.'
|
|
|
|
#
|
|
|
|
# double check that repr and friends are not the mixin's or various
|
|
|
|
# things break (such as pickle)
|
|
|
|
# however, if the method is defined in the Enum itself, don't replace
|
|
|
|
# it
|
|
|
|
enum_class = type(cls_name, (etype, ), body, boundary=boundary, _simple=True)
|
|
|
|
for name in ('__repr__', '__str__', '__format__', '__reduce_ex__'):
|
|
|
|
if name in body:
|
|
|
|
continue
|
|
|
|
class_method = getattr(enum_class, name)
|
|
|
|
obj_method = getattr(member_type, name, None)
|
|
|
|
enum_method = getattr(etype, name, None)
|
|
|
|
if obj_method is not None and obj_method is class_method:
|
|
|
|
setattr(enum_class, name, enum_method)
|
|
|
|
gnv_last_values = []
|
|
|
|
if issubclass(enum_class, Flag):
|
|
|
|
# Flag / IntFlag
|
|
|
|
single_bits = multi_bits = 0
|
|
|
|
for name, value in attrs.items():
|
|
|
|
if isinstance(value, auto) and auto.value is _auto_null:
|
|
|
|
value = gnv(name, 1, len(member_names), gnv_last_values)
|
|
|
|
if value in value2member_map:
|
|
|
|
# an alias to an existing member
|
|
|
|
redirect = property()
|
|
|
|
redirect.__set_name__(enum_class, name)
|
|
|
|
setattr(enum_class, name, redirect)
|
|
|
|
member_map[name] = value2member_map[value]
|
|
|
|
else:
|
|
|
|
# create the member
|
|
|
|
if use_args:
|
|
|
|
if not isinstance(value, tuple):
|
|
|
|
value = (value, )
|
|
|
|
member = new_member(enum_class, *value)
|
|
|
|
value = value[0]
|
|
|
|
else:
|
|
|
|
member = new_member(enum_class)
|
|
|
|
if __new__ is None:
|
|
|
|
member._value_ = value
|
|
|
|
member._name_ = name
|
|
|
|
member.__objclass__ = enum_class
|
|
|
|
member.__init__(value)
|
|
|
|
redirect = property()
|
|
|
|
redirect.__set_name__(enum_class, name)
|
|
|
|
setattr(enum_class, name, redirect)
|
|
|
|
member_map[name] = member
|
|
|
|
member._sort_order_ = len(member_names)
|
|
|
|
value2member_map[value] = member
|
|
|
|
if _is_single_bit(value):
|
|
|
|
# not a multi-bit alias, record in _member_names_ and _flag_mask_
|
|
|
|
member_names.append(name)
|
|
|
|
single_bits |= value
|
|
|
|
else:
|
|
|
|
multi_bits |= value
|
|
|
|
gnv_last_values.append(value)
|
|
|
|
enum_class._flag_mask_ = single_bits
|
|
|
|
enum_class._all_bits_ = 2 ** ((single_bits|multi_bits).bit_length()) - 1
|
|
|
|
# set correct __iter__
|
|
|
|
member_list = [m._value_ for m in enum_class]
|
|
|
|
if member_list != sorted(member_list):
|
|
|
|
enum_class._iter_member_ = enum_class._iter_member_by_def_
|
|
|
|
else:
|
|
|
|
# Enum / IntEnum / StrEnum
|
|
|
|
for name, value in attrs.items():
|
|
|
|
if isinstance(value, auto):
|
|
|
|
if value.value is _auto_null:
|
|
|
|
value.value = gnv(name, 1, len(member_names), gnv_last_values)
|
|
|
|
value = value.value
|
|
|
|
if value in value2member_map:
|
|
|
|
# an alias to an existing member
|
|
|
|
redirect = property()
|
|
|
|
redirect.__set_name__(enum_class, name)
|
|
|
|
setattr(enum_class, name, redirect)
|
|
|
|
member_map[name] = value2member_map[value]
|
|
|
|
else:
|
|
|
|
# create the member
|
|
|
|
if use_args:
|
|
|
|
if not isinstance(value, tuple):
|
|
|
|
value = (value, )
|
|
|
|
member = new_member(enum_class, *value)
|
|
|
|
value = value[0]
|
|
|
|
else:
|
|
|
|
member = new_member(enum_class)
|
|
|
|
if __new__ is None:
|
|
|
|
member._value_ = value
|
|
|
|
member._name_ = name
|
|
|
|
member.__objclass__ = enum_class
|
|
|
|
member.__init__(value)
|
|
|
|
member._sort_order_ = len(member_names)
|
|
|
|
redirect = property()
|
|
|
|
redirect.__set_name__(enum_class, name)
|
|
|
|
setattr(enum_class, name, redirect)
|
|
|
|
member_map[name] = member
|
|
|
|
value2member_map[value] = member
|
|
|
|
member_names.append(name)
|
|
|
|
gnv_last_values.append(value)
|
|
|
|
if '__new__' in body:
|
|
|
|
enum_class.__new_member__ = enum_class.__new__
|
|
|
|
enum_class.__new__ = Enum.__new__
|
|
|
|
return enum_class
|
|
|
|
return convert_class
|
|
|
|
|
|
|
|
def _test_simple_enum(checked_enum, simple_enum):
|
|
|
|
"""
|
|
|
|
A function that can be used to test an enum created with :func:`_simple_enum`
|
|
|
|
against the version created by subclassing :class:`Enum`::
|
|
|
|
|
|
|
|
>>> from enum import Enum, _simple_enum, _test_simple_enum
|
|
|
|
>>> @_simple_enum(Enum)
|
|
|
|
... class Color:
|
|
|
|
... RED = auto()
|
|
|
|
... GREEN = auto()
|
|
|
|
... BLUE = auto()
|
|
|
|
>>> class CheckedColor(Enum):
|
|
|
|
... RED = auto()
|
|
|
|
... GREEN = auto()
|
|
|
|
... BLUE = auto()
|
|
|
|
>>> _test_simple_enum(CheckedColor, Color)
|
|
|
|
|
|
|
|
If differences are found, a :exc:`TypeError` is raised.
|
|
|
|
"""
|
|
|
|
failed = []
|
|
|
|
if checked_enum.__dict__ != simple_enum.__dict__:
|
|
|
|
checked_dict = checked_enum.__dict__
|
|
|
|
checked_keys = list(checked_dict.keys())
|
|
|
|
simple_dict = simple_enum.__dict__
|
|
|
|
simple_keys = list(simple_dict.keys())
|
|
|
|
member_names = set(
|
|
|
|
list(checked_enum._member_map_.keys())
|
|
|
|
+ list(simple_enum._member_map_.keys())
|
|
|
|
)
|
|
|
|
for key in set(checked_keys + simple_keys):
|
|
|
|
if key in ('__module__', '_member_map_', '_value2member_map_'):
|
|
|
|
# keys known to be different
|
|
|
|
continue
|
|
|
|
elif key in member_names:
|
|
|
|
# members are checked below
|
|
|
|
continue
|
|
|
|
elif key not in simple_keys:
|
|
|
|
failed.append("missing key: %r" % (key, ))
|
|
|
|
elif key not in checked_keys:
|
|
|
|
failed.append("extra key: %r" % (key, ))
|
|
|
|
else:
|
|
|
|
checked_value = checked_dict[key]
|
|
|
|
simple_value = simple_dict[key]
|
|
|
|
if callable(checked_value):
|
|
|
|
continue
|
|
|
|
if key == '__doc__':
|
|
|
|
# remove all spaces/tabs
|
|
|
|
compressed_checked_value = checked_value.replace(' ','').replace('\t','')
|
|
|
|
compressed_simple_value = simple_value.replace(' ','').replace('\t','')
|
|
|
|
if compressed_checked_value != compressed_simple_value:
|
|
|
|
failed.append("%r:\n %s\n %s" % (
|
|
|
|
key,
|
|
|
|
"checked -> %r" % (checked_value, ),
|
|
|
|
"simple -> %r" % (simple_value, ),
|
|
|
|
))
|
|
|
|
elif checked_value != simple_value:
|
|
|
|
failed.append("%r:\n %s\n %s" % (
|
|
|
|
key,
|
|
|
|
"checked -> %r" % (checked_value, ),
|
|
|
|
"simple -> %r" % (simple_value, ),
|
|
|
|
))
|
|
|
|
failed.sort()
|
|
|
|
for name in member_names:
|
|
|
|
failed_member = []
|
|
|
|
if name not in simple_keys:
|
|
|
|
failed.append('missing member from simple enum: %r' % name)
|
|
|
|
elif name not in checked_keys:
|
|
|
|
failed.append('extra member in simple enum: %r' % name)
|
|
|
|
else:
|
|
|
|
checked_member_dict = checked_enum[name].__dict__
|
|
|
|
checked_member_keys = list(checked_member_dict.keys())
|
|
|
|
simple_member_dict = simple_enum[name].__dict__
|
|
|
|
simple_member_keys = list(simple_member_dict.keys())
|
|
|
|
for key in set(checked_member_keys + simple_member_keys):
|
|
|
|
if key in ('__module__', '__objclass__'):
|
|
|
|
# keys known to be different
|
|
|
|
continue
|
|
|
|
elif key not in simple_member_keys:
|
|
|
|
failed_member.append("missing key %r not in the simple enum member %r" % (key, name))
|
|
|
|
elif key not in checked_member_keys:
|
|
|
|
failed_member.append("extra key %r in simple enum member %r" % (key, name))
|
|
|
|
else:
|
|
|
|
checked_value = checked_member_dict[key]
|
|
|
|
simple_value = simple_member_dict[key]
|
|
|
|
if checked_value != simple_value:
|
|
|
|
failed_member.append("%r:\n %s\n %s" % (
|
|
|
|
key,
|
|
|
|
"checked member -> %r" % (checked_value, ),
|
|
|
|
"simple member -> %r" % (simple_value, ),
|
|
|
|
))
|
|
|
|
if failed_member:
|
|
|
|
failed.append('%r member mismatch:\n %s' % (
|
|
|
|
name, '\n '.join(failed_member),
|
|
|
|
))
|
|
|
|
for method in (
|
|
|
|
'__str__', '__repr__', '__reduce_ex__', '__format__',
|
|
|
|
'__getnewargs_ex__', '__getnewargs__', '__reduce_ex__', '__reduce__'
|
|
|
|
):
|
|
|
|
if method in simple_keys and method in checked_keys:
|
|
|
|
# cannot compare functions, and it exists in both, so we're good
|
|
|
|
continue
|
|
|
|
elif method not in simple_keys and method not in checked_keys:
|
|
|
|
# method is inherited -- check it out
|
|
|
|
checked_method = getattr(checked_enum, method, None)
|
|
|
|
simple_method = getattr(simple_enum, method, None)
|
|
|
|
if hasattr(checked_method, '__func__'):
|
|
|
|
checked_method = checked_method.__func__
|
|
|
|
simple_method = simple_method.__func__
|
|
|
|
if checked_method != simple_method:
|
|
|
|
failed.append("%r: %-30s %s" % (
|
|
|
|
method,
|
|
|
|
"checked -> %r" % (checked_method, ),
|
|
|
|
"simple -> %r" % (simple_method, ),
|
|
|
|
))
|
|
|
|
else:
|
|
|
|
# if the method existed in only one of the enums, it will have been caught
|
|
|
|
# in the first checks above
|
|
|
|
pass
|
|
|
|
if failed:
|
|
|
|
raise TypeError('enum mismatch:\n %s' % '\n '.join(failed))
|
|
|
|
|
|
|
|
def _old_convert_(etype, name, module, filter, source=None, *, boundary=None):
|
|
|
|
"""
|
|
|
|
Create a new Enum subclass that replaces a collection of global constants
|
|
|
|
"""
|
|
|
|
# convert all constants from source (or module) that pass filter() to
|
|
|
|
# a new Enum called name, and export the enum and its members back to
|
|
|
|
# module;
|
|
|
|
# also, replace the __reduce_ex__ method so unpickling works in
|
|
|
|
# previous Python versions
|
|
|
|
module_globals = sys.modules[module].__dict__
|
|
|
|
if source:
|
|
|
|
source = source.__dict__
|
|
|
|
else:
|
|
|
|
source = module_globals
|
|
|
|
# _value2member_map_ is populated in the same order every time
|
|
|
|
# for a consistent reverse mapping of number to name when there
|
|
|
|
# are multiple names for the same number.
|
|
|
|
members = [
|
|
|
|
(name, value)
|
|
|
|
for name, value in source.items()
|
|
|
|
if filter(name)]
|
|
|
|
try:
|
|
|
|
# sort by value
|
|
|
|
members.sort(key=lambda t: (t[1], t[0]))
|
|
|
|
except TypeError:
|
|
|
|
# unless some values aren't comparable, in which case sort by name
|
|
|
|
members.sort(key=lambda t: t[0])
|
|
|
|
cls = etype(name, members, module=module, boundary=boundary or KEEP)
|
|
|
|
cls.__reduce_ex__ = _reduce_ex_by_name
|
|
|
|
cls.__repr__ = global_enum_repr
|
|
|
|
return cls
|