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

Function generate_init_for_class

mypyc/codegen/emitclass.py:779–800  ·  view source on GitHub ↗

Generate an init function suitable for use as tp_init. tp_init needs to be a function that returns an int, and our __init__ methods return a PyObject. Translate NULL to -1, everything else to 0.

(cl: ClassIR, init_fn: FuncIR, emitter: Emitter)

Source from the content-addressed store, hash-verified

777
778
779def generate_init_for_class(cl: ClassIR, init_fn: FuncIR, emitter: Emitter) -> str:
780 """Generate an init function suitable for use as tp_init.
781
782 tp_init needs to be a function that returns an int, and our
783 __init__ methods return a PyObject. Translate NULL to -1,
784 everything else to 0.
785 """
786 func_name = f"{cl.name_prefix(emitter.names)}_init"
787
788 emitter.emit_line("static int")
789 emitter.emit_line(f"{func_name}(PyObject *self, PyObject *args, PyObject *kwds)")
790 emitter.emit_line("{")
791 if cl.allow_interpreted_subclasses or cl.builtin_base or cl.has_method("__new__"):
792 emitter.emit_line(
793 f"return {emitter.wrapper_function_call(init_fn.decl)}"
794 "(self, args, kwds) != NULL ? 0 : -1;"
795 )
796 else:
797 emitter.emit_line("return 0;")
798 emitter.emit_line("}")
799
800 return func_name
801
802
803def generate_new_for_class(

Callers 1

emitclass.pyFile · 0.85

Calls 4

name_prefixMethod · 0.80
has_methodMethod · 0.80
wrapper_function_callMethod · 0.80
emit_lineMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…