def unbox_listtype(typ, val, c): context = c.context builder = c.builder miptr = c.pyapi.object_getattr_string(val, '_opaque') native = c.unbox(types.MemInfoPointer(types.voidptr), miptr) mi = native.value ctor = cgutils.create_struct_proxy(typ) lstruct = ctor(context, builder) data_pointer = context.nrt.meminfo_data(builder, mi) data_pointer = builder.bitcast( data_pointer, listobject.ll_list_type.as_pointer(), ) lstruct.data = builder.load(data_pointer) lstruct.meminfo = mi lstobj = lstruct._getvalue() c.pyapi.decref(miptr) return NativeValue(lstobj)
def unbox_listtype(typ, val, c): context = c.context builder = c.builder # Check that `type(val) is Dict` list_type = c.pyapi.unserialize(c.pyapi.serialize_object(List)) valtype = c.pyapi.object_type(val) same_type = builder.icmp_unsigned("==", valtype, list_type) with c.builder.if_else(same_type) as (then, orelse): with then: miptr = c.pyapi.object_getattr_string(val, '_opaque') native = c.unbox(types.MemInfoPointer(types.voidptr), miptr) mi = native.value ctor = cgutils.create_struct_proxy(typ) lstruct = ctor(context, builder) data_pointer = context.nrt.meminfo_data(builder, mi) data_pointer = builder.bitcast( data_pointer, listobject.ll_list_type.as_pointer(), ) lstruct.data = builder.load(data_pointer) lstruct.meminfo = mi lstobj = lstruct._getvalue() c.pyapi.decref(miptr) bb_unboxed = c.builder.basic_block with orelse: # Raise error on incorrect type c.pyapi.err_format( "PyExc_TypeError", "can't unbox a %S as a %S", valtype, list_type, ) bb_else = c.builder.basic_block # Phi nodes to gather the output lstobj_res = c.builder.phi(lstobj.type) is_error_res = c.builder.phi(cgutils.bool_t) lstobj_res.add_incoming(lstobj, bb_unboxed) lstobj_res.add_incoming(lstobj.type(None), bb_else) is_error_res.add_incoming(cgutils.false_bit, bb_unboxed) is_error_res.add_incoming(cgutils.true_bit, bb_else) # cleanup c.pyapi.decref(list_type) c.pyapi.decref(valtype) return NativeValue(lstobj_res, is_error=is_error_res)
def unbox_dicttype(typ, val, c): context = c.context # Check that `type(val) is Dict` dict_type = c.pyapi.unserialize(c.pyapi.serialize_object(Dict)) valtype = c.pyapi.object_type(val) same_type = c.builder.icmp_unsigned("==", valtype, dict_type) with c.builder.if_else(same_type) as (then, orelse): with then: miptr = c.pyapi.object_getattr_string(val, '_opaque') mip_type = types.MemInfoPointer(types.voidptr) native = c.unbox(mip_type, miptr) mi = native.value argtypes = mip_type, typeof(typ) def convert(mi, typ): return dictobject._from_meminfo(mi, typ) sig = signature(typ, *argtypes) nil_typeref = context.get_constant_null(argtypes[1]) args = (mi, nil_typeref) is_error, dctobj = c.pyapi.call_jit_code(convert, sig, args) # decref here because we are stealing a reference. c.context.nrt.decref(c.builder, typ, dctobj) c.pyapi.decref(miptr) bb_unboxed = c.builder.basic_block with orelse: # Raise error on incorrect type c.pyapi.err_format( "PyExc_TypeError", "can't unbox a %S as a %S", valtype, dict_type, ) bb_else = c.builder.basic_block # Phi nodes to gather the output dctobj_res = c.builder.phi(dctobj.type) is_error_res = c.builder.phi(is_error.type) dctobj_res.add_incoming(dctobj, bb_unboxed) dctobj_res.add_incoming(dctobj.type(None), bb_else) is_error_res.add_incoming(is_error, bb_unboxed) is_error_res.add_incoming(cgutils.true_bit, bb_else) # cleanup c.pyapi.decref(dict_type) c.pyapi.decref(valtype) return NativeValue(dctobj_res, is_error=is_error_res)
def unbox_index(typ, obj, c): """ Convert a Index object to a native structure. """ data = c.pyapi.object_getattr_string(obj, "_data") index = make_index(c.context, c.builder, typ) index.data = c.unbox(typ.as_array, data).value return NativeValue(index._getvalue())
def unbox_series(typ, obj, c): """ Convert a Series object to a native structure. """ index = c.pyapi.object_getattr_string(obj, "_index") values = c.pyapi.object_getattr_string(obj, "_values") series = make_series(c.context, c.builder, typ) series.index = c.unbox(typ.index, index).value series.values = c.unbox(typ.values, values).value return NativeValue(series._getvalue())
def unbox_struct_ref(typ, obj, c): mi_obj = c.pyapi.object_getattr_string(obj, "_meminfo") mip_type = types.MemInfoPointer(types.voidptr) mi = c.unbox(mip_type, mi_obj).value utils = _Utils(c.context, c.builder, typ) struct_ref = utils.new_struct_ref(mi) out = struct_ref._getvalue() c.pyapi.decref(mi_obj) return NativeValue(out)
def unbox_dicttype(typ, val, c): context = c.context miptr = c.pyapi.object_getattr_string(val, '_opaque') mip_type = types.MemInfoPointer(types.voidptr) native = c.unbox(mip_type, miptr) mi = native.value argtypes = mip_type, typeof(typ) def convert(mi, typ): return dictobject._from_meminfo(mi, typ) sig = signature(typ, *argtypes) nil_typeref = context.get_constant_null(argtypes[1]) args = (mi, nil_typeref) is_error, dctobj = c.pyapi.call_jit_code(convert, sig, args) # decref here because we are stealing a reference. c.context.nrt.decref(c.builder, typ, dctobj) c.pyapi.decref(miptr) return NativeValue(dctobj, is_error=is_error)
def unbox_index(typ, obj, c): return NativeValue(c.context.get_dummy_value())