def test_inline_always_ssa_scope_validity(self): # Make sure IR inlining correctly updates the scope(s). See #7802 def bar(): b = 5 while b > 1: b //= 2 return 10 @overload(bar, inline="always") def bar_impl(): return bar @njit def foo(): bar() with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always', errors.NumbaIRAssumptionWarning) ignore_internal_warnings() self.assertEqual(foo(), foo.py_func()) # There should be no warnings as the IR scopes should be consistent with # the IR involved. self.assertEqual(len(w), 0)
def test_return_type_warning(self): y = np.ones(4, dtype=np.float32) def return_external_array(): return y with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always', NumbaWarning) ignore_internal_warnings() cfunc = jit(_nrt=False)(return_external_array) cfunc() self.assertEqual(len(w), 3) # Legal return value failure self.assertEqual(w[0].category, NumbaWarning) self.assertIn('return type', str(w[0].message)) # Object mode fall-back self.assertEqual(w[1].category, NumbaWarning) self.assertIn('object mode without forceobj=True', str(w[1].message)) # check objmode deprecation warning self.check_objmode_deprecation_warning(w[2])
def test_no_warning_with_forceobj(self): def add(x, y): a = [] # noqa dead return x + y with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always', NumbaWarning) ignore_internal_warnings() cfunc = jit(add, forceobj=True) cfunc(1, 2) self.assertEqual(len(w), 0)
def test_deprecated(self): @deprecated('foo') def bar(): pass with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') ignore_internal_warnings() bar() self.assertEqual(len(w), 1) self.assertEqual(w[0].category, DeprecationWarning) self.assertIn('bar', str(w[0].message)) self.assertIn('foo', str(w[0].message))
def test_return_type_warning_with_nrt(self): """ Rerun test_return_type_warning with nrt """ y = np.ones(4, dtype=np.float32) def return_external_array(): return y with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always', NumbaWarning) ignore_internal_warnings() cfunc = jit(return_external_array) cfunc() # No more warning self.assertEqual(len(w), 0)
def test_linking_cu_log_warning(self): bar = cuda.declare_device('bar', 'int32(int32)') link = os.path.join(os.path.dirname(__file__), 'data', 'warn.cu') with warnings.catch_warnings(record=True) as w: ignore_internal_warnings() @cuda.jit('void(int32)', link=[link]) def kernel(x): bar(x) self.assertEqual(len(w), 1, 'Expected warnings from NVRTC') # Check the warning refers to the log messages self.assertIn('NVRTC log messages', str(w[0].message)) # Check the message pertaining to the unused variable is provided self.assertIn('declared but never referenced', str(w[0].message))
def test_irregularly_indented_source(self): @njit(debug=True) def foo(): # NOTE: THIS COMMENT MUST START AT COLUMN 0 FOR THIS SAMPLE CODE TO BE VALID # noqa: E115, E501 return 1 with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always', NumbaDebugInfoWarning) ignore_internal_warnings() foo() # No warnings self.assertEqual(len(w), 0) metadata = self._get_metadata(foo, foo.signatures[0]) lines = self._get_lines_from_debuginfo(metadata) # Only one line self.assertEqual(len(lines), 1)
def test_unparsable_indented_source(self): @njit(debug=True) def foo(): # NOTE: THIS COMMENT MUST START AT COLUMN 0 FOR THIS SAMPLE CODE TO BE VALID # noqa: E115, E501 return 1 with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always', NumbaDebugInfoWarning) ignore_internal_warnings() foo() self.assertEqual(len(w), 1) found = w[0] self.assertEqual(found.category, NumbaDebugInfoWarning) msg = str(found.message) # make sure the warning contains the right message self.assertIn('Could not parse the source for function', msg) # and refers to the offending function self.assertIn(str(foo.py_func), msg)
def test_experimental_warning(self): # Check that if the isinstance feature is in use then an experiemental # warning is raised. with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always', errors.NumbaWarning) ignore_internal_warnings() @njit def foo(x): return isinstance(x, float) foo(1.234) self.assertEqual(len(w), 1) self.assertEqual(w[0].category, errors.NumbaExperimentalFeatureWarning) msg = ("Use of isinstance() detected. This is an experimental " "feature.") self.assertIn(msg, str(w[0].message))
def test_missing_source(self): strsrc = """ def foo(): return 1 """ l = dict() exec(dedent(strsrc), {}, l) foo = njit(debug=True)(l['foo']) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always', NumbaDebugInfoWarning) ignore_internal_warnings() foo() self.assertEqual(len(w), 1) found = w[0] self.assertEqual(found.category, NumbaDebugInfoWarning) msg = str(found.message) # make sure the warning contains the right message self.assertIn('Could not find source for function', msg) # and refers to the offending function self.assertIn(str(foo.py_func), msg)
def test_warnings_fixer(self): # For some context, see #4083 wfix = errors.WarningsFixer(errors.NumbaWarning) with wfix.catch_warnings('foo', 10): warnings.warn(errors.NumbaWarning('same')) warnings.warn(errors.NumbaDeprecationWarning('same')) ignore_internal_warnings() with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') ignore_internal_warnings() wfix.flush() self.assertEqual(len(w), 2) # the order of these will be backwards to the above, the # WarningsFixer flush method sorts with a key based on str # comparison self.assertEqual(w[0].category, NumbaDeprecationWarning) self.assertEqual(w[1].category, NumbaWarning) self.assertIn('same', str(w[0].message)) self.assertIn('same', str(w[1].message))
def test_loop_lift_warn(self): def do_loop(x): a = {} # noqa dead for i in range(x.shape[0]): x[i] *= 2 with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always', NumbaWarning) ignore_internal_warnings() x = np.ones(4, dtype=np.float32) cfunc = jit(do_loop) cfunc(x) msg = '\n'.join(f"----------\n{x.message}" for x in w) self.assertEqual(len(w), 4, msg=msg) # Type inference failure (1st pass, in npm, fall-back to objmode # with looplift) self.assertEqual(w[0].category, NumbaWarning) self.assertIn('type inference', str(w[0].message)) self.assertIn('WITH looplifting', str(w[0].message)) # Type inference failure (2nd pass, objmode with lifted loops, # loop found but still failed, fall back to objmode no looplift) self.assertEqual(w[1].category, NumbaWarning) self.assertIn('type inference', str(w[1].message)) self.assertIn('WITHOUT looplifting', str(w[1].message)) # States compilation outcome self.assertEqual(w[2].category, NumbaWarning) self.assertIn('compiled in object mode without forceobj=True', str(w[2].message)) self.assertIn('but has lifted loops', str(w[2].message)) # check objmode deprecation warning self.check_objmode_deprecation_warning(w[3])
def test_type_infer_warning(self): def add(x, y): a = {} # noqa dead return x + y with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always', NumbaWarning) ignore_internal_warnings() cfunc = jit(add) cfunc(1, 2) self.assertEqual(len(w), 3) # Type inference failure self.assertEqual(w[0].category, NumbaWarning) self.assertIn('type inference', str(w[0].message)) # Object mode self.assertEqual(w[1].category, NumbaWarning) self.assertIn('object mode', str(w[1].message)) # check objmode deprecation warning self.check_objmode_deprecation_warning(w[2])
def deprecation_warning_catcher(): with warnings.catch_warnings(record=True) as w: ignore_internal_warnings() warnings.simplefilter("always", category=NumbaDeprecationWarning) yield w