def generic(self, args, kws): assert not kws if args: iterable, = args if isinstance(iterable, types.IterableType): dtype = iterable.iterator_type.yield_type return signature(types.List(dtype), iterable) else: return signature(types.List(types.undefined))
def _typeof_list(val, c): if len(val) == 0: raise ValueError("Cannot type empty list") ty = typeof_impl(val[0], c) if ty is None: raise ValueError(f"Cannot type list element type {type(val[0])}") return types.List(ty, reflected=True)
def codegen(context, builder, sig, args): nitems = args[0] list_type = types.List(string_type) result = numba.cpython.listobj.ListInstance.allocate( context, builder, list_type, nitems) result.size = nitems return impl_ret_new_ref(context, builder, list_type, result.value)
def test_subclass_specialization(self): os = OverloadSelector() self.assertTrue(issubclass(types.Sequence, types.Container)) os.append(1, (types.Container, types.Container,)) lstty = types.List(types.boolean) self.assertEqual(os.find((lstty, lstty)), 1) os.append(2, (types.Container, types.Sequence,)) self.assertEqual(os.find((lstty, lstty)), 2)
def test_lists(self): v = [1.0] * 100 self.assertEqual(typeof(v), types.List(types.float64, reflected=True)) bad_v = [{1: 3}] with self.assertRaises(ValueError) as raises: typeof(bad_v) self.assertIn("Cannot type list element type", str(raises.exception))
def ol_bar(x): self.assertTrue(isinstance(x, types.LiteralList)) lv = x.literal_value self.assertTrue(isinstance(lv, list)) self.assertEqual(lv[0], types.literal(1)) self.assertEqual(lv[1], types.literal('a')) self.assertEqual(lv[2], types.Array(types.float64, 1, 'C')) self.assertEqual(lv[3], types.List(types.intp, reflected=False, initial_value=[1, 2, 3])) self.assertTrue(isinstance(lv[4], types.LiteralList)) self.assertEqual(lv[4].literal_value[0], types.literal('cat')) self.assertEqual(lv[4].literal_value[1], types.literal(10)) return lambda x: x
def test_print_values(self): """ Test printing a single argument value. """ pyfunc = print_value def check_values(typ, values): cr = compile_isolated(pyfunc, (typ,)) cfunc = cr.entry_point for val in values: with captured_stdout(): cfunc(val) self.assertEqual(sys.stdout.getvalue(), str(val) + '\n') # Various scalars check_values(types.int32, (1, -234)) check_values(types.int64, (1, -234, 123456789876543210, -123456789876543210)) check_values(types.uint64, (1, 234, 123456789876543210, 2**63 + 123)) check_values(types.boolean, (True, False)) check_values(types.float64, (1.5, 100.0**10.0, float('nan'))) check_values(types.complex64, (1+1j,)) check_values(types.NPTimedelta('ms'), (np.timedelta64(100, 'ms'),)) cr = compile_isolated(pyfunc, (types.float32,)) cfunc = cr.entry_point with captured_stdout(): cfunc(1.1) # Float32 will lose precision got = sys.stdout.getvalue() expect = '1.10000002384' self.assertTrue(got.startswith(expect)) self.assertTrue(got.endswith('\n')) # NRT-enabled type with self.assertNoNRTLeak(): x = [1, 3, 5, 7] with self.assertRefCount(x): check_values(types.List(types.int32), (x,)) # Array will have to use object mode arraytype = types.Array(types.int32, 1, 'C') cr = compile_isolated(pyfunc, (arraytype,), flags=enable_pyobj_flags) cfunc = cr.entry_point with captured_stdout(): cfunc(np.arange(10, dtype=np.int32)) self.assertEqual(sys.stdout.getvalue(), '[0 1 2 3 4 5 6 7 8 9]\n')
def ol_bar(x): self.assertTrue(isinstance(x, types.LiteralStrKeyDict)) dlv = x.literal_value inner_literal = { types.literal('g'): types.literal('h'), types.literal('i'): types.Array(types.float64, 1, 'C') } inner_dict = types.LiteralStrKeyDict(inner_literal) outer_literal = { types.literal('a'): types.LiteralList([ types.literal(1), types.literal('a'), types.DictType(types.unicode_type, types.intp, initial_value={'f': 1}), inner_dict ]), types.literal('b'): types.literal(2), types.literal('c'): types.List(types.complex128, reflected=False) } def check_same(a, b): if (isinstance(a, types.LiteralList) and isinstance(b, types.LiteralList)): for i, j in zip(a.literal_value, b.literal_value): check_same(a.literal_value, b.literal_value) elif (isinstance(a, list) and isinstance(b, list)): for i, j in zip(a, b): check_same(i, j) elif (isinstance(a, types.LiteralStrKeyDict) and isinstance(b, types.LiteralStrKeyDict)): for (ki, vi), (kj, vj) in zip(a.literal_value.items(), b.literal_value.items()): check_same(ki, kj) check_same(vi, vj) elif (isinstance(a, dict) and isinstance(b, dict)): for (ki, vi), (kj, vj) in zip(a.items(), b.items()): check_same(ki, kj) check_same(vi, vj) else: self.assertEqual(a, b) check_same(dlv, outer_literal) return lambda x: x
def test_lists(self): ty = types.List(types.int32) self.check_pickling(ty)
def test_list_type_getitem(self): for listty in (types.int64, types.Array(types.float64, 1, 'C')): l_int = types.List(listty) self.assertTrue(isinstance(l_int, types.List)) self.assertTrue(isinstance(l_int[0], type(listty)))
def test_disallow_list(self): self.assert_disallow_key(types.List(types.intp)) self.assert_disallow_value(types.List(types.intp))
def test_lists(self): v = [1.0] * 100 self.assertEqual(typeof(v), types.List(types.float64, reflected=True))
def make_jitclass_container(self): spec = { 'data': types.List(dtype=types.List(types.float64[::1])), } JCContainer = jitclass(spec)(Container) return JCContainer
def test_list(self): aty = types.List(types.undefined) bty = types.List(i32) self.assert_unify(aty, bty, bty) aty = types.List(i16) bty = types.List(i32) self.assert_unify(aty, bty, bty) aty = types.List(types.Tuple([i32, i16])) bty = types.List(types.Tuple([i16, i64])) cty = types.List(types.Tuple([i32, i64])) self.assert_unify(aty, bty, cty) # Different reflections aty = types.List(i16, reflected=True) bty = types.List(i32) cty = types.List(i32, reflected=True) self.assert_unify(aty, bty, cty) # Incompatible dtypes aty = types.List(i16) bty = types.List(types.Tuple([i16])) self.assert_unify_failure(aty, bty)
def test_1d_slicing_set_list(self, flags=enable_pyobj_flags): """ List to 1d slice assignment """ self.check_1d_slicing_set_sequence(flags, types.List(types.int16), [8, -42])
def test_range_iter_list(self): range_iter_func = range_iter_len2 cres = compile_isolated(range_iter_func, [types.List(types.intp)]) cfunc = cres.entry_point arglist = [1, 2, 3, 4, 5] self.assertEqual(cfunc(arglist), len(arglist))
@intrinsic def alloc_str_list(typingctx, n_t=None): def codegen(context, builder, sig, args): nitems = args[0] list_type = types.List(string_type) result = numba.cpython.listobj.ListInstance.allocate( context, builder, list_type, nitems) result.size = nitems return impl_ret_new_ref(context, builder, list_type, result.value) return types.List(string_type)(types.intp), codegen # XXX using list of list string instead of array of list string since Numba's # arrays can't store lists list_string_array_type = types.List(types.List(string_type)) @intrinsic def alloc_list_list_str(typingctx, n_t=None): def codegen(context, builder, sig, args): nitems = args[0] list_type = list_string_array_type result = numba.cpython.listobj.ListInstance.allocate( context, builder, list_type, nitems) result.size = nitems return impl_ret_new_ref(context, builder, list_type, result.value) return list_string_array_type(types.intp), codegen
def resolve_split(self, dict, args, kws): assert not kws assert len(args) == 1 return signature(types.List(std_str_type), types.unliteral(args[0]))