From f10cbf3e7a2b3fc0c0f9bde54449bc86c1ddbcd7 Mon Sep 17 00:00:00 2001 From: "Kalistratov, Alexander" Date: Wed, 1 Jan 2020 23:46:29 +0300 Subject: [PATCH 1/2] Test for literally in overload_method --- sdc/tests/test_hpat_jit.py | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/sdc/tests/test_hpat_jit.py b/sdc/tests/test_hpat_jit.py index d585bd233..510df2774 100644 --- a/sdc/tests/test_hpat_jit.py +++ b/sdc/tests/test_hpat_jit.py @@ -33,6 +33,10 @@ import pandas as pd from sdc import * from numba.typed import Dict +from numba.extending import (overload_method, overload, models, register_model, intrinsic) +from numba.special import literally +from numba.typing import signature +from numba import cgutils from collections import defaultdict from sdc.tests.test_base import TestCase from sdc.tests.test_utils import skip_numba_jit @@ -436,6 +440,56 @@ def test_impl(): hpat_func = self.jit(test_impl) pd.testing.assert_frame_equal(hpat_func(), test_impl()) + @unittest.expectedFailure + def test_literally_with_overload_method(self): + class Dummy: + def lit(self, a): + return a + + class DummyType(numba.types.Type): + def __init__(self): + super().__init__(name="dummy") + + @register_model(DummyType) + class DummyTypeModel(models.StructModel): + def __init__(self, dmm, fe_type): + members = [] + super().__init__(dmm, fe_type, members) + + @intrinsic + def init_dummy(typingctx): + def codegen(context, builder, signature, args): + dummy = cgutils.create_struct_proxy( + signature.return_type)(context, builder) + + return dummy._getvalue() + + sig = signature(DummyType()) + return sig, codegen + + @overload(Dummy) + def dummy_overload(): + def ctor(): + return init_dummy() + + return ctor + + @overload_method(DummyType, 'lit') + def lit_overload(self, a): + def impl(self, a): + return literally(a) + # return a + + return impl + + def test_impl(a): + d = Dummy() + + return d.lit(a) + + jtest = numba.njit(test_impl) + test_impl(5) + if __name__ == "__main__": unittest.main() From 1aa9bc31fa166aad12ca13e117e30782ebddccb9 Mon Sep 17 00:00:00 2001 From: "Kalistratov, Alexander" Date: Sun, 5 Jan 2020 03:05:08 +0300 Subject: [PATCH 2/2] Add test for user exception in @unbox --- sdc/tests/test_hpat_jit.py | 53 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/sdc/tests/test_hpat_jit.py b/sdc/tests/test_hpat_jit.py index 510df2774..5aa6b5b72 100644 --- a/sdc/tests/test_hpat_jit.py +++ b/sdc/tests/test_hpat_jit.py @@ -33,7 +33,9 @@ import pandas as pd from sdc import * from numba.typed import Dict -from numba.extending import (overload_method, overload, models, register_model, intrinsic) +from numba.extending import (overload_method, overload, models, register_model, + intrinsic, unbox, typeof_impl, make_attribute_wrapper, + NativeValue) from numba.special import literally from numba.typing import signature from numba import cgutils @@ -488,7 +490,54 @@ def test_impl(a): return d.lit(a) jtest = numba.njit(test_impl) - test_impl(5) + self.assertEqual(jtest(5), 5) + + @unittest.expectedFailure + def test_unbox_with_exception(self): + class Dummy: + def __init__(self, a=0): + self.a = a + + class DummyType(numba.types.Type): + def __init__(self): + super().__init__(name="dummy") + + @register_model(DummyType) + class DummyTypeModel(models.StructModel): + def __init__(self, dmm, fe_type): + members = [('a', numba.types.int64)] + super().__init__(dmm, fe_type, members) + + @unbox(DummyType) + def unbox_dummy(typ, val, c): + context = c.context + builder = c.builder + + a_obj = c.pyapi.object_getattr_string(val, "a") + a_value = c.pyapi.long_as_longlong(a_obj) + + dmm = cgutils.create_struct_proxy(typ)(context, builder) + + with builder.if_then(a_value): + context.call_conv.return_user_exc( + builder, ValueError, + ("exception!",) + ) + + dmm.a = a_value + return NativeValue(dmm._getvalue()) + + make_attribute_wrapper(DummyType, 'a', 'a') + + def test_impl(d): + return d.a + + @typeof_impl.register(Dummy) + def typeof_dummy(val, c): + return DummyType() + + jtest = numba.njit(test_impl) + print(jtest(Dummy(0))) if __name__ == "__main__":