MCPcopy Index your code
hub / github.com/python/mypy / allocate_class

Function allocate_class

mypyc/irbuild/classdef.py:469–532  ·  view source on GitHub ↗
(builder: IRBuilder, cdef: ClassDef)

Source from the content-addressed store, hash-verified

467
468
469def allocate_class(builder: IRBuilder, cdef: ClassDef) -> Value:
470 # OK AND NOW THE FUN PART
471 base_exprs = cdef.base_type_exprs + cdef.removed_base_type_exprs
472 new_style_type_args = cdef.type_args
473 if new_style_type_args:
474 bases = [make_generic_base_class(builder, cdef.fullname, new_style_type_args, cdef.line)]
475 else:
476 bases = []
477
478 if base_exprs or new_style_type_args:
479 bases.extend([builder.accept(x) for x in base_exprs])
480 tp_bases = builder.new_tuple(bases, cdef.line)
481 else:
482 tp_bases = builder.add(LoadErrorValue(object_rprimitive, is_borrowed=True))
483 modname = builder.load_str(builder.module_name)
484 template = builder.add(
485 LoadStatic(object_rprimitive, cdef.name + "_template", builder.module_name, NAMESPACE_TYPE)
486 )
487 # Create the class
488 tp = builder.call_c(pytype_from_template_op, [template, tp_bases, modname], cdef.line)
489
490 # Set type object to be immortal if free threaded, as otherwise reference count contention
491 # can cause a big performance hit.
492 builder.set_immortal_if_free_threaded(tp, cdef.line)
493
494 # Immediately fix up the trait vtables, before doing anything with the class.
495 ir = builder.mapper.type_to_ir[cdef.info]
496 if not ir.is_trait and not ir.builtin_base:
497 builder.add(
498 Call(
499 FuncDecl(
500 cdef.name + "_trait_vtable_setup",
501 None,
502 builder.module_name,
503 FuncSignature([], bool_rprimitive),
504 ),
505 [],
506 cdef.line,
507 )
508 )
509 builder.add_coroutine_setup_call(cdef.name, tp)
510
511 # Populate a '__mypyc_attrs__' field containing the list of attrs
512 builder.primitive_op(
513 py_setattr_op,
514 [
515 tp,
516 builder.load_str("__mypyc_attrs__"),
517 create_mypyc_attrs_tuple(builder, builder.mapper.type_to_ir[cdef.info], cdef.line),
518 ],
519 cdef.line,
520 )
521
522 # Save the class
523 builder.add(InitStatic(tp, cdef.name, builder.module_name, NAMESPACE_TYPE))
524
525 # Add it to the dict
526 builder.call_c(

Callers 1

__init__Method · 0.85

Calls 15

LoadErrorValueClass · 0.90
LoadStaticClass · 0.90
CallClass · 0.90
FuncDeclClass · 0.90
FuncSignatureClass · 0.90
InitStaticClass · 0.90
make_generic_base_classFunction · 0.85
create_mypyc_attrs_tupleFunction · 0.85
extendMethod · 0.80
load_globals_dictMethod · 0.80
acceptMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…