From 7a671fb2fdd8c5fcee7fc25f4f3f575f5607da0c Mon Sep 17 00:00:00 2001 From: whitequark Date: Thu, 7 Jul 2016 05:00:09 +0000 Subject: [PATCH] embedding: treat numpy.{int32,int64,array} specially (#424). Also, remove them from prelude to not pollute the namespace. --- artiq/compiler/builtins.py | 6 ++++++ artiq/compiler/embedding.py | 18 +++++++++++++++++- artiq/test/coredevice/test_embedding.py | 6 ++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/artiq/compiler/builtins.py b/artiq/compiler/builtins.py index eae7f9337..f060330f0 100644 --- a/artiq/compiler/builtins.py +++ b/artiq/compiler/builtins.py @@ -125,6 +125,12 @@ def fn_bool(): def fn_int(): return types.TConstructor(TInt()) +def fn_int32(): + return types.TConstructor(TInt32()) + +def fn_int64(): + return types.TConstructor(TInt64()) + def fn_float(): return types.TConstructor(TFloat()) diff --git a/artiq/compiler/embedding.py b/artiq/compiler/embedding.py index 8941ea51f..9295c05ad 100644 --- a/artiq/compiler/embedding.py +++ b/artiq/compiler/embedding.py @@ -158,6 +158,18 @@ class ASTSynthesizer: typ = builtins.TBool() return asttyped.NameConstantT(value=value, type=typ, loc=self._add(repr(value))) + elif value is numpy.int32: + typ = builtins.fn_int32() + return asttyped.NameConstantT(value=None, type=typ, + loc=self._add("numpy.int32")) + elif value is numpy.int64: + typ = builtins.fn_int64() + return asttyped.NameConstantT(value=None, type=typ, + loc=self._add("numpy.int64")) + elif value is numpy.array: + typ = builtins.fn_array() + return asttyped.NameConstantT(value=None, type=typ, + loc=self._add("numpy.array")) elif isinstance(value, (int, float)): if isinstance(value, int): typ = builtins.TInt() @@ -637,9 +649,13 @@ class Stitcher: self.name = "" self.typedtree = [] self.inject_at = 0 + self.globals = {} + + # We don't want some things from the prelude as they are provided in + # the host Python namespace and gain special meaning when quoted. self.prelude = prelude.globals() self.prelude.pop("print") - self.globals = {} + self.prelude.pop("array") self.functions = {} diff --git a/artiq/test/coredevice/test_embedding.py b/artiq/test/coredevice/test_embedding.py index 583b2ca0d..7a1037ef2 100644 --- a/artiq/test/coredevice/test_embedding.py +++ b/artiq/test/coredevice/test_embedding.py @@ -105,6 +105,10 @@ class _RPC(EnvExperiment): def args1kwargs2(self): return self.kwargs("X", a="A", b=1) + @kernel + def numpy_things(self): + return (numpy.int32(10), numpy.int64(20), numpy.array([42,])) + @kernel def builtin(self): sleep(1.0) @@ -120,6 +124,8 @@ class RPCTest(ExperimentCase): self.assertEqual(exp.kwargs1(), 1) self.assertEqual(exp.kwargs2(), 2) self.assertEqual(exp.args1kwargs2(), 2) + self.assertEqual(exp.numpy_things(), + (numpy.int32(10), numpy.int64(20), numpy.array([42,]))) exp.builtin()