diff --git a/mypyc/codegen/emitclass.py b/mypyc/codegen/emitclass.py index a9d973425aa1..4bc45030fc38 100644 --- a/mypyc/codegen/emitclass.py +++ b/mypyc/codegen/emitclass.py @@ -587,8 +587,6 @@ def generate_object_struct(cl: ClassIR, emitter: Emitter) -> None: # This must come before any field whose presence depends on the class, since # tp_weaklistoffset has to agree between a base class and its subclasses. lines.append("PyObject *weakreflist;") - if cl.has_method("__call__"): - lines.append("vectorcallfunc vectorcall;") # The flag affects attribute offsets, so concrete bases must agree on its presence. assert all( base.has_running_flag == cl.has_running_flag for base in cl.base_mro @@ -617,6 +615,14 @@ def generate_object_struct(cl: ClassIR, emitter: Emitter) -> None: if isinstance(rtype, RTuple): emitter.declare_tuple_struct(rtype) + if cl.has_method("__call__"): + # This goes after the attributes, since a subclass may define __call__ when a + # base class doesn't, and the attributes of the base class must be at the same + # offsets in the subclass. The field is only accessed through the struct of the + # class or through tp_vectorcall_offset, so its offset can differ between + # classes. + lines.append("vectorcallfunc vectorcall;") + lines.append(f"}} {cl.struct_name(emitter.names)};") lines.append("") emitter.context.declarations[cl.struct_name(emitter.names)] = HeaderDeclaration( diff --git a/mypyc/test-data/run-classes.test b/mypyc/test-data/run-classes.test index ee562eeff3d9..91a3803ffbce 100644 --- a/mypyc/test-data/run-classes.test +++ b/mypyc/test-data/run-classes.test @@ -4659,6 +4659,139 @@ class Derived(Base): def test_inherited() -> None: assert Derived()() == 1 +[case testClassWith__call__InSubclassOnly] +import gc +import weakref +from typing import Any +from mypy_extensions import i64, mypyc_attr + +class Base: + def __init__(self, x: str, n: i64) -> None: + self.x = x + self.n = n + self.b = True + + def get_x(self) -> str: + return self.x + + def get_n(self) -> i64: + return self.n + + def set_x(self, x: str) -> None: + self.x = x + +class CallSub(Base): + def __init__(self, x: str, n: i64) -> None: + super().__init__(x, n) + self.y = "y" + + def __call__(self, a: int) -> str: + return f"{self.x}:{self.n}:{self.y}:{a}" + +class CallSubSub(CallSub): + def __init__(self, x: str, n: i64) -> None: + super().__init__(x, n) + self.z = "z" + + def __call__(self, a: int) -> str: + return f"{self.x}:{self.y}:{self.z}:{a}" + +class Mid(Base): + def __init__(self, x: str, n: i64) -> None: + super().__init__(x, n) + self.m = "m" + +class CallSubOfMid(Mid): + def __call__(self) -> str: + return self.x + self.m + +@mypyc_attr(weakrefs=True) +class WeakBase: + def __init__(self, x: str) -> None: + self.x = x + + def get_x(self) -> str: + return self.x + +class WeakCallSub(WeakBase): + def __init__(self, x: str) -> None: + super().__init__(x) + self.y = "y" + + def __call__(self) -> str: + return self.x + self.y + +@mypyc_attr(allow_interpreted_subclasses=True) +class OpenBase: + def __init__(self, x: str) -> None: + self.x = x + + def get_x(self) -> str: + return self.x + +@mypyc_attr(allow_interpreted_subclasses=True) +class OpenCallSub(OpenBase): + def __init__(self, x: str) -> None: + super().__init__(x) + self.y = "y" + + def __call__(self) -> str: + return self.x + self.y + +def get_x(b: Base) -> str: + return b.x + +def test_subclass_defines_call() -> None: + c = CallSub("x", 5) + assert c.get_x() == "x" + assert c.get_n() == 5 + assert get_x(c) == "x" + assert c.x == "x" and c.n == 5 and c.b and c.y == "y" + c.set_x("xx") + assert c.x == "xx" + assert c(1) == "xx:5:y:1" + o: Any = c + assert o(2) == "xx:5:y:2" + assert o.get_x() == "xx" + +def test_call_overridden_in_subclass() -> None: + c = CallSubSub("x", -3) + assert c.get_x() == "x" and c.get_n() == -3 and get_x(c) == "x" + assert c.y == "y" and c.z == "z" + assert c(1) == "x:y:z:1" + s: CallSub = c + assert s(2) == "x:y:z:2" + o: Any = c + assert o(3) == "x:y:z:3" + +def test_call_in_indirect_subclass() -> None: + c = CallSubOfMid("x", 7) + assert c.get_x() == "x" and c.get_n() == 7 and get_x(c) == "x" + assert c.m == "m" + assert c() == "xm" + +def test_call_with_weakrefs() -> None: + c = WeakCallSub("x") + assert c.get_x() == "x" and c.y == "y" + assert c() == "xy" + r = weakref.ref(c) + assert r() is c + del c + gc.collect() + assert r() is None + +def test_interpreted_subclass() -> None: + type2: Any = type + Sub = type2("Sub", (OpenCallSub,), {}) + c = Sub("x") + c.z = "z" + assert c.get_x() == "x" and c.y == "y" and c.z == "z" + assert c() == "xy" + CallSub2 = type2("CallSub2", (OpenBase,), {"__call__": lambda self: self.get_x() + "!"}) + d = CallSub2("x") + assert d.get_x() == "x" + assert d() == "x!" + [case testCallTypeObjectOfNativeClass] from abc import ABC, abstractmethod from typing import Any, overload diff --git a/mypyc/test-data/run-multimodule.test b/mypyc/test-data/run-multimodule.test index 3c8b4ceb9995..5dc8151fd3fe 100644 --- a/mypyc/test-data/run-multimodule.test +++ b/mypyc/test-data/run-multimodule.test @@ -617,6 +617,37 @@ class ActionBase(argparse.Action): super().__init__(["-x"], "x") self.x = 1 +[case testMultiModuleCallInSubclassOnly] +# The base class is in another group in separate compilation mode, and only the +# subclass defines __call__. +from other_call_base import CallBase, call_base_x + +class CallSub(CallBase): + def __init__(self, x: str) -> None: + super().__init__(x) + self.y = "y" + + def __call__(self, a: int) -> str: + return self.x + self.y + str(a) + +def test_call_in_subclass() -> None: + c = CallSub("x") + assert c.get_x() == "x" + assert call_base_x(c) == "x" + assert c.x == "x" and c.y == "y" + assert c(1) == "xy1" + +[file other_call_base.py] +class CallBase: + def __init__(self, x: str) -> None: + self.x = x + + def get_x(self) -> str: + return self.x + +def call_base_x(b: CallBase) -> str: + return b.x + [case testNonNativeImportInPackageFile] # The import is really non-native only in separate compilation mode where __init__.py and # other_cache.py are in different libraries and the import uses the standard Python procedure.