Skip to content

Commit 499389f

Browse files
committed
Address more review comments
1 parent 98d7abb commit 499389f

3 files changed

Lines changed: 54 additions & 29 deletions

File tree

mypyc/irbuild/specialize.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
Var,
3535
)
3636
from mypy.types import AnyType, TypeOfAny
37+
from mypyc.ir.class_ir import all_concrete_classes
3738
from mypyc.ir.ops import (
3839
BasicBlock,
40+
Call,
3941
Extend,
4042
Integer,
4143
PrimitiveDescription,
@@ -1102,6 +1104,9 @@ def translate_object_new(builder: IRBuilder, expr: CallExpr, callee: RefExpr) ->
11021104
method_args = fn.fitem.arg_names
11031105
if isinstance(typ_arg, NameExpr) and len(method_args) > 0 and method_args[0] == typ_arg.name:
11041106
subtype = builder.accept(expr.args[0])
1107+
classes = all_concrete_classes(ir)
1108+
if classes and len(classes) == 1:
1109+
return builder.add(Call(ir.setup, [subtype], expr.line))
11051110
# Call a function that dynamically resolves the setup function of extension classes from the type object.
11061111
# This is necessary because the setup involves default attribute initialization and setting up
11071112
# the vtable which are specific to a given type and will not work if a subtype is created using

mypyc/lib-rt/generic_ops.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ PyObject *CPy_SetupObject(PyObject *type) {
7979
}
8080
}
8181
if (!def || !def->ml_name) {
82-
PyErr_SetString(PyExc_RuntimeError, "Internal error: Unable to find object setup function");
82+
PyErr_SetString(PyExc_RuntimeError, "Internal mypyc error: Unable to find object setup function");
8383
return NULL;
8484
}
8585

mypyc/test-data/irbuild-classes.test

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,13 @@ class Test:
16851685
obj.val = val
16861686
return obj
16871687

1688+
class Test2:
1689+
def __new__(cls) -> Test2:
1690+
return super().__new__(cls)
1691+
1692+
class Sub(Test2):
1693+
pass
1694+
16881695
def fn() -> Test:
16891696
return Test.__new__(Test, 42)
16901697

@@ -1712,15 +1719,20 @@ class NotTransformed:
17121719
def Test.__new__(cls, val):
17131720
cls :: object
17141721
val :: int
1715-
r0 :: object
1716-
r1, obj :: __main__.Test
1717-
r2 :: bool
1722+
r0, obj :: __main__.Test
1723+
r1 :: bool
17181724
L0:
1719-
r0 = CPy_SetupObject(cls)
1720-
r1 = cast(__main__.Test, r0)
1721-
obj = r1
1722-
obj.val = val; r2 = is_error
1725+
r0 = __mypyc__Test_setup(cls)
1726+
obj = r0
1727+
obj.val = val; r1 = is_error
17231728
return obj
1729+
def Test2.__new__(cls):
1730+
cls, r0 :: object
1731+
r1 :: __main__.Test2
1732+
L0:
1733+
r0 = CPy_SetupObject(cls)
1734+
r1 = cast(__main__.Test2, r0)
1735+
return r1
17241736
def fn():
17251737
r0 :: object
17261738
r1 :: __main__.Test
@@ -1731,14 +1743,12 @@ L0:
17311743
def NewClassMethod.__new__(cls, val):
17321744
cls :: object
17331745
val :: int
1734-
r0 :: object
1735-
r1, obj :: __main__.NewClassMethod
1736-
r2 :: bool
1746+
r0, obj :: __main__.NewClassMethod
1747+
r1 :: bool
17371748
L0:
1738-
r0 = CPy_SetupObject(cls)
1739-
r1 = cast(__main__.NewClassMethod, r0)
1740-
obj = r1
1741-
obj.val = val; r2 = is_error
1749+
r0 = __mypyc__NewClassMethod_setup(cls)
1750+
obj = r0
1751+
obj.val = val; r1 = is_error
17421752
return obj
17431753
def fn2():
17441754
r0 :: object
@@ -1826,6 +1836,13 @@ class Test:
18261836
obj.val = val
18271837
return obj
18281838

1839+
class Test2:
1840+
def __new__(cls) -> Test2:
1841+
return object.__new__(cls)
1842+
1843+
class Sub(Test2):
1844+
pass
1845+
18291846
def fn() -> Test:
18301847
return Test.__new__(Test, 42)
18311848

@@ -1871,15 +1888,20 @@ def object_new_outside_class() -> None:
18711888
def Test.__new__(cls, val):
18721889
cls :: object
18731890
val :: int
1874-
r0 :: object
1875-
r1, obj :: __main__.Test
1876-
r2 :: bool
1891+
r0, obj :: __main__.Test
1892+
r1 :: bool
18771893
L0:
1878-
r0 = CPy_SetupObject(cls)
1879-
r1 = cast(__main__.Test, r0)
1880-
obj = r1
1881-
obj.val = val; r2 = is_error
1894+
r0 = __mypyc__Test_setup(cls)
1895+
obj = r0
1896+
obj.val = val; r1 = is_error
18821897
return obj
1898+
def Test2.__new__(cls):
1899+
cls, r0 :: object
1900+
r1 :: __main__.Test2
1901+
L0:
1902+
r0 = CPy_SetupObject(cls)
1903+
r1 = cast(__main__.Test2, r0)
1904+
return r1
18831905
def fn():
18841906
r0 :: object
18851907
r1 :: __main__.Test
@@ -1890,14 +1912,12 @@ L0:
18901912
def NewClassMethod.__new__(cls, val):
18911913
cls :: object
18921914
val :: int
1893-
r0 :: object
1894-
r1, obj :: __main__.NewClassMethod
1895-
r2 :: bool
1915+
r0, obj :: __main__.NewClassMethod
1916+
r1 :: bool
18961917
L0:
1897-
r0 = CPy_SetupObject(cls)
1898-
r1 = cast(__main__.NewClassMethod, r0)
1899-
obj = r1
1900-
obj.val = val; r2 = is_error
1918+
r0 = __mypyc__NewClassMethod_setup(cls)
1919+
obj = r0
1920+
obj.val = val; r1 = is_error
19011921
return obj
19021922
def fn2():
19031923
r0 :: object

0 commit comments

Comments
 (0)