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)
| 777 | |
| 778 | |
| 779 | def 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 | |
| 803 | def generate_new_for_class( |
no test coverage detected
searching dependent graphs…