diff options
Diffstat (limited to 'modules/language/python/module/enum.py')
-rw-r--r-- | modules/language/python/module/enum.py | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/modules/language/python/module/enum.py b/modules/language/python/module/enum.py index eefc1b5..0f623f0 100644 --- a/modules/language/python/module/enum.py +++ b/modules/language/python/module/enum.py @@ -83,7 +83,7 @@ class _EnumDict(dict): if key not in ( '_order_', '_create_pseudo_member_', '_generate_next_value_', '_missing_', - ): + ): raise ValueError('_names_ are reserved for future Enum use') if key == '_generate_next_value_': setattr(self, '_generate_next_value', value) @@ -118,7 +118,7 @@ class EnumMeta(type): def __prepare__(metacls, cls, bases): # create the namespace dict enum_dict = _EnumDict() - + # inherit previous flags and _generate_next_value_ function member_type, first_enum = metacls._get_mixins_(bases) @@ -157,14 +157,14 @@ class EnumMeta(type): # create our new Enum type enum_class = super().__new__(metacls, cls, bases, classdict) - + pk(enum_class) enum_class._member_names_ = [] # names in definition order enum_class._member_map_ = OrderedDict() # name->value map enum_class._member_type_ = member_type # save attributes from super classes so we know if we can take # the shortcut of storing members in the class dict - + base_attributes = {a for b in enum_class.mro() for a in b.__dict__} # Reverse value->name map for hashable values. @@ -261,7 +261,7 @@ class EnumMeta(type): _order_ = _order_.replace(',', ' ').split() if _order_ != enum_class._member_names_: raise TypeError('member order does not match _order_') - pk('enum class fom new',enum_class) + return enum_class def __bool__(self): @@ -364,11 +364,17 @@ class EnumMeta(type): resulting in an inconsistent Enumeration. """ + member_map = cls.__dict__.get('_member_map_', {}) + if name in member_map: raise AttributeError('Cannot reassign members.') + + pk('set',name) + super().__setattr__(name, value) + def _create_(cls, class_name, names=None, *, module=None, qualname=None, type=None, start=1): """Convenience method to create a new Enum class. @@ -516,13 +522,15 @@ class EnumMeta(type): return __new__, save_new, use_args -pk(1) + class Enum(metaclass=EnumMeta): """Generic enumeration. Derive from this class to define new enumerations. """ + pk(1) + def __new__(cls, value): # all enum instances are actually created during class construction # without calling this method; this method is called by the metaclass' @@ -543,6 +551,8 @@ class Enum(metaclass=EnumMeta): # still not found -- try _missing_ hook return cls._missing_(value) + pk(2) + def _generate_next_value_(name, start, count, last_values): for last_value in reversed(last_values): try: @@ -552,6 +562,8 @@ class Enum(metaclass=EnumMeta): else: return start + pk(3) + @classmethod def _missing_(cls, value): raise ValueError("%r is not a valid %s" % (value, cls.__name__)) @@ -571,7 +583,7 @@ class Enum(metaclass=EnumMeta): if m[0] != '_' and m not in self._member_map_ ] return (['__class__', '__doc__', '__module__'] + added_behavior) - + pk(4) def __format__(self, format_spec): # 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 @@ -599,7 +611,7 @@ class Enum(metaclass=EnumMeta): # to have members named `name` and `value`. This works because enumeration # members are not set directly on the enum class -- __getattr__ is # used to look them up. - + pk(5) @DynamicClassAttribute def name(self): """The name of the Enum member.""" @@ -646,7 +658,6 @@ class Enum(metaclass=EnumMeta): module_globals[name] = cls return cls -pk(2) class IntEnum(int, Enum): """Enum where members are also (and must be) ints""" @@ -763,7 +774,6 @@ class Flag(Enum): inverted = reduce(_or_, inverted_members, self.__class__(0)) return self.__class__(inverted) - class IntFlag(int, Flag): """Support for integer-based Flags""" @@ -828,7 +838,6 @@ class IntFlag(int, Flag): result = self.__class__(~self._value_) return result - def _high_bit(value): """returns index of highest bit, or -1 if value is zero or negative""" return value.bit_length() - 1 |