(cdef: ClassDef, ir: ClassIR, module_name: str, mapper: Mapper)
| 673 | |
| 674 | |
| 675 | def prepare_init_method(cdef: ClassDef, ir: ClassIR, module_name: str, mapper: Mapper) -> None: |
| 676 | # Set up a constructor decl |
| 677 | init_node = cdef.info["__init__"].node |
| 678 | |
| 679 | new_node: SymbolNode | None = None |
| 680 | new_symbol = cdef.info.get("__new__") |
| 681 | # We are only interested in __new__ method defined in a user-defined class, |
| 682 | # so we ignore it if it comes from a builtin type. It's usually builtins.object |
| 683 | # but could also be builtins.type for metaclasses so we detect the prefix which |
| 684 | # matches both. |
| 685 | if new_symbol and new_symbol.fullname and not new_symbol.fullname.startswith("builtins."): |
| 686 | new_node = new_symbol.node |
| 687 | if isinstance(new_node, (Decorator, OverloadedFuncDef)): |
| 688 | new_node = get_func_def(new_node) |
| 689 | if not ir.is_trait and not ir.builtin_base and isinstance(init_node, FuncDef): |
| 690 | init_sig = mapper.fdef_to_sig(init_node, True) |
| 691 | args_match = True |
| 692 | if isinstance(new_node, FuncDef): |
| 693 | new_sig = mapper.fdef_to_sig(new_node, True) |
| 694 | args_match = check_matching_args(init_sig, new_sig) |
| 695 | |
| 696 | defining_ir = mapper.type_to_ir.get(init_node.info) |
| 697 | # If there is a nontrivial __init__ that wasn't defined in an |
| 698 | # extension class, we need to make the constructor take *args, |
| 699 | # **kwargs so it can call tp_init. |
| 700 | if ( |
| 701 | ( |
| 702 | defining_ir is None |
| 703 | or not defining_ir.is_ext_class |
| 704 | or cdef.info["__init__"].plugin_generated |
| 705 | ) |
| 706 | and init_node.info.fullname != "builtins.object" |
| 707 | ) or not args_match: |
| 708 | init_sig = FuncSignature( |
| 709 | [ |
| 710 | init_sig.args[0], |
| 711 | RuntimeArg("args", tuple_rprimitive, ARG_STAR), |
| 712 | RuntimeArg("kwargs", dict_rprimitive, ARG_STAR2), |
| 713 | ], |
| 714 | init_sig.ret_type, |
| 715 | ) |
| 716 | |
| 717 | last_arg = len(init_sig.args) - init_sig.num_bitmap_args |
| 718 | ctor_sig = FuncSignature(init_sig.args[1:last_arg], RInstance(ir)) |
| 719 | ir.ctor = FuncDecl(cdef.name, None, module_name, ctor_sig) |
| 720 | mapper.func_to_decl[cdef.info] = ir.ctor |
| 721 | |
| 722 | |
| 723 | def prepare_non_ext_class_def( |
no test coverage detected
searching dependent graphs…