def test_errors(self): f = cfunc(div_sig)(div_usecase) with captured_stderr() as err: self.assertPreciseEqual(f.ctypes(5, 2), 2.5) self.assertEqual(err.getvalue(), "") with captured_stderr() as err: res = f.ctypes(5, 0) # This is just a side effect of Numba zero-initializing # stack variables, and could change in the future. self.assertPreciseEqual(res, 0.0) err = err.getvalue() self.assertIn("ZeroDivisionError:", err) self.assertIn("Exception ignored", err)
def check_module(self, mod): f = mod.add_usecase self.assertPreciseEqual(f.ctypes(2.0, 3.0), 6.0) f = mod.add_nocache_usecase self.assertPreciseEqual(f.ctypes(2.0, 3.0), 6.0) f = mod.outer self.assertPreciseEqual(f.ctypes(5.0, 2.0), 4.0) f = mod.div_usecase with captured_stderr() as err: self.assertPreciseEqual(f.ctypes(7, 2), 3.5) self.assertEqual(err.getvalue(), "") with captured_stderr() as err: f.ctypes(7, 0) err = err.getvalue() self.assertIn("ZeroDivisionError", err)
def compile_simple_cuda(self): with captured_stderr() as err: with captured_stdout() as out: cfunc = cuda.jit((float64[:], float64[:]))(simple_cuda) # Call compiled function (to ensure PTX is generated) # and sanity-check results. A = np.linspace(0, 1, 10).astype(np.float64) B = np.zeros_like(A) cfunc[1, 10](A, B) self.assertTrue(np.allclose(A + 1.5, B)) # stderr shouldn't be affected by debug output self.assertFalse(err.getvalue()) return out.getvalue()
def test_vectorize_error_in_objectmode(self): # An exception raised inside an object mode @vectorized function # is printed out and ignored. ufunc = vectorize(['int32(int32)'], forceobj=True)(uerror) a = numpy.arange(4, dtype='int32') b = numpy.zeros_like(a) with support.captured_stderr() as err: ufunc(a, out=b) err = err.getvalue() if sys.version_info >= (3, 4): self.assertIn("Exception ignored in: 'object mode ufunc'", err) self.assertIn("MyException: I'm here", err) else: self.assertRegexpMatches(err, r"Exception [^\n]* in 'object mode ufunc' ignored") self.assertIn("I'm here", err) self.assertTrue(numpy.all(b == numpy.array([1, 2, 0, 4])))
def check_ignored_exception(self, ctx): with captured_stderr() as cap: yield ctx.deallocations.clear() self.assertFalse(cap.getvalue())