(
cl: ClassIR,
func_name: str,
vtable_name: str,
setup_name: str,
init_fn: FuncIR | None,
emitter: Emitter,
)
| 801 | |
| 802 | |
| 803 | def generate_new_for_class( |
| 804 | cl: ClassIR, |
| 805 | func_name: str, |
| 806 | vtable_name: str, |
| 807 | setup_name: str, |
| 808 | init_fn: FuncIR | None, |
| 809 | emitter: Emitter, |
| 810 | ) -> None: |
| 811 | emitter.emit_line("static PyObject *") |
| 812 | emitter.emit_line(f"{func_name}(PyTypeObject *type, PyObject *args, PyObject *kwds)") |
| 813 | emitter.emit_line("{") |
| 814 | # TODO: Check and unbox arguments |
| 815 | if not cl.allow_interpreted_subclasses: |
| 816 | emitter.emit_line(f"if (type != {emitter.type_struct_name(cl)}) {{") |
| 817 | emitter.emit_line( |
| 818 | 'PyErr_SetString(PyExc_TypeError, "interpreted classes cannot inherit from compiled");' |
| 819 | ) |
| 820 | emitter.emit_line("return NULL;") |
| 821 | emitter.emit_line("}") |
| 822 | |
| 823 | type_arg = "(PyObject*)type" |
| 824 | new_args = "args, kwds" |
| 825 | emit_setup_or_dunder_new_call(cl, setup_name, type_arg, False, new_args, emitter) |
| 826 | if ( |
| 827 | not init_fn |
| 828 | or cl.allow_interpreted_subclasses |
| 829 | or cl.builtin_base |
| 830 | or cl.is_serializable() |
| 831 | or cl.has_method("__new__") |
| 832 | ): |
| 833 | # Match Python semantics -- __new__ doesn't call __init__. |
| 834 | emitter.emit_line("return self;") |
| 835 | else: |
| 836 | # __new__ of a native class implicitly calls __init__ so that we |
| 837 | # can enforce that instances are always properly initialized. This |
| 838 | # is needed to support always defined attributes. |
| 839 | emitter.emit_line( |
| 840 | f"PyObject *ret = {emitter.wrapper_function_call(init_fn.decl)}(self, args, kwds);" |
| 841 | ) |
| 842 | emitter.emit_lines("if (ret == NULL) {", " Py_DECREF(self);", " return NULL;", "}") |
| 843 | emitter.emit_line("Py_DECREF(ret);") |
| 844 | emitter.emit_line("return self;") |
| 845 | emitter.emit_line("}") |
| 846 | |
| 847 | |
| 848 | def generate_new_for_trait(cl: ClassIR, func_name: str, emitter: Emitter) -> None: |
no test coverage detected
searching dependent graphs…