MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / __new__

Method __new__

tools/python-3.11.9-amd64/Lib/enum.py:1095–1149  ·  view source on GitHub ↗
(cls, value)

Source from the content-addressed store, hash-verified

1093 """
1094
1095 def __new__(cls, value):
1096 # all enum instances are actually created during class construction
1097 # without calling this method; this method is called by the metaclass'
1098 # __call__ (i.e. Color(3) ), and by pickle
1099 if type(value) is cls:
1100 # For lookups like Color(Color.RED)
1101 return value
1102 # by-value search for a matching enum member
1103 # see if it's in the reverse mapping (for hashable values)
1104 try:
1105 return cls._value2member_map_[value]
1106 except KeyError:
1107 # Not found, no need to do long O(n) search
1108 pass
1109 except TypeError:
1110 # not there, now do long search -- O(n) behavior
1111 for member in cls._member_map_.values():
1112 if member._value_ == value:
1113 return member
1114 # still not found -- verify that members exist, in-case somebody got here mistakenly
1115 # (such as via super when trying to override __new__)
1116 if not cls._member_map_:
1117 raise TypeError("%r has no members defined" % cls)
1118 #
1119 # still not found -- try _missing_ hook
1120 try:
1121 exc = None
1122 result = cls._missing_(value)
1123 except Exception as e:
1124 exc = e
1125 result = None
1126 try:
1127 if isinstance(result, cls):
1128 return result
1129 elif (
1130 Flag is not None and issubclass(cls, Flag)
1131 and cls._boundary_ is EJECT and isinstance(result, int)
1132 ):
1133 return result
1134 else:
1135 ve_exc = ValueError("%r is not a valid %s" % (value, cls.__qualname__))
1136 if result is None and exc is None:
1137 raise ve_exc
1138 elif exc is None:
1139 exc = TypeError(
1140 'error in %s._missing_: returned %r instead of None or a valid member'
1141 % (cls.__name__, result)
1142 )
1143 if not isinstance(exc, ValueError):
1144 exc.__context__ = ve_exc
1145 raise exc
1146 finally:
1147 # ensure all variables that could hold an exception are destroyed
1148 exc = None
1149 ve_exc = None
1150
1151 def __init__(self, *args, **kwds):
1152 pass

Callers 5

__new__Method · 0.45
__call__Method · 0.45
_create_Method · 0.45
__new__Method · 0.45
_missing_Method · 0.45

Calls 3

typeClass · 0.50
valuesMethod · 0.45
_missing_Method · 0.45

Tested by

no test coverage detected