def test_namedtuple_types_exception(self): with self.assertRaises(errors.TypingError) as raises: types.NamedTuple(types.uint32, 'p') self.assertIn( "Argument 'types' is not iterable", str(raises.exception) )
def _gettyperecord_impl(typingctx, codepoint): """ Provides the binding to numba_gettyperecord, returns a `typerecord` namedtuple of properties from the codepoint. """ if not isinstance(codepoint, types.Integer): raise TypingError("codepoint must be an integer") def details(context, builder, signature, args): ll_void = context.get_value_type(types.void) ll_Py_UCS4 = context.get_value_type(_Py_UCS4) ll_intc = context.get_value_type(types.intc) ll_intc_ptr = ll_intc.as_pointer() ll_uchar = context.get_value_type(types.uchar) ll_uchar_ptr = ll_uchar.as_pointer() ll_ushort = context.get_value_type(types.ushort) ll_ushort_ptr = ll_ushort.as_pointer() fnty = lc.Type.function( ll_void, [ ll_Py_UCS4, # code ll_intc_ptr, # upper ll_intc_ptr, # lower ll_intc_ptr, # title ll_uchar_ptr, # decimal ll_uchar_ptr, # digit ll_ushort_ptr, # flags ], ) fn = builder.module.get_or_insert_function(fnty, name="numba_gettyperecord") upper = cgutils.alloca_once(builder, ll_intc, name="upper") lower = cgutils.alloca_once(builder, ll_intc, name="lower") title = cgutils.alloca_once(builder, ll_intc, name="title") decimal = cgutils.alloca_once(builder, ll_uchar, name="decimal") digit = cgutils.alloca_once(builder, ll_uchar, name="digit") flags = cgutils.alloca_once(builder, ll_ushort, name="flags") byref = [upper, lower, title, decimal, digit, flags] builder.call(fn, [args[0]] + byref) buf = [] for x in byref: buf.append(builder.load(x)) res = context.make_tuple(builder, signature.return_type, tuple(buf)) return impl_ret_untracked(context, builder, signature.return_type, res) tupty = types.NamedTuple( [ types.intc, types.intc, types.intc, types.uchar, types.uchar, types.ushort ], typerecord, ) sig = tupty(_Py_UCS4) return sig, details
def build_map(context, builder, dict_type, item_types, items): if isinstance(dict_type, types.LiteralStrKeyDict): unliteral_tys = [x for x in dict_type.literal_value.values()] nbty = types.NamedTuple(unliteral_tys, dict_type.tuple_ty) values = [x[1] for x in items] # replace with make_tuple call? tup = context.get_constant_undef(nbty) literal_tys = [x for x in dict_type.literal_value.values()] # this is to deal with repeated keys value_index = dict_type.value_index if value_index is None: # 1:1 map keys:values value_indexer = range(len(values)) else: # 1:>1 map keys:values, e.g. {'a':1, 'a': 'foo'} value_indexer = value_index.values() for i, ix in enumerate(value_indexer): val = values[ix] casted = context.cast(builder, val, literal_tys[i], unliteral_tys[i]) tup = builder.insert_value(tup, casted, i) d = tup context.nrt.incref(builder, nbty, d) else: from numba.typed import Dict dt = types.DictType(dict_type.key_type, dict_type.value_type) kt, vt = dict_type.key_type, dict_type.value_type sig = typing.signature(dt) def make_dict(): return Dict.empty(kt, vt) d = context.compile_internal(builder, make_dict, sig, ()) if items: for (kt, vt), (k, v) in zip(item_types, items): sig = typing.signature(types.void, dt, kt, vt) args = d, k, v def put(d, k, v): d[k] = v context.compile_internal(builder, put, sig, args) return d
def test_namedtuple(self): v = Point(1, 2) tp_point = typeof(v) self.assertEqual(tp_point, types.NamedUniTuple(types.intp, 2, Point)) v = Point(1, 2.0) self.assertEqual(typeof(v), types.NamedTuple([types.intp, types.float64], Point)) w = Rect(3, 4) tp_rect = typeof(w) self.assertEqual(tp_rect, types.NamedUniTuple(types.intp, 2, Rect)) self.assertNotEqual(tp_rect, tp_point) self.assertNotEqual(tp_rect, types.UniTuple(tp_rect.dtype, tp_rect.count))
def test_namedtuples(self): ty1 = types.NamedUniTuple(types.intp, 2, Point) self.check_pickling(ty1) ty2 = types.NamedTuple((types.intp, types.float64), Point) self.check_pickling(ty2)