Skip to content

Commit 23a719f

Browse files
authored
[mypyc] Fix some deserialization related issues (python#7989)
Certain parts of the mypy AST aren't present and we were relying on them anyways. This caused a crash when subclassing and classes not making it into the type table, preventing using fast paths.
1 parent 6cc6c66 commit 23a719f

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

mypyc/genops.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,11 @@ def load_type_map(mapper: 'Mapper',
162162
deser_ctx: DeserMaps) -> None:
163163
"""Populate a Mapper with deserialized IR from a list of modules."""
164164
for module in modules:
165-
for node in module.defs:
166-
if isinstance(node, ClassDef):
167-
mapper.type_to_ir[node.info] = deser_ctx.classes[node.fullname]
165+
for name, node in module.names.items():
166+
if isinstance(node.node, TypeInfo):
167+
ir = deser_ctx.classes[node.node.fullname]
168+
mapper.type_to_ir[node.node] = ir
169+
mapper.func_to_decl[node.node] = ir.ctor
168170

169171
for module in modules:
170172
for func in get_module_func_defs(module):
@@ -457,8 +459,8 @@ def fdef_to_sig(self, fdef: FuncDef) -> FuncSignature:
457459
arg_types = [object_rprimitive for arg in fdef.arguments]
458460
ret = object_rprimitive
459461

460-
args = [RuntimeArg(arg.variable.name, arg_type, arg.kind)
461-
for arg, arg_type in zip(fdef.arguments, arg_types)]
462+
args = [RuntimeArg(arg_name, arg_type, arg_kind)
463+
for arg_name, arg_kind, arg_type in zip(fdef.arg_names, fdef.arg_kinds, arg_types)]
462464

463465
# We force certain dunder methods to return objects to support letting them
464466
# return NotImplemented. It also avoids some pointless boxing and unboxing,

mypyc/test-data/run-multimodule.test

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,41 @@ except TypeError:
3030
else:
3131
assert False
3232

33+
[case testMultiModuleFastpaths]
34+
[file other_main.py]
35+
36+
[file other_main.py.2]
37+
from other_b import A, func
38+
39+
class B(A):
40+
pass
41+
42+
def test() -> None:
43+
a = A()
44+
assert func() == 12
45+
assert a.method() == "test"
46+
47+
test()
48+
49+
[file other_b.py]
50+
class A:
51+
def method(self) -> str:
52+
return "test"
53+
54+
def func() -> int:
55+
return 12
56+
57+
# Remove all the methods and functions from globals to ensure that
58+
# they get called via the fastpaths even when doing incremental
59+
# compilation.
60+
setattr(A, 'method', None)
61+
setattr(A, '__init__', None)
62+
globals()['func'] = None
63+
globals()['A'] = None
64+
65+
[file driver.py]
66+
import other_main
67+
3368
[case testMultiModuleSameNames]
3469
# Use same names in both modules
3570
import other

mypyc/test/test_run.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,10 @@ def run_case_step(self, testcase: DataDrivenTestCase, incremental_step: int) ->
224224
print(line)
225225
assert False, 'Compile error'
226226

227-
# Check that serialization works on this IR
228-
check_serialization_roundtrip(ir)
227+
# Check that serialization works on this IR. (Only on the first
228+
# step because the the returned ir only includes updated code.)
229+
if incremental_step == 1:
230+
check_serialization_roundtrip(ir)
229231

230232
setup_file = os.path.abspath(os.path.join(WORKDIR, 'setup.py'))
231233
# We pass the C file information to the build script via setup.py unfortunately

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy