def test_exercise_code_path_with_lifted_loop(self): """ Ensures that lifted loops are handled correctly in obj mode """ # the functions to jit def bar(x): return x def foo(x): h = 0. for i in range(x): # py 38 needs two loops for one to lift?! h = h + i for k in range(x): h = h + k if x: h = h - bar(x) return h # compile into an isolated context flags = Flags() flags.enable_pyobject = True flags.enable_looplift = True cres = compile_isolated(foo, [types.intp], flags=flags) ta = cres.type_annotation buf = StringIO() ta.html_annotate(buf) output = buf.getvalue() buf.close() self.assertIn("bar", output) self.assertIn("foo", output) self.assertIn("LiftedLoop", output)
from io import StringIO import numpy as np from numba.core import types from numba.core.compiler import compile_isolated, Flags from numba.tests.support import TestCase, tag, MemoryLeakMixin import unittest looplift_flags = Flags() looplift_flags.enable_pyobject = True looplift_flags.enable_looplift = True pyobject_looplift_flags = looplift_flags.copy() pyobject_looplift_flags.enable_pyobject_looplift = True def lift1(x): # Outer needs object mode because of np.empty() a = np.empty(3) for i in range(a.size): # Inner is nopython-compliant a[i] = x return a def lift2(x): # Outer needs object mode because of np.empty() a = np.empty((3, 4)) for i in range(a.shape[0]): for j in range(a.shape[1]): # Inner is nopython-compliant