From 000d68443e5935390108827db9892f5ffe175e43 Mon Sep 17 00:00:00 2001 From: Valentin Haenel Date: Tue, 14 Jan 2020 12:28:28 +0100 Subject: [PATCH 1/2] Catch the use of global typed-list in JITed functions As Numba treats globals as compile time constants, we detect the use of a global typed-list inside a JITed function early on during type-inference and emit an appropriate error. The previous behaviour was to error out during lowering with a less informative error message. Includes test. Fixes: 5041 --- numba/ir_utils.py | 2 +- numba/tests/test_typedlist.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/numba/ir_utils.py b/numba/ir_utils.py index 2dea66345a2..5a27b3aea5a 100644 --- a/numba/ir_utils.py +++ b/numba/ir_utils.py @@ -1987,7 +1987,7 @@ def raise_on_unsupported_feature(func_ir, typemap): "compile-time constants and there is no known way to " "compile a %s type as a constant.") if (getattr(ty, 'reflected', False) or - isinstance(ty, types.DictType)): + isinstance(ty, (types.DictType, types.ListType))): raise TypingError(msg % (ty, stmt.value.name, ty), loc=stmt.loc) # checks for generator expressions (yield in use when func_ir has diff --git a/numba/tests/test_typedlist.py b/numba/tests/test_typedlist.py index 5fb393a7625..f7cb05ca809 100644 --- a/numba/tests/test_typedlist.py +++ b/numba/tests/test_typedlist.py @@ -9,6 +9,7 @@ from numba import jitclass, typeof from numba.typed import List, Dict from numba.utils import IS_PY3 +from numba.errors import TypingError from .support import (TestCase, MemoryLeakMixin, unittest, override_config, forbid_codegen) @@ -18,6 +19,11 @@ skip_py2 = unittest.skipUnless(IS_PY3, reason='not supported in py2') +# global typed-list for testing purposes +global_typed_list = List.empty_list(int32) +for i in (int32(1), int32(2), int32(3)): + global_typed_list.append(i) + def to_tl(l): """ Convert cpython list to typed-list. """ @@ -459,6 +465,25 @@ def test_list_create_no_jit_using_List(self): l = List() self.assertEqual(type(l), list) + def test_catch_global_typed_list(self): + @njit() + def foo(): + x = List() + for i in global_typed_list: + x.append(i) + + expected_message = ("The use of a ListType[int32] type, assigned to " + "variable 'global_typed_list' in globals, is not " + "supported as globals are considered compile-time " + "constants and there is no known way to compile " + "a ListType[int32] type as a constant.") + with self.assertRaises(TypingError) as raises: + foo() + self.assertIn( + expected_message, + str(raises.exception), + ) + class TestAllocation(MemoryLeakMixin, TestCase): From 3284d11f46c573849815d699039ab3f799b2d3fb Mon Sep 17 00:00:00 2001 From: Valentin Haenel Date: Wed, 15 Jan 2020 15:31:04 +0100 Subject: [PATCH 2/2] refactor for loop to make it shorter As title. --- numba/tests/test_typedlist.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/numba/tests/test_typedlist.py b/numba/tests/test_typedlist.py index f7cb05ca809..8c48d199f08 100644 --- a/numba/tests/test_typedlist.py +++ b/numba/tests/test_typedlist.py @@ -21,8 +21,8 @@ # global typed-list for testing purposes global_typed_list = List.empty_list(int32) -for i in (int32(1), int32(2), int32(3)): - global_typed_list.append(i) +for i in (1, 2, 3): + global_typed_list.append(int32(i)) def to_tl(l):