Skip to content

Commit 56b8cbb

Browse files
committed
[mypyc] Move setting of __cpyfunction__ outside of constructor
1 parent 61b3b5d commit 56b8cbb

4 files changed

Lines changed: 23 additions & 22 deletions

File tree

mypyc/codegen/emitclass.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ def emit_line() -> None:
413413
emitter.emit_line()
414414
if generate_full:
415415
generate_setup_for_class(
416-
cl, defaults_fn, vtable_name, shadow_vtable_name, coroutine_setup_name, emitter
416+
cl, defaults_fn, vtable_name, shadow_vtable_name, emitter
417417
)
418418
emitter.emit_line()
419419
generate_constructor_for_class(cl, cl.ctor, init_fn, setup_name, vtable_name, emitter)
@@ -606,7 +606,6 @@ def generate_setup_for_class(
606606
defaults_fn: FuncIR | None,
607607
vtable_name: str,
608608
shadow_vtable_name: str | None,
609-
coroutine_setup_name: str,
610609
emitter: Emitter,
611610
) -> None:
612611
"""Generate a native function that allocates an instance of a class."""
@@ -662,13 +661,6 @@ def generate_setup_for_class(
662661
if defaults_fn is not None:
663662
emit_attr_defaults_func_call(defaults_fn, "self", emitter)
664663

665-
# Initialize function wrapper for callable classes. As opposed to regular functions,
666-
# each instance of a callable class needs its own wrapper because they might be instantiated
667-
# inside other functions.
668-
if cl.coroutine_name:
669-
emitter.emit_line(f"if ({NATIVE_PREFIX}{coroutine_setup_name}((PyObject *)self) != 1)")
670-
emitter.emit_line(" return NULL;")
671-
672664
emitter.emit_line("return (PyObject *)self;")
673665
emitter.emit_line("}")
674666

mypyc/irbuild/builder.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
Assign,
7070
BasicBlock,
7171
Branch,
72+
Call,
7273
ComparisonOp,
7374
GetAttr,
7475
InitStatic,
@@ -91,6 +92,7 @@
9192
RType,
9293
RUnion,
9394
bitmap_rprimitive,
95+
bool_rprimitive,
9496
bytes_rprimitive,
9597
c_pyssize_t_rprimitive,
9698
dict_rprimitive,
@@ -1460,6 +1462,20 @@ def add_function(self, func_ir: FuncIR, line: int) -> None:
14601462
def get_current_class_ir(self) -> ClassIR | None:
14611463
type_info = self.fn_info.fitem.info
14621464
return self.mapper.type_to_ir.get(type_info)
1465+
1466+
def add_coroutine_setup_call(self, class_name: str, obj: Value) -> Value:
1467+
return self.add(
1468+
Call(
1469+
FuncDecl(
1470+
class_name + "_coroutine_setup",
1471+
None,
1472+
self.module_name,
1473+
FuncSignature([RuntimeArg("type", object_rprimitive)], bool_rprimitive),
1474+
),
1475+
[obj],
1476+
-1,
1477+
)
1478+
)
14631479

14641480

14651481
def gen_arg_defaults(builder: IRBuilder) -> None:

mypyc/irbuild/callable_class.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,9 @@ def instantiate_callable_class(builder: IRBuilder, fn_info: FuncInfo) -> Value:
232232
curr_env_reg = builder.fn_info.curr_env_reg
233233
if curr_env_reg:
234234
builder.add(SetAttr(func_reg, ENV_ATTR_NAME, curr_env_reg, fitem.line))
235+
# Initialize function wrapper for callable classes. As opposed to regular functions,
236+
# each instance of a callable class needs its own wrapper because they might be instantiated
237+
# inside other functions.
238+
if not fn_info.in_non_ext and fn_info.is_coroutine:
239+
builder.add_coroutine_setup_call(fn_info.callable_class.ir.name, func_reg)
235240
return func_reg

mypyc/irbuild/classdef.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -473,19 +473,7 @@ def allocate_class(builder: IRBuilder, cdef: ClassDef) -> Value:
473473
-1,
474474
)
475475
)
476-
477-
builder.add(
478-
Call(
479-
FuncDecl(
480-
cdef.name + "_coroutine_setup",
481-
None,
482-
builder.module_name,
483-
FuncSignature([RuntimeArg("type", object_rprimitive)], bool_rprimitive),
484-
),
485-
[tp],
486-
-1,
487-
)
488-
)
476+
builder.add_coroutine_setup_call(cdef.name, tp)
489477

490478
# Populate a '__mypyc_attrs__' field containing the list of attrs
491479
builder.primitive_op(

0 commit comments

Comments
 (0)