MCPcopy Index your code
hub / github.com/RustPython/RustPython / __set_name__

Method __set_name__

Lib/enum.py:239–324  ·  view source on GitHub ↗

convert each quasi-member into an instance of the new enum class

(self, enum_class, member_name)

Source from the content-addressed store, hash-verified

237 self.value = value
238
239 def __set_name__(self, enum_class, member_name):
240 """
241 convert each quasi-member into an instance of the new enum class
242 """
243 # first step: remove ourself from enum_class
244 delattr(enum_class, member_name)
245 # second step: create member based on enum_class
246 value = self.value
247 if not isinstance(value, tuple):
248 args = (value, )
249 else:
250 args = value
251 if enum_class._member_type_ is tuple: # special case for tuple enums
252 args = (args, ) # wrap it one more time
253 if not enum_class._use_args_:
254 enum_member = enum_class._new_member_(enum_class)
255 else:
256 enum_member = enum_class._new_member_(enum_class, *args)
257 if not hasattr(enum_member, '_value_'):
258 if enum_class._member_type_ is object:
259 enum_member._value_ = value
260 else:
261 try:
262 enum_member._value_ = enum_class._member_type_(*args)
263 except Exception as exc:
264 new_exc = TypeError(
265 '_value_ not set in __new__, unable to create it'
266 )
267 new_exc.__cause__ = exc
268 raise new_exc
269 value = enum_member._value_
270 enum_member._name_ = member_name
271 enum_member.__objclass__ = enum_class
272 enum_member.__init__(*args)
273 enum_member._sort_order_ = len(enum_class._member_names_)
274
275 if Flag is not None and issubclass(enum_class, Flag):
276 if isinstance(value, int):
277 enum_class._flag_mask_ |= value
278 if _is_single_bit(value):
279 enum_class._singles_mask_ |= value
280 enum_class._all_bits_ = 2 ** ((enum_class._flag_mask_).bit_length()) - 1
281
282 # If another member with the same value was already defined, the
283 # new member becomes an alias to the existing one.
284 try:
285 try:
286 # try to do a fast lookup to avoid the quadratic loop
287 enum_member = enum_class._value2member_map_[value]
288 except TypeError:
289 for name, canonical_member in enum_class._member_map_.items():
290 if canonical_member._value_ == value:
291 enum_member = canonical_member
292 break
293 else:
294 raise KeyError
295 except KeyError:
296 # this could still be an alias if the value is multi-bit and the

Callers

nothing calls this directly

Calls 12

delattrFunction · 0.85
isinstanceFunction · 0.85
hasattrFunction · 0.85
lenFunction · 0.85
issubclassFunction · 0.85
_is_single_bitFunction · 0.85
_add_member_Method · 0.80
__init__Method · 0.45
bit_lengthMethod · 0.45
itemsMethod · 0.45
appendMethod · 0.45
setdefaultMethod · 0.45

Tested by

no test coverage detected