def unbox_unicodecharseq(typ, obj, c): lty = c.context.get_value_type(typ) ok, buffer, size, kind, is_ascii, hashv = \ c.pyapi.string_as_string_size_and_kind(obj) # If conversion is ok, copy the buffer to the output storage. with cgutils.if_likely(c.builder, ok): # Check if the returned string size fits in the charseq storage_size = ir.Constant(size.type, typ.count) size_fits = c.builder.icmp_unsigned("<=", size, storage_size) # Allow truncation of string size = c.builder.select(size_fits, size, storage_size) # Initialize output to zero bytes null_string = ir.Constant(lty, None) outspace = cgutils.alloca_once_value(c.builder, null_string) # We don't need to set the NULL-terminator because the storage # is already zero-filled. cgutils.memcpy(c.builder, c.builder.bitcast(outspace, buffer.type), buffer, size) ret = c.builder.load(outspace) return NativeValue(ret, is_error=c.builder.not_(ok))
def unbox_set(typ, obj, c): """ Convert set *obj* to a native set. If set was previously unboxed, we reuse the existing native set to ensure consistency. """ size = c.pyapi.set_size(obj) errorptr = cgutils.alloca_once_value(c.builder, cgutils.false_bit) setptr = cgutils.alloca_once(c.builder, c.context.get_value_type(typ)) # See if the set was previously unboxed, if so, re-use the meminfo. ptr = c.pyapi.object_get_private_data(obj) with c.builder.if_else(cgutils.is_not_null(c.builder, ptr)) \ as (has_meminfo, otherwise): with has_meminfo: # Set was previously unboxed => reuse meminfo inst = setobj.SetInstance.from_meminfo(c.context, c.builder, typ, ptr) if typ.reflected: inst.parent = obj c.builder.store(inst.value, setptr) with otherwise: _python_set_to_native(typ, obj, c, size, setptr, errorptr) def cleanup(): # Clean up the associated pointer, as the meminfo is now invalid. c.pyapi.object_reset_private_data(obj) return NativeValue(c.builder.load(setptr), is_error=c.builder.load(errorptr), cleanup=cleanup)
def unbox_optional(typ, obj, c): """ Convert object *obj* to a native optional structure. """ noneval = c.context.make_optional_none(c.builder, typ.type) is_not_none = c.builder.icmp_signed('!=', obj, c.pyapi.borrow_none()) retptr = cgutils.alloca_once(c.builder, noneval.type) errptr = cgutils.alloca_once_value(c.builder, cgutils.false_bit) with c.builder.if_else(is_not_none) as (then, orelse): with then: native = c.unbox(typ.type, obj) just = c.context.make_optional_value(c.builder, typ.type, native.value) c.builder.store(just, retptr) c.builder.store(native.is_error, errptr) with orelse: c.builder.store(noneval, retptr) if native.cleanup is not None: def cleanup(): with c.builder.if_then(is_not_none): native.cleanup() else: cleanup = None ret = c.builder.load(retptr) return NativeValue(ret, is_error=c.builder.load(errptr), cleanup=cleanup)
def _unbox_class_instance(typ, val, c): def access_member(member_offset): # Access member by byte offset offset = c.context.get_constant(types.uintp, member_offset) llvoidptr = ir.IntType(8).as_pointer() ptr = cgutils.pointer_add(c.builder, val, offset) casted = c.builder.bitcast(ptr, llvoidptr.as_pointer()) return c.builder.load(casted) struct_cls = cgutils.create_struct_proxy(typ) inst = struct_cls(c.context, c.builder) # load from Python object ptr_meminfo = access_member(_box.box_meminfoptr_offset) ptr_dataptr = access_member(_box.box_dataptr_offset) # store to native structure inst.meminfo = c.builder.bitcast(ptr_meminfo, inst.meminfo.type) inst.data = c.builder.bitcast(ptr_dataptr, inst.data.type) ret = inst._getvalue() c.context.nrt.incref(c.builder, typ, ret) return NativeValue(ret, is_error=c.pyapi.c_api_error())
def unbox_numpy_random_generator(typ, obj, c): """ Here we're creating a NumPyRandomGeneratorType StructModel with following fields: * ('bit_generator', _bit_gen_type): The unboxed BitGenerator associated with this Generator object instance. * ('parent', types.pyobject): Pointer to the original Generator PyObject. * ('meminfo', types.MemInfoPointer(types.voidptr)): The information about the memory stored at the pointer (to the original Generator PyObject). This is useful for keeping track of reference counts within the Python runtime. Helps prevent cases where deletion happens in Python runtime without NRT being awareness of it. """ is_error_ptr = cgutils.alloca_once_value(c.builder, cgutils.false_bit) with ExitStack() as stack: struct_ptr = cgutils.create_struct_proxy(typ)(c.context, c.builder) bit_gen_inst = c.pyapi.object_getattr_string(obj, 'bit_generator') with early_exit_if_null(c.builder, stack, bit_gen_inst): c.builder.store(cgutils.true_bit, is_error_ptr) unboxed = c.unbox(_bit_gen_type, bit_gen_inst).value struct_ptr.bit_generator = unboxed struct_ptr.parent = obj NULL = cgutils.voidptr_t(None) struct_ptr.meminfo = c.pyapi.nrt_meminfo_new_from_pyobject( NULL, # there's no data obj, # the python object, the call to nrt_meminfo_new_from_pyobject # will py_incref ) c.pyapi.decref(bit_gen_inst) return NativeValue(struct_ptr._getvalue(), is_error=c.builder.load(is_error_ptr))
def unbox_deferred(typ, obj, c): native_value = c.pyapi.to_native_value(typ.get(), obj) model = c.context.data_model_manager[typ] res = model.set(c.builder, model.make_uninitialized(), native_value.value) return NativeValue(res, is_error=native_value.is_error, cleanup=native_value.cleanup)
def unbox_buffer(typ, obj, c): """ Convert a Py_buffer-providing object to a native array structure. """ buf = c.pyapi.alloca_buffer() res = c.pyapi.get_buffer(obj, buf) is_error = cgutils.is_not_null(c.builder, res) nativearycls = c.context.make_array(typ) nativeary = nativearycls(c.context, c.builder) aryptr = nativeary._getpointer() with cgutils.if_likely(c.builder, c.builder.not_(is_error)): ptr = c.builder.bitcast(aryptr, c.pyapi.voidptr) if c.context.enable_nrt: c.pyapi.nrt_adapt_buffer_from_python(buf, ptr) else: c.pyapi.numba_buffer_adaptor(buf, ptr) def cleanup(): c.pyapi.release_buffer(buf) return NativeValue(c.builder.load(aryptr), is_error=is_error, cleanup=cleanup)
def unbox_float(typ, obj, c): fobj = c.pyapi.number_float(obj) dbval = c.pyapi.float_as_double(fobj) c.pyapi.decref(fobj) if typ == types.float32: val = c.builder.fptrunc(dbval, c.context.get_argument_type(typ)) else: assert typ == types.float64 val = dbval return NativeValue(val, is_error=c.pyapi.c_api_error())
def unbox_tuple(typ, obj, c): """ Convert tuple *obj* to a native array (if homogeneous) or structure. """ n = len(typ) values = [] cleanups = [] lty = c.context.get_value_type(typ) is_error_ptr = cgutils.alloca_once_value(c.builder, cgutils.false_bit) value_ptr = cgutils.alloca_once(c.builder, lty) # Issue #1638: need to check the tuple size actual_size = c.pyapi.tuple_size(obj) size_matches = c.builder.icmp_unsigned( "==", actual_size, ir.Constant(actual_size.type, n) ) with c.builder.if_then(c.builder.not_(size_matches), likely=False): c.pyapi.err_format( "PyExc_ValueError", "size mismatch for tuple, expected %d element(s) but got %%zd" % (n,), actual_size, ) c.builder.store(cgutils.true_bit, is_error_ptr) # We unbox the items even if not `size_matches`, to avoid issues with # the generated IR (instruction doesn't dominate all uses) for i, eltype in enumerate(typ): elem = c.pyapi.tuple_getitem(obj, i) native = c.unbox(eltype, elem) values.append(native.value) with c.builder.if_then(native.is_error, likely=False): c.builder.store(cgutils.true_bit, is_error_ptr) if native.cleanup is not None: cleanups.append(native.cleanup) value = c.context.make_tuple(c.builder, typ, values) c.builder.store(value, value_ptr) if cleanups: with c.builder.if_then(size_matches, likely=True): def cleanup(): for func in reversed(cleanups): func() else: cleanup = None return NativeValue( c.builder.load(value_ptr), cleanup=cleanup, is_error=c.builder.load(is_error_ptr), )
def unbox_slice(typ, obj, c): """ Convert object *obj* to a native slice structure. """ from numba.cpython import slicing ok, start, stop, step = c.pyapi.slice_as_ints(obj) sli = c.context.make_helper(c.builder, typ) sli.start = start sli.stop = stop sli.step = step return NativeValue(sli._getvalue(), is_error=c.builder.not_(ok))
def unbox_record(typ, obj, c): buf = c.pyapi.alloca_buffer() ptr = c.pyapi.extract_record_data(obj, buf) is_error = cgutils.is_null(c.builder, ptr) ltyp = c.context.get_value_type(typ) val = c.builder.bitcast(ptr, ltyp) def cleanup(): c.pyapi.release_buffer(buf) return NativeValue(val, cleanup=cleanup, is_error=is_error)
def unbox_integer(typ, obj, c): ll_type = c.context.get_argument_type(typ) val = cgutils.alloca_once(c.builder, ll_type) longobj = c.pyapi.number_long(obj) with c.pyapi.if_object_ok(longobj): if typ.signed: llval = c.pyapi.long_as_longlong(longobj) else: llval = c.pyapi.long_as_ulonglong(longobj) c.pyapi.decref(longobj) c.builder.store(c.builder.trunc(llval, ll_type), val) return NativeValue(c.builder.load(val), is_error=c.pyapi.c_api_error())
def unbox_funcptr(typ, obj, c): if typ.get_pointer is None: raise NotImplementedError(typ) # Call get_pointer() on the object to get the raw pointer value ptrty = c.context.get_function_pointer_type(typ) ret = cgutils.alloca_once_value(c.builder, ir.Constant(ptrty, None), name="fnptr") ser = c.pyapi.serialize_object(typ.get_pointer) get_pointer = c.pyapi.unserialize(ser) with cgutils.if_likely(c.builder, cgutils.is_not_null(c.builder, get_pointer)): intobj = c.pyapi.call_function_objargs(get_pointer, (obj,)) c.pyapi.decref(get_pointer) with cgutils.if_likely(c.builder, cgutils.is_not_null(c.builder, intobj)): ptr = c.pyapi.long_as_voidptr(intobj) c.pyapi.decref(intobj) c.builder.store(c.builder.bitcast(ptr, ptrty), ret) return NativeValue(c.builder.load(ret), is_error=c.pyapi.c_api_error())
def unbox_array(typ, obj, c): """ Convert a Numpy array object to a native array structure. """ # This is necessary because unbox_buffer() does not work on some # dtypes, e.g. datetime64 and timedelta64. # TODO check matching dtype. # currently, mismatching dtype will still work and causes # potential memory corruption nativearycls = c.context.make_array(typ) nativeary = nativearycls(c.context, c.builder) aryptr = nativeary._getpointer() ptr = c.builder.bitcast(aryptr, c.pyapi.voidptr) if c.context.enable_nrt: errcode = c.pyapi.nrt_adapt_ndarray_from_python(obj, ptr) else: errcode = c.pyapi.numba_array_adaptor(obj, ptr) # TODO: here we have minimal typechecking by the itemsize. # need to do better try: expected_itemsize = numpy_support.as_dtype(typ.dtype).itemsize except NotImplementedError: # Don't check types that can't be `as_dtype()`-ed itemsize_mismatch = cgutils.false_bit else: expected_itemsize = nativeary.itemsize.type(expected_itemsize) itemsize_mismatch = c.builder.icmp_unsigned( '!=', nativeary.itemsize, expected_itemsize, ) failed = c.builder.or_( cgutils.is_not_null(c.builder, errcode), itemsize_mismatch, ) # Handle error with c.builder.if_then(failed, likely=False): c.pyapi.err_set_string( "PyExc_TypeError", "can't unbox array from PyObject into " "native value. The object maybe of a " "different type") return NativeValue(c.builder.load(aryptr), is_error=failed)
def unbox_complex(typ, obj, c): # First unbox to complex128, since that's what CPython gives us c128 = c.context.make_complex(c.builder, types.complex128) ok = c.pyapi.complex_adaptor(obj, c128._getpointer()) failed = cgutils.is_false(c.builder, ok) with cgutils.if_unlikely(c.builder, failed): c.pyapi.err_set_string("PyExc_TypeError", "conversion to %s failed" % (typ,)) if typ == types.complex64: # Downcast to complex64 if necessary cplx = c.context.make_complex(c.builder, typ) cplx.real = c.context.cast(c.builder, c128.real, types.float64, types.float32) cplx.imag = c.context.cast(c.builder, c128.imag, types.float64, types.float32) else: assert typ == types.complex128 cplx = c128 return NativeValue(cplx._getvalue(), is_error=failed)
def unbox_dispatcher(typ, obj, c): # In native code, Dispatcher types can be casted to FunctionType. return NativeValue(obj)
def unbox_string_literal(typ, obj, c): # A string literal is a dummy value return NativeValue(c.context.get_dummy_value())
def unbox_number_class(typ, val, c): return NativeValue(c.context.get_dummy_value())
def unbox_boolean(typ, obj, c): istrue = c.pyapi.object_istrue(obj) zero = ir.Constant(istrue.type, 0) val = c.builder.icmp_signed('!=', istrue, zero) return NativeValue(val, is_error=c.pyapi.c_api_error())
def unbox_nptimedelta(typ, obj, c): val = c.pyapi.extract_np_timedelta(obj) return NativeValue(val, is_error=c.pyapi.c_api_error())
def unbox_npdatetime(typ, obj, c): val = c.pyapi.extract_np_datetime(obj) return NativeValue(val, is_error=c.pyapi.c_api_error())
def unbox_none(typ, val, c): return NativeValue(c.context.get_dummy_value())
def unbox_typeref(typ, val, c): return NativeValue(c.context.get_dummy_value(), is_error=cgutils.false_bit)
def unbox_numpy_random_bitgenerator(typ, obj, c): """ The bit_generator instance has a `.ctypes` attr which is a namedtuple with the following members (types): * state_address (Python int) * state (ctypes.c_void_p) * next_uint64 (ctypes.CFunctionType instance) * next_uint32 (ctypes.CFunctionType instance) * next_double (ctypes.CFunctionType instance) * bit_generator (ctypes.c_void_p) """ is_error_ptr = cgutils.alloca_once_value(c.builder, cgutils.false_bit) extra_refs = [] def clear_extra_refs(): for _ref in extra_refs: c.pyapi.decref(_ref) def handle_failure(): c.builder.store(cgutils.true_bit, is_error_ptr) clear_extra_refs() with ExitStack() as stack: def object_getattr_safely(obj, attr): attr_obj = c.pyapi.object_getattr_string(obj, attr) extra_refs.append(attr_obj) return attr_obj struct_ptr = cgutils.create_struct_proxy(typ)(c.context, c.builder) struct_ptr.parent = obj # Get the .ctypes attr ctypes_binding = object_getattr_safely(obj, 'ctypes') with early_exit_if_null(c.builder, stack, ctypes_binding): handle_failure() # Look up the "state_address" member and wire it into the struct interface_state_address = object_getattr_safely( ctypes_binding, 'state_address') with early_exit_if_null(c.builder, stack, interface_state_address): handle_failure() setattr(struct_ptr, 'state_address', c.unbox(types.uintp, interface_state_address).value) # Look up the "state" member and wire it into the struct interface_state = object_getattr_safely(ctypes_binding, 'state') with early_exit_if_null(c.builder, stack, interface_state): handle_failure() interface_state_value = object_getattr_safely( interface_state, 'value') with early_exit_if_null(c.builder, stack, interface_state_value): handle_failure() setattr( struct_ptr, 'state', c.unbox( types.uintp, interface_state_value).value) # Want to store callable function pointers to these CFunctionTypes, so # import ctypes and use it to cast the CFunctionTypes to c_void_p and # store the results. # First find ctypes.cast, and ctypes.c_void_p ctypes_name = c.context.insert_const_string(c.builder.module, 'ctypes') ctypes_module = c.pyapi.import_module_noblock(ctypes_name) extra_refs.append(ctypes_module) with early_exit_if_null(c.builder, stack, ctypes_module): handle_failure() ct_cast = object_getattr_safely(ctypes_module, 'cast') with early_exit_if_null(c.builder, stack, ct_cast): handle_failure() ct_voidptr_ty = object_getattr_safely(ctypes_module, 'c_void_p') with early_exit_if_null(c.builder, stack, ct_voidptr_ty): handle_failure() # This wires in the fnptrs referred to by name def wire_in_fnptrs(name): # Find the CFunctionType function interface_next_fn = c.pyapi.object_getattr_string( ctypes_binding, name) extra_refs.append(interface_next_fn) with early_exit_if_null(c.builder, stack, interface_next_fn): handle_failure() # Want to do ctypes.cast(CFunctionType, ctypes.c_void_p), create an # args tuple for that. extra_refs.append(ct_voidptr_ty) args = c.pyapi.tuple_pack([interface_next_fn, ct_voidptr_ty]) with early_exit_if_null(c.builder, stack, args): handle_failure() extra_refs.append(ct_voidptr_ty) # Call ctypes.cast() interface_next_fn_casted = c.pyapi.call(ct_cast, args) # Fetch the .value attr on the resulting ctypes.c_void_p for storage # in the function pointer slot. interface_next_fn_casted_value = object_getattr_safely( interface_next_fn_casted, 'value') with early_exit_if_null(c.builder, stack, interface_next_fn_casted_value): handle_failure() # Wire up setattr(struct_ptr, f'fnptr_{name}', c.unbox(types.uintp, interface_next_fn_casted_value).value) wire_in_fnptrs('next_double') wire_in_fnptrs('next_uint64') wire_in_fnptrs('next_uint32') clear_extra_refs() return NativeValue(struct_ptr._getvalue(), is_error=c.builder.load(is_error_ptr))
def unbox_pyobject(typ, obj, c): return NativeValue(obj)
def unbox_unsupported(typ, obj, c): c.pyapi.err_set_string("PyExc_TypeError", "can't unbox {!r} type".format(typ)) res = c.context.get_constant_null(typ) return NativeValue(res, is_error=cgutils.true_bit)
def unbox_meminfo_pointer(typ, obj, c): res = c.pyapi.nrt_meminfo_from_pyobject(obj) errored = cgutils.is_null(c.builder, res) return NativeValue(res, is_error=errored)
def unbox_pyarrow_table(typ, val, c): # incref pyobject, as Numba releases it when returning from objmode c.pyapi.incref(val) return NativeValue(val)
def unbox_dispatcher(typ, obj, c): # A dispatcher object has no meaningful value in native code res = c.context.get_constant_undef(typ) return NativeValue(res)