Skip to content

Commit

Permalink
Merge pull request numba#5078 from esc/typed_list/catch_global
Browse files Browse the repository at this point in the history
Catch the use of global typed-list in JITed functions
  • Loading branch information
sklam committed Jan 15, 2020
2 parents 360d8fa + 3284d11 commit 2a497bb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion numba/ir_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1998,7 +1998,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
Expand Down
25 changes: 25 additions & 0 deletions numba/tests/test_typedlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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 (1, 2, 3):
global_typed_list.append(int32(i))


def to_tl(l):
""" Convert cpython list to typed-list. """
Expand Down Expand Up @@ -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):

Expand Down

0 comments on commit 2a497bb

Please sign in to comment.