From 2dc8d417ca593f709de2d41f8bbc70465bcdcb46 Mon Sep 17 00:00:00 2001 From: whitequark Date: Mon, 30 Jan 2017 10:10:21 +0000 Subject: [PATCH] compiler: support methods defined on singleton instances. Fixes #638. --- artiq/compiler/embedding.py | 6 ++++- .../test/lit/embedding/method_on_instance.py | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 artiq/test/lit/embedding/method_on_instance.py diff --git a/artiq/compiler/embedding.py b/artiq/compiler/embedding.py index 7c6b7f617..af3facc6e 100644 --- a/artiq/compiler/embedding.py +++ b/artiq/compiler/embedding.py @@ -561,7 +561,11 @@ class StitchingInferencer(Inferencer): # of having the host-to-ARTIQ mapping code in only one place and # also immediately getting proper diagnostics on type errors. attr_value = getattr(object_value, attr_name) - if inspect.ismethod(attr_value) and types.is_instance(object_type): + if (inspect.ismethod(attr_value) and + types.is_instance(object_type) and + # Check that the method is indeed defined on the class, + # and not just this instance. + hasattr(type(attr_value), attr_name)): # In cases like: # class c: # @kernel diff --git a/artiq/test/lit/embedding/method_on_instance.py b/artiq/test/lit/embedding/method_on_instance.py new file mode 100644 index 000000000..018545f6b --- /dev/null +++ b/artiq/test/lit/embedding/method_on_instance.py @@ -0,0 +1,22 @@ +# RUN: %python -m artiq.compiler.testbench.embedding %s + +from artiq.language.core import * +from artiq.language.types import * + +class a: + def foo(self, x): + print(x) + +class b: + def __init__(self): + self.obj = a() + self.meth = self.obj.foo + + @kernel + def run(self): + self.meth(1) + +bi = b() +@kernel +def entrypoint(): + bi.run()