Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../proc/self/root/lib64/python3....
File: enum.py
import sys
[0] Fix | Delete
from types import MappingProxyType, DynamicClassAttribute
[1] Fix | Delete
[2] Fix | Delete
[3] Fix | Delete
__all__ = [
[4] Fix | Delete
'EnumMeta',
[5] Fix | Delete
'Enum', 'IntEnum', 'Flag', 'IntFlag',
[6] Fix | Delete
'auto', 'unique',
[7] Fix | Delete
]
[8] Fix | Delete
[9] Fix | Delete
[10] Fix | Delete
def _is_descriptor(obj):
[11] Fix | Delete
"""
[12] Fix | Delete
Returns True if obj is a descriptor, False otherwise.
[13] Fix | Delete
"""
[14] Fix | Delete
return (
[15] Fix | Delete
hasattr(obj, '__get__') or
[16] Fix | Delete
hasattr(obj, '__set__') or
[17] Fix | Delete
hasattr(obj, '__delete__')
[18] Fix | Delete
)
[19] Fix | Delete
[20] Fix | Delete
def _is_dunder(name):
[21] Fix | Delete
"""
[22] Fix | Delete
Returns True if a __dunder__ name, False otherwise.
[23] Fix | Delete
"""
[24] Fix | Delete
return (
[25] Fix | Delete
len(name) > 4 and
[26] Fix | Delete
name[:2] == name[-2:] == '__' and
[27] Fix | Delete
name[2] != '_' and
[28] Fix | Delete
name[-3] != '_'
[29] Fix | Delete
)
[30] Fix | Delete
[31] Fix | Delete
def _is_sunder(name):
[32] Fix | Delete
"""
[33] Fix | Delete
Returns True if a _sunder_ name, False otherwise.
[34] Fix | Delete
"""
[35] Fix | Delete
return (
[36] Fix | Delete
len(name) > 2 and
[37] Fix | Delete
name[0] == name[-1] == '_' and
[38] Fix | Delete
name[1:2] != '_' and
[39] Fix | Delete
name[-2:-1] != '_'
[40] Fix | Delete
)
[41] Fix | Delete
[42] Fix | Delete
def _make_class_unpicklable(cls):
[43] Fix | Delete
"""
[44] Fix | Delete
Make the given class un-picklable.
[45] Fix | Delete
"""
[46] Fix | Delete
def _break_on_call_reduce(self, proto):
[47] Fix | Delete
raise TypeError('%r cannot be pickled' % self)
[48] Fix | Delete
cls.__reduce_ex__ = _break_on_call_reduce
[49] Fix | Delete
cls.__module__ = '<unknown>'
[50] Fix | Delete
[51] Fix | Delete
_auto_null = object()
[52] Fix | Delete
class auto:
[53] Fix | Delete
"""
[54] Fix | Delete
Instances are replaced with an appropriate value in Enum class suites.
[55] Fix | Delete
"""
[56] Fix | Delete
value = _auto_null
[57] Fix | Delete
[58] Fix | Delete
[59] Fix | Delete
class _EnumDict(dict):
[60] Fix | Delete
"""
[61] Fix | Delete
Track enum member order and ensure member names are not reused.
[62] Fix | Delete
[63] Fix | Delete
EnumMeta will use the names found in self._member_names as the
[64] Fix | Delete
enumeration member names.
[65] Fix | Delete
"""
[66] Fix | Delete
def __init__(self):
[67] Fix | Delete
super().__init__()
[68] Fix | Delete
self._member_names = []
[69] Fix | Delete
self._last_values = []
[70] Fix | Delete
self._ignore = []
[71] Fix | Delete
self._auto_called = False
[72] Fix | Delete
[73] Fix | Delete
def __setitem__(self, key, value):
[74] Fix | Delete
"""
[75] Fix | Delete
Changes anything not dundered or not a descriptor.
[76] Fix | Delete
[77] Fix | Delete
If an enum member name is used twice, an error is raised; duplicate
[78] Fix | Delete
values are not checked for.
[79] Fix | Delete
[80] Fix | Delete
Single underscore (sunder) names are reserved.
[81] Fix | Delete
"""
[82] Fix | Delete
if _is_sunder(key):
[83] Fix | Delete
if key not in (
[84] Fix | Delete
'_order_', '_create_pseudo_member_',
[85] Fix | Delete
'_generate_next_value_', '_missing_', '_ignore_',
[86] Fix | Delete
):
[87] Fix | Delete
raise ValueError('_names_ are reserved for future Enum use')
[88] Fix | Delete
if key == '_generate_next_value_':
[89] Fix | Delete
# check if members already defined as auto()
[90] Fix | Delete
if self._auto_called:
[91] Fix | Delete
raise TypeError("_generate_next_value_ must be defined before members")
[92] Fix | Delete
setattr(self, '_generate_next_value', value)
[93] Fix | Delete
elif key == '_ignore_':
[94] Fix | Delete
if isinstance(value, str):
[95] Fix | Delete
value = value.replace(',',' ').split()
[96] Fix | Delete
else:
[97] Fix | Delete
value = list(value)
[98] Fix | Delete
self._ignore = value
[99] Fix | Delete
already = set(value) & set(self._member_names)
[100] Fix | Delete
if already:
[101] Fix | Delete
raise ValueError(
[102] Fix | Delete
'_ignore_ cannot specify already set names: %r'
[103] Fix | Delete
% (already, )
[104] Fix | Delete
)
[105] Fix | Delete
elif _is_dunder(key):
[106] Fix | Delete
if key == '__order__':
[107] Fix | Delete
key = '_order_'
[108] Fix | Delete
elif key in self._member_names:
[109] Fix | Delete
# descriptor overwriting an enum?
[110] Fix | Delete
raise TypeError('Attempted to reuse key: %r' % key)
[111] Fix | Delete
elif key in self._ignore:
[112] Fix | Delete
pass
[113] Fix | Delete
elif not _is_descriptor(value):
[114] Fix | Delete
if key in self:
[115] Fix | Delete
# enum overwriting a descriptor?
[116] Fix | Delete
raise TypeError('%r already defined as: %r' % (key, self[key]))
[117] Fix | Delete
if isinstance(value, auto):
[118] Fix | Delete
if value.value == _auto_null:
[119] Fix | Delete
value.value = self._generate_next_value(
[120] Fix | Delete
key,
[121] Fix | Delete
1,
[122] Fix | Delete
len(self._member_names),
[123] Fix | Delete
self._last_values[:],
[124] Fix | Delete
)
[125] Fix | Delete
self._auto_called = True
[126] Fix | Delete
value = value.value
[127] Fix | Delete
self._member_names.append(key)
[128] Fix | Delete
self._last_values.append(value)
[129] Fix | Delete
super().__setitem__(key, value)
[130] Fix | Delete
[131] Fix | Delete
[132] Fix | Delete
# Dummy value for Enum as EnumMeta explicitly checks for it, but of course
[133] Fix | Delete
# until EnumMeta finishes running the first time the Enum class doesn't exist.
[134] Fix | Delete
# This is also why there are checks in EnumMeta like `if Enum is not None`
[135] Fix | Delete
Enum = None
[136] Fix | Delete
[137] Fix | Delete
class EnumMeta(type):
[138] Fix | Delete
"""
[139] Fix | Delete
Metaclass for Enum
[140] Fix | Delete
"""
[141] Fix | Delete
@classmethod
[142] Fix | Delete
def __prepare__(metacls, cls, bases):
[143] Fix | Delete
# check that previous enum members do not exist
[144] Fix | Delete
metacls._check_for_existing_members(cls, bases)
[145] Fix | Delete
# create the namespace dict
[146] Fix | Delete
enum_dict = _EnumDict()
[147] Fix | Delete
# inherit previous flags and _generate_next_value_ function
[148] Fix | Delete
member_type, first_enum = metacls._get_mixins_(cls, bases)
[149] Fix | Delete
if first_enum is not None:
[150] Fix | Delete
enum_dict['_generate_next_value_'] = getattr(
[151] Fix | Delete
first_enum, '_generate_next_value_', None,
[152] Fix | Delete
)
[153] Fix | Delete
return enum_dict
[154] Fix | Delete
[155] Fix | Delete
def __new__(metacls, cls, bases, classdict):
[156] Fix | Delete
# an Enum class is final once enumeration items have been defined; it
[157] Fix | Delete
# cannot be mixed with other types (int, float, etc.) if it has an
[158] Fix | Delete
# inherited __new__ unless a new __new__ is defined (or the resulting
[159] Fix | Delete
# class will fail).
[160] Fix | Delete
#
[161] Fix | Delete
# remove any keys listed in _ignore_
[162] Fix | Delete
classdict.setdefault('_ignore_', []).append('_ignore_')
[163] Fix | Delete
ignore = classdict['_ignore_']
[164] Fix | Delete
for key in ignore:
[165] Fix | Delete
classdict.pop(key, None)
[166] Fix | Delete
member_type, first_enum = metacls._get_mixins_(cls, bases)
[167] Fix | Delete
__new__, save_new, use_args = metacls._find_new_(
[168] Fix | Delete
classdict, member_type, first_enum,
[169] Fix | Delete
)
[170] Fix | Delete
[171] Fix | Delete
# save enum items into separate mapping so they don't get baked into
[172] Fix | Delete
# the new class
[173] Fix | Delete
enum_members = {k: classdict[k] for k in classdict._member_names}
[174] Fix | Delete
for name in classdict._member_names:
[175] Fix | Delete
del classdict[name]
[176] Fix | Delete
[177] Fix | Delete
# adjust the sunders
[178] Fix | Delete
_order_ = classdict.pop('_order_', None)
[179] Fix | Delete
[180] Fix | Delete
# check for illegal enum names (any others?)
[181] Fix | Delete
invalid_names = set(enum_members) & {'mro', ''}
[182] Fix | Delete
if invalid_names:
[183] Fix | Delete
raise ValueError('Invalid enum member name: {0}'.format(
[184] Fix | Delete
','.join(invalid_names)))
[185] Fix | Delete
[186] Fix | Delete
# create a default docstring if one has not been provided
[187] Fix | Delete
if '__doc__' not in classdict:
[188] Fix | Delete
classdict['__doc__'] = 'An enumeration.'
[189] Fix | Delete
[190] Fix | Delete
# create our new Enum type
[191] Fix | Delete
enum_class = super().__new__(metacls, cls, bases, classdict)
[192] Fix | Delete
enum_class._member_names_ = [] # names in definition order
[193] Fix | Delete
enum_class._member_map_ = {} # name->value map
[194] Fix | Delete
enum_class._member_type_ = member_type
[195] Fix | Delete
[196] Fix | Delete
# save DynamicClassAttribute attributes from super classes so we know
[197] Fix | Delete
# if we can take the shortcut of storing members in the class dict
[198] Fix | Delete
dynamic_attributes = {
[199] Fix | Delete
k for c in enum_class.mro()
[200] Fix | Delete
for k, v in c.__dict__.items()
[201] Fix | Delete
if isinstance(v, DynamicClassAttribute)
[202] Fix | Delete
}
[203] Fix | Delete
[204] Fix | Delete
# Reverse value->name map for hashable values.
[205] Fix | Delete
enum_class._value2member_map_ = {}
[206] Fix | Delete
[207] Fix | Delete
# If a custom type is mixed into the Enum, and it does not know how
[208] Fix | Delete
# to pickle itself, pickle.dumps will succeed but pickle.loads will
[209] Fix | Delete
# fail. Rather than have the error show up later and possibly far
[210] Fix | Delete
# from the source, sabotage the pickle protocol for this class so
[211] Fix | Delete
# that pickle.dumps also fails.
[212] Fix | Delete
#
[213] Fix | Delete
# However, if the new class implements its own __reduce_ex__, do not
[214] Fix | Delete
# sabotage -- it's on them to make sure it works correctly. We use
[215] Fix | Delete
# __reduce_ex__ instead of any of the others as it is preferred by
[216] Fix | Delete
# pickle over __reduce__, and it handles all pickle protocols.
[217] Fix | Delete
if '__reduce_ex__' not in classdict:
[218] Fix | Delete
if member_type is not object:
[219] Fix | Delete
methods = ('__getnewargs_ex__', '__getnewargs__',
[220] Fix | Delete
'__reduce_ex__', '__reduce__')
[221] Fix | Delete
if not any(m in member_type.__dict__ for m in methods):
[222] Fix | Delete
_make_class_unpicklable(enum_class)
[223] Fix | Delete
[224] Fix | Delete
# instantiate them, checking for duplicates as we go
[225] Fix | Delete
# we instantiate first instead of checking for duplicates first in case
[226] Fix | Delete
# a custom __new__ is doing something funky with the values -- such as
[227] Fix | Delete
# auto-numbering ;)
[228] Fix | Delete
for member_name in classdict._member_names:
[229] Fix | Delete
value = enum_members[member_name]
[230] Fix | Delete
if not isinstance(value, tuple):
[231] Fix | Delete
args = (value, )
[232] Fix | Delete
else:
[233] Fix | Delete
args = value
[234] Fix | Delete
if member_type is tuple: # special case for tuple enums
[235] Fix | Delete
args = (args, ) # wrap it one more time
[236] Fix | Delete
if not use_args:
[237] Fix | Delete
enum_member = __new__(enum_class)
[238] Fix | Delete
if not hasattr(enum_member, '_value_'):
[239] Fix | Delete
enum_member._value_ = value
[240] Fix | Delete
else:
[241] Fix | Delete
enum_member = __new__(enum_class, *args)
[242] Fix | Delete
if not hasattr(enum_member, '_value_'):
[243] Fix | Delete
if member_type is object:
[244] Fix | Delete
enum_member._value_ = value
[245] Fix | Delete
else:
[246] Fix | Delete
enum_member._value_ = member_type(*args)
[247] Fix | Delete
value = enum_member._value_
[248] Fix | Delete
enum_member._name_ = member_name
[249] Fix | Delete
enum_member.__objclass__ = enum_class
[250] Fix | Delete
enum_member.__init__(*args)
[251] Fix | Delete
# If another member with the same value was already defined, the
[252] Fix | Delete
# new member becomes an alias to the existing one.
[253] Fix | Delete
for name, canonical_member in enum_class._member_map_.items():
[254] Fix | Delete
if canonical_member._value_ == enum_member._value_:
[255] Fix | Delete
enum_member = canonical_member
[256] Fix | Delete
break
[257] Fix | Delete
else:
[258] Fix | Delete
# Aliases don't appear in member names (only in __members__).
[259] Fix | Delete
enum_class._member_names_.append(member_name)
[260] Fix | Delete
# performance boost for any member that would not shadow
[261] Fix | Delete
# a DynamicClassAttribute
[262] Fix | Delete
if member_name not in dynamic_attributes:
[263] Fix | Delete
setattr(enum_class, member_name, enum_member)
[264] Fix | Delete
# now add to _member_map_
[265] Fix | Delete
enum_class._member_map_[member_name] = enum_member
[266] Fix | Delete
try:
[267] Fix | Delete
# This may fail if value is not hashable. We can't add the value
[268] Fix | Delete
# to the map, and by-value lookups for this value will be
[269] Fix | Delete
# linear.
[270] Fix | Delete
enum_class._value2member_map_[value] = enum_member
[271] Fix | Delete
except TypeError:
[272] Fix | Delete
pass
[273] Fix | Delete
[274] Fix | Delete
# double check that repr and friends are not the mixin's or various
[275] Fix | Delete
# things break (such as pickle)
[276] Fix | Delete
# however, if the method is defined in the Enum itself, don't replace
[277] Fix | Delete
# it
[278] Fix | Delete
for name in ('__repr__', '__str__', '__format__', '__reduce_ex__'):
[279] Fix | Delete
if name in classdict:
[280] Fix | Delete
continue
[281] Fix | Delete
class_method = getattr(enum_class, name)
[282] Fix | Delete
obj_method = getattr(member_type, name, None)
[283] Fix | Delete
enum_method = getattr(first_enum, name, None)
[284] Fix | Delete
if obj_method is not None and obj_method is class_method:
[285] Fix | Delete
setattr(enum_class, name, enum_method)
[286] Fix | Delete
[287] Fix | Delete
# replace any other __new__ with our own (as long as Enum is not None,
[288] Fix | Delete
# anyway) -- again, this is to support pickle
[289] Fix | Delete
if Enum is not None:
[290] Fix | Delete
# if the user defined their own __new__, save it before it gets
[291] Fix | Delete
# clobbered in case they subclass later
[292] Fix | Delete
if save_new:
[293] Fix | Delete
enum_class.__new_member__ = __new__
[294] Fix | Delete
enum_class.__new__ = Enum.__new__
[295] Fix | Delete
[296] Fix | Delete
# py3 support for definition order (helps keep py2/py3 code in sync)
[297] Fix | Delete
if _order_ is not None:
[298] Fix | Delete
if isinstance(_order_, str):
[299] Fix | Delete
_order_ = _order_.replace(',', ' ').split()
[300] Fix | Delete
if _order_ != enum_class._member_names_:
[301] Fix | Delete
raise TypeError('member order does not match _order_')
[302] Fix | Delete
[303] Fix | Delete
return enum_class
[304] Fix | Delete
[305] Fix | Delete
def __bool__(self):
[306] Fix | Delete
"""
[307] Fix | Delete
classes/types should always be True.
[308] Fix | Delete
"""
[309] Fix | Delete
return True
[310] Fix | Delete
[311] Fix | Delete
def __call__(cls, value, names=None, *, module=None, qualname=None, type=None, start=1):
[312] Fix | Delete
"""
[313] Fix | Delete
Either returns an existing member, or creates a new enum class.
[314] Fix | Delete
[315] Fix | Delete
This method is used both when an enum class is given a value to match
[316] Fix | Delete
to an enumeration member (i.e. Color(3)) and for the functional API
[317] Fix | Delete
(i.e. Color = Enum('Color', names='RED GREEN BLUE')).
[318] Fix | Delete
[319] Fix | Delete
When used for the functional API:
[320] Fix | Delete
[321] Fix | Delete
`value` will be the name of the new class.
[322] Fix | Delete
[323] Fix | Delete
`names` should be either a string of white-space/comma delimited names
[324] Fix | Delete
(values will start at `start`), or an iterator/mapping of name, value pairs.
[325] Fix | Delete
[326] Fix | Delete
`module` should be set to the module this class is being created in;
[327] Fix | Delete
if it is not set, an attempt to find that module will be made, but if
[328] Fix | Delete
it fails the class will not be picklable.
[329] Fix | Delete
[330] Fix | Delete
`qualname` should be set to the actual location this class can be found
[331] Fix | Delete
at in its module; by default it is set to the global scope. If this is
[332] Fix | Delete
not correct, unpickling will fail in some circumstances.
[333] Fix | Delete
[334] Fix | Delete
`type`, if set, will be mixed in as the first base class.
[335] Fix | Delete
"""
[336] Fix | Delete
if names is None: # simple value lookup
[337] Fix | Delete
return cls.__new__(cls, value)
[338] Fix | Delete
# otherwise, functional API: we're creating a new Enum type
[339] Fix | Delete
return cls._create_(
[340] Fix | Delete
value,
[341] Fix | Delete
names,
[342] Fix | Delete
module=module,
[343] Fix | Delete
qualname=qualname,
[344] Fix | Delete
type=type,
[345] Fix | Delete
start=start,
[346] Fix | Delete
)
[347] Fix | Delete
[348] Fix | Delete
def __contains__(cls, member):
[349] Fix | Delete
if not isinstance(member, Enum):
[350] Fix | Delete
raise TypeError(
[351] Fix | Delete
"unsupported operand type(s) for 'in': '%s' and '%s'" % (
[352] Fix | Delete
type(member).__qualname__, cls.__class__.__qualname__))
[353] Fix | Delete
return isinstance(member, cls) and member._name_ in cls._member_map_
[354] Fix | Delete
[355] Fix | Delete
def __delattr__(cls, attr):
[356] Fix | Delete
# nicer error message when someone tries to delete an attribute
[357] Fix | Delete
# (see issue19025).
[358] Fix | Delete
if attr in cls._member_map_:
[359] Fix | Delete
raise AttributeError("%s: cannot delete Enum member." % cls.__name__)
[360] Fix | Delete
super().__delattr__(attr)
[361] Fix | Delete
[362] Fix | Delete
def __dir__(self):
[363] Fix | Delete
return (
[364] Fix | Delete
['__class__', '__doc__', '__members__', '__module__']
[365] Fix | Delete
+ self._member_names_
[366] Fix | Delete
)
[367] Fix | Delete
[368] Fix | Delete
def __getattr__(cls, name):
[369] Fix | Delete
"""
[370] Fix | Delete
Return the enum member matching `name`
[371] Fix | Delete
[372] Fix | Delete
We use __getattr__ instead of descriptors or inserting into the enum
[373] Fix | Delete
class' __dict__ in order to support `name` and `value` being both
[374] Fix | Delete
properties for enum members (which live in the class' __dict__) and
[375] Fix | Delete
enum members themselves.
[376] Fix | Delete
"""
[377] Fix | Delete
if _is_dunder(name):
[378] Fix | Delete
raise AttributeError(name)
[379] Fix | Delete
try:
[380] Fix | Delete
return cls._member_map_[name]
[381] Fix | Delete
except KeyError:
[382] Fix | Delete
raise AttributeError(name) from None
[383] Fix | Delete
[384] Fix | Delete
def __getitem__(cls, name):
[385] Fix | Delete
return cls._member_map_[name]
[386] Fix | Delete
[387] Fix | Delete
def __iter__(cls):
[388] Fix | Delete
"""
[389] Fix | Delete
Returns members in definition order.
[390] Fix | Delete
"""
[391] Fix | Delete
return (cls._member_map_[name] for name in cls._member_names_)
[392] Fix | Delete
[393] Fix | Delete
def __len__(cls):
[394] Fix | Delete
return len(cls._member_names_)
[395] Fix | Delete
[396] Fix | Delete
@property
[397] Fix | Delete
def __members__(cls):
[398] Fix | Delete
"""
[399] Fix | Delete
Returns a mapping of member name->value.
[400] Fix | Delete
[401] Fix | Delete
This mapping lists all enum members, including aliases. Note that this
[402] Fix | Delete
is a read-only view of the internal mapping.
[403] Fix | Delete
"""
[404] Fix | Delete
return MappingProxyType(cls._member_map_)
[405] Fix | Delete
[406] Fix | Delete
def __repr__(cls):
[407] Fix | Delete
return "<enum %r>" % cls.__name__
[408] Fix | Delete
[409] Fix | Delete
def __reversed__(cls):
[410] Fix | Delete
"""
[411] Fix | Delete
Returns members in reverse definition order.
[412] Fix | Delete
"""
[413] Fix | Delete
return (cls._member_map_[name] for name in reversed(cls._member_names_))
[414] Fix | Delete
[415] Fix | Delete
def __setattr__(cls, name, value):
[416] Fix | Delete
"""
[417] Fix | Delete
Block attempts to reassign Enum members.
[418] Fix | Delete
[419] Fix | Delete
A simple assignment to the class namespace only changes one of the
[420] Fix | Delete
several possible ways to get an Enum member from the Enum class,
[421] Fix | Delete
resulting in an inconsistent Enumeration.
[422] Fix | Delete
"""
[423] Fix | Delete
member_map = cls.__dict__.get('_member_map_', {})
[424] Fix | Delete
if name in member_map:
[425] Fix | Delete
raise AttributeError('Cannot reassign members.')
[426] Fix | Delete
super().__setattr__(name, value)
[427] Fix | Delete
[428] Fix | Delete
def _create_(cls, class_name, names, *, module=None, qualname=None, type=None, start=1):
[429] Fix | Delete
"""
[430] Fix | Delete
Convenience method to create a new Enum class.
[431] Fix | Delete
[432] Fix | Delete
`names` can be:
[433] Fix | Delete
[434] Fix | Delete
* A string containing member names, separated either with spaces or
[435] Fix | Delete
commas. Values are incremented by 1 from `start`.
[436] Fix | Delete
* An iterable of member names. Values are incremented by 1 from `start`.
[437] Fix | Delete
* An iterable of (member name, value) pairs.
[438] Fix | Delete
* A mapping of member name -> value pairs.
[439] Fix | Delete
"""
[440] Fix | Delete
metacls = cls.__class__
[441] Fix | Delete
bases = (cls, ) if type is None else (type, cls)
[442] Fix | Delete
_, first_enum = cls._get_mixins_(cls, bases)
[443] Fix | Delete
classdict = metacls.__prepare__(class_name, bases)
[444] Fix | Delete
[445] Fix | Delete
# special processing needed for names?
[446] Fix | Delete
if isinstance(names, str):
[447] Fix | Delete
names = names.replace(',', ' ').split()
[448] Fix | Delete
if isinstance(names, (tuple, list)) and names and isinstance(names[0], str):
[449] Fix | Delete
original_names, names = names, []
[450] Fix | Delete
last_values = []
[451] Fix | Delete
for count, name in enumerate(original_names):
[452] Fix | Delete
value = first_enum._generate_next_value_(name, start, count, last_values[:])
[453] Fix | Delete
last_values.append(value)
[454] Fix | Delete
names.append((name, value))
[455] Fix | Delete
[456] Fix | Delete
# Here, names is either an iterable of (name, value) or a mapping.
[457] Fix | Delete
for item in names:
[458] Fix | Delete
if isinstance(item, str):
[459] Fix | Delete
member_name, member_value = item, names[item]
[460] Fix | Delete
else:
[461] Fix | Delete
member_name, member_value = item
[462] Fix | Delete
classdict[member_name] = member_value
[463] Fix | Delete
enum_class = metacls.__new__(metacls, class_name, bases, classdict)
[464] Fix | Delete
[465] Fix | Delete
# TODO: replace the frame hack if a blessed way to know the calling
[466] Fix | Delete
# module is ever developed
[467] Fix | Delete
if module is None:
[468] Fix | Delete
try:
[469] Fix | Delete
module = sys._getframe(2).f_globals['__name__']
[470] Fix | Delete
except (AttributeError, ValueError, KeyError) as exc:
[471] Fix | Delete
pass
[472] Fix | Delete
if module is None:
[473] Fix | Delete
_make_class_unpicklable(enum_class)
[474] Fix | Delete
else:
[475] Fix | Delete
enum_class.__module__ = module
[476] Fix | Delete
if qualname is not None:
[477] Fix | Delete
enum_class.__qualname__ = qualname
[478] Fix | Delete
[479] Fix | Delete
return enum_class
[480] Fix | Delete
[481] Fix | Delete
def _convert_(cls, name, module, filter, source=None):
[482] Fix | Delete
"""
[483] Fix | Delete
Create a new Enum subclass that replaces a collection of global constants
[484] Fix | Delete
"""
[485] Fix | Delete
# convert all constants from source (or module) that pass filter() to
[486] Fix | Delete
# a new Enum called name, and export the enum and its members back to
[487] Fix | Delete
# module;
[488] Fix | Delete
# also, replace the __reduce_ex__ method so unpickling works in
[489] Fix | Delete
# previous Python versions
[490] Fix | Delete
module_globals = vars(sys.modules[module])
[491] Fix | Delete
if source:
[492] Fix | Delete
source = vars(source)
[493] Fix | Delete
else:
[494] Fix | Delete
source = module_globals
[495] Fix | Delete
# _value2member_map_ is populated in the same order every time
[496] Fix | Delete
# for a consistent reverse mapping of number to name when there
[497] Fix | Delete
# are multiple names for the same number.
[498] Fix | Delete
members = [
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function