def unbox_pandas_timestamp(typ, val, c): year_obj = c.pyapi.object_getattr_string(val, "year") month_obj = c.pyapi.object_getattr_string(val, "month") day_obj = c.pyapi.object_getattr_string(val, "day") hour_obj = c.pyapi.object_getattr_string(val, "hour") minute_obj = c.pyapi.object_getattr_string(val, "minute") second_obj = c.pyapi.object_getattr_string(val, "second") microsecond_obj = c.pyapi.object_getattr_string(val, "microsecond") nanosecond_obj = c.pyapi.object_getattr_string(val, "nanosecond") pd_timestamp = cgutils.create_struct_proxy(typ)(c.context, c.builder) pd_timestamp.year = c.pyapi.long_as_longlong(year_obj) pd_timestamp.month = c.pyapi.long_as_longlong(month_obj) pd_timestamp.day = c.pyapi.long_as_longlong(day_obj) pd_timestamp.hour = c.pyapi.long_as_longlong(hour_obj) pd_timestamp.minute = c.pyapi.long_as_longlong(minute_obj) pd_timestamp.second = c.pyapi.long_as_longlong(second_obj) pd_timestamp.microsecond = c.pyapi.long_as_longlong(microsecond_obj) pd_timestamp.nanosecond = c.pyapi.long_as_longlong(nanosecond_obj) c.pyapi.decref(year_obj) c.pyapi.decref(month_obj) c.pyapi.decref(day_obj) c.pyapi.decref(hour_obj) c.pyapi.decref(minute_obj) c.pyapi.decref(second_obj) c.pyapi.decref(microsecond_obj) c.pyapi.decref(nanosecond_obj) is_error = cgutils.is_not_null(c.builder, c.pyapi.err_occurred()) return NativeValue(pd_timestamp._getvalue(), is_error=is_error)
def unbox_series(typ, val, c): arr_obj = c.pyapi.object_getattr_string(val, "values") series = cgutils.create_struct_proxy(typ)(c.context, c.builder) series.data = _unbox_series_data(typ.dtype, typ.data, arr_obj, c).value # TODO: handle index and name c.pyapi.decref(arr_obj) return NativeValue(series._getvalue())
def unbox_execution_progress(typ, obj, c): """ Convert a ExecutionProgress object to a native structure. """ output_progress_to_console_obj = c.pyapi.object_getattr_string( obj, "output_progress_to_console") is_true = c.pyapi.object_istrue(output_progress_to_console_obj) zero = ir.Constant(is_true.type, 0) lower_bound_obj = c.pyapi.object_getattr_string(obj, "lower_bound") number_of_decimal_places_obj = c.pyapi.object_getattr_string( obj, "number_of_decimal_places") execution_progress = cgutils.create_struct_proxy(typ)(c.context, c.builder) execution_progress.output_progress_to_console = c.builder.icmp_signed( '!=', is_true, zero) execution_progress.lower_bound = c.pyapi.float_as_double(lower_bound_obj) ll_type = c.context.get_argument_type(types.intc) val = cgutils.alloca_once(c.builder, ll_type) longobj = c.pyapi.number_long(number_of_decimal_places_obj) with c.pyapi.if_object_ok(longobj): llval = c.pyapi.long_as_longlong(longobj) c.pyapi.decref(longobj) c.builder.store(c.builder.trunc(llval, ll_type), val) execution_progress.number_of_decimal_places = c.builder.load(val) c.pyapi.decref(lower_bound_obj) is_error = cgutils.is_not_null(c.builder, c.pyapi.err_occurred()) return NativeValue(execution_progress._getvalue(), is_error=is_error)
def _unbox_native_field(typ, obj, field_name: str, c): ret_ptr = cgutils.alloca_once(c.builder, c.context.get_value_type(typ)) is_error_ptr = cgutils.alloca_once_value(c.builder, cgutils.false_bit) fail_obj = c.context.get_constant_null(typ) with local_return(c.builder) as ret: fail_blk = c.builder.append_basic_block("fail") with c.builder.goto_block(fail_blk): c.builder.store(cgutils.true_bit, is_error_ptr) c.builder.store(fail_obj, ret_ptr) ret() field_obj = c.pyapi.object_getattr_string(obj, field_name) with cgutils.if_unlikely(c.builder, cgutils.is_null(c.builder, field_obj)): c.builder.branch(fail_blk) field_native = c.unbox(typ, field_obj) c.pyapi.decref(field_obj) with cgutils.if_unlikely(c.builder, field_native.is_error): c.builder.branch(fail_blk) c.builder.store(cgutils.false_bit, is_error_ptr) c.builder.store(field_native.value, ret_ptr) return NativeValue(c.builder.load(ret_ptr), is_error=c.builder.load(is_error_ptr))
def unbox_dicttype(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) dstruct = ctor(context, builder) data_pointer = context.nrt.meminfo_data(builder, mi) data_pointer = builder.bitcast( data_pointer, dictobject.ll_dict_type.as_pointer(), ) dstruct.data = builder.load(data_pointer) dstruct.meminfo = mi dctobj = dstruct._getvalue() c.pyapi.decref(miptr) return NativeValue(dctobj)
def unbox_dataframe(typ, val, c): """unbox dataframe to an empty DataFrame struct columns will be extracted later if necessary. """ n_cols = len(typ.columns) column_strs = [numba.unicode.make_string_from_constant( c.context, c.builder, string_type, a) for a in typ.columns] # create dataframe struct and store values dataframe = cgutils.create_struct_proxy( typ)(c.context, c.builder) column_tup = c.context.make_tuple( c.builder, types.UniTuple(string_type, n_cols), column_strs) zero = c.context.get_constant(types.int8, 0) unboxed_tup = c.context.make_tuple( c.builder, types.UniTuple(types.int8, n_cols + 1), [zero] * (n_cols + 1)) # TODO: support unboxing index if typ.index == types.none: dataframe.index = c.context.get_constant(types.none, None) dataframe.columns = column_tup dataframe.unboxed = unboxed_tup dataframe.parent = val # increase refcount of stored values if c.context.enable_nrt: # TODO: other objects? for var in column_strs: c.context.nrt.incref(c.builder, string_type, var) return NativeValue(dataframe._getvalue())
def unbox_(typ, obj, c): ptr_obj = c.pyapi.object_getattr_string(obj, "address") ctx = cgutils.create_struct_proxy(typ)(c.context, c.builder) ctx.ptr = c.pyapi.long_as_voidptr(ptr_obj) c.pyapi.decref(ptr_obj) is_error = cgutils.is_not_null(c.builder, c.pyapi.err_occurred()) return NativeValue(ctx._getvalue(), is_error=is_error)
def unbox_range_index(typ, val, c): start_obj = c.pyapi.object_getattr_string(val, "start") stop_obj = c.pyapi.object_getattr_string(val, "stop") step_obj = c.pyapi.object_getattr_string(val, "step") # TODO: support range unboxing with reference to parent in Numba? range_index = cgutils.create_struct_proxy(typ)(c.context, c.builder) range_index_data = cgutils.create_struct_proxy(RangeIndexDataType)( c.context, c.builder) range_index_data.start = c.pyapi.long_as_longlong(start_obj) range_index_data.stop = c.pyapi.long_as_longlong(stop_obj) range_index_data.step = c.pyapi.long_as_longlong(step_obj) range_index.data = range_index_data._getvalue() if typ.is_named: name_obj = c.pyapi.object_getattr_string(val, "name") range_index.name = numba.cpython.unicode.unbox_unicode_str( types.unicode_type, name_obj, c).value c.pyapi.decref(name_obj) c.pyapi.decref(start_obj) c.pyapi.decref(stop_obj) c.pyapi.decref(step_obj) is_error = cgutils.is_not_null(c.builder, c.pyapi.err_occurred()) return NativeValue(range_index._getvalue(), is_error=is_error)
def unbox_positional_index(typ, val, c): positional_index = cgutils.create_struct_proxy(typ)(c.context, c.builder) res = unbox_range_index(typ.data, val, c) positional_index.data = res.value is_error = res.is_error return NativeValue(positional_index._getvalue(), is_error=is_error)
def unbox_me(typ, obj, c): 'Call C++ unboxing function and return as NativeValue' # Define the signature fnty = lir.FunctionType(lir.IntType(8).as_pointer(), [lir.IntType(8).as_pointer(),]) # Get function fn = c.builder.module.get_or_insert_function(fnty, name='unbox_'+self.spec.c_name) # finally generate the call ptr = c.builder.call(fn, [obj]) return NativeValue(ptr, is_error=c.pyapi.c_api_error())
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_type(typ, obj, c): ctx = cgutils.create_struct_proxy(typ)(c.context, c.builder) ctx.ptr = call_raw_function_pointer( addr_func_c, ir.FunctionType(ir.PointerType(ir.IntType(8)), (ir.PointerType(ir.IntType(8)),)), (obj,), c.builder, ) return NativeValue(ctx._getvalue())
def unbox_MultiVector(typ: MultiVectorType, obj: MultiVector, c) -> NativeValue: value = c.pyapi.object_getattr_string(obj, "value") layout = c.pyapi.object_getattr_string(obj, "layout") mv = cgutils.create_struct_proxy(typ)(c.context, c.builder) mv.layout = c.unbox(typ.layout_type, layout).value mv.value = c.unbox(typ.value_type, value).value c.pyapi.decref(value) c.pyapi.decref(layout) is_error = cgutils.is_not_null(c.builder, c.pyapi.err_occurred()) return NativeValue(mv._getvalue(), is_error=is_error)
def unboxer(typ, obj, c): # The unboxer that calls some jitcode def bridge(x): if x > 0: raise ValueError('cannot be x > 0') return x args = [c.context.get_constant(types.intp, 1)] sig = signature(types.voidptr, types.intp) is_error, res = c.pyapi.call_jit_code(bridge, sig, args) return NativeValue(res, is_error=is_error)
def unboxer(typ, obj, c): # The unboxer that calls some jitcode def bridge(x): # proof that this is a jit'ed context by calling jit only # intrinsic return my_intrinsic(x) args = [c.context.get_constant(types.intp, magic_token)] sig = signature(types.voidptr, types.intp) is_error, res = c.pyapi.call_jit_code(bridge, sig, args) return NativeValue(res, is_error=is_error)
def unbox_string(typ, obj, c): """ """ ok, buffer, size = c.pyapi.string_as_string_and_size(obj) fnty = lir.FunctionType(lir.IntType(8).as_pointer(), [lir.IntType(8).as_pointer(), lir.IntType(64)]) fn = c.builder.module.get_or_insert_function(fnty, name="init_string") ret = c.builder.call(fn, [buffer, size]) return NativeValue(ret, is_error=c.builder.not_(ok))
def unbox_random_state(typ, obj, c): """Convert a `RandomState` object to a native `RandomStateNumbaModel` structure. Note that this will create a 'fake' structure which will just get the `RandomState` objects accepted in Numba functions but the actual information of the Numba's random state is stored internally and can be accessed anytime using ``numba._helperlib.rnd_get_np_state_ptr()``. """ interval = cgutils.create_struct_proxy(typ)(c.context, c.builder) is_error = cgutils.is_not_null(c.builder, c.pyapi.err_occurred()) return NativeValue(interval._getvalue(), is_error=is_error)
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_empty_index(typ, val, c): index_struct = cgutils.create_struct_proxy(typ)(c.context, c.builder) if typ.is_named: name_obj = c.pyapi.object_getattr_string(val, "name") index_struct.name = numba.cpython.unicode.unbox_unicode_str( types.unicode_type, name_obj, c).value c.pyapi.decref(name_obj) is_error = cgutils.is_not_null(c.builder, c.pyapi.err_occurred()) return NativeValue(index_struct._getvalue(), is_error=is_error)
def unbox_dataframe(typ, val, c): """unbox dataframe to an empty DataFrame struct columns will be extracted later if necessary. """ n_cols = len(typ.columns) column_strs = [ numba.unicode.make_string_from_constant(c.context, c.builder, string_type, a) for a in typ.columns ] # create dataframe struct and store values dataframe = cgutils.create_struct_proxy(typ)(c.context, c.builder) column_tup = c.context.make_tuple(c.builder, types.UniTuple(string_type, n_cols), column_strs) # this unboxes all DF columns so that no column unboxing occurs later for col_ind in range(n_cols): series_obj = c.pyapi.object_getattr_string(val, typ.columns[col_ind]) arr_obj = c.pyapi.object_getattr_string(series_obj, "values") ty_series = typ.data[col_ind] if isinstance(ty_series, types.Array): native_val = unbox_array(typ.data[col_ind], arr_obj, c) elif ty_series == string_array_type: native_val = unbox_str_series(string_array_type, series_obj, c) dataframe.data = c.builder.insert_value(dataframe.data, native_val.value, col_ind) # TODO: support unboxing index if typ.index == types.none: dataframe.index = c.context.get_constant(types.none, None) if typ.index == string_array_type: index_obj = c.pyapi.object_getattr_string(val, "index") dataframe.index = unbox_str_series(string_array_type, index_obj, c).value if isinstance(typ.index, types.Array): index_obj = c.pyapi.object_getattr_string(val, "index") index_data = c.pyapi.object_getattr_string(index_obj, "_data") dataframe.index = unbox_array(typ.index, index_data, c).value dataframe.columns = column_tup dataframe.parent = val # increase refcount of stored values if c.context.enable_nrt: # TODO: other objects? for var in column_strs: c.context.nrt.incref(c.builder, string_type, var) return NativeValue(dataframe._getvalue())
def unbox_interval(typ, obj, c): """ Convert a Interval object to a native interval structure. """ lo_obj = c.pyapi.object_getattr_string(obj, "lo") hi_obj = c.pyapi.object_getattr_string(obj, "hi") interval = cgutils.create_struct_proxy(typ)(c.context, c.builder) interval.lo = c.pyapi.float_as_double(lo_obj) interval.hi = c.pyapi.float_as_double(hi_obj) c.pyapi.decref(lo_obj) c.pyapi.decref(hi_obj) is_error = cgutils.is_not_null(c.builder, c.pyapi.err_occurred()) return NativeValue(interval._getvalue(), is_error=is_error)
def unbox_doubledouble(typ, obj, c): """ Convert a DoubleDouble object to a native doubledouble structure. """ x_obj = c.pyapi.object_getattr_string(obj, "x") y_obj = c.pyapi.object_getattr_string(obj, "y") doubledouble = cgutils.create_struct_proxy(typ)(c.context, c.builder) doubledouble.x = c.pyapi.float_as_double(x_obj) doubledouble.y = c.pyapi.float_as_double(y_obj) c.pyapi.decref(x_obj) c.pyapi.decref(y_obj) is_error = cgutils.is_not_null(c.builder, c.pyapi.err_occurred()) return NativeValue(doubledouble._getvalue(), is_error=is_error)
def unbox_dataframe(typ, val, c): """unbox dataframe to an empty DataFrame struct columns will be extracted later if necessary. """ n_cols = len(typ.columns) # create dataframe struct and store values dataframe = cgutils.create_struct_proxy(typ)(c.context, c.builder) errorptr = cgutils.alloca_once_value(c.builder, cgutils.false_bit) _, data_typs_map, types_order = get_structure_maps(typ.data, typ.columns) for col_typ in types_order: type_id, col_indices = data_typs_map[col_typ] n_type_cols = len(col_indices) list_type = types.List(col_typ) ok, inst = listobj.ListInstance.allocate_ex(c.context, c.builder, list_type, n_type_cols) with c.builder.if_else(ok, likely=True) as (if_ok, if_not_ok): with if_ok: inst.size = c.context.get_constant(types.intp, n_type_cols) for i, col_idx in enumerate(col_indices): series_obj = c.pyapi.object_getattr_string(val, typ.columns[col_idx]) arr_obj = c.pyapi.object_getattr_string(series_obj, "values") ty_series = typ.data[col_idx] # FIXME: CategoricalType has wrong dtype attribute value (i.e. dtype of codes) # current implementation offers pd_dtype for this purpose, so use it column_dtype = ty_series.pd_dtype if isinstance(ty_series, Categorical) else ty_series.dtype native_val = _unbox_series_data(column_dtype, ty_series, arr_obj, c) inst.setitem(c.context.get_constant(types.intp, i), native_val.value, incref=False) c.pyapi.decref(arr_obj) c.pyapi.decref(series_obj) dataframe.data = c.builder.insert_value(dataframe.data, inst.value, type_id) with if_not_ok: c.builder.store(cgutils.true_bit, errorptr) # If an error occurred, drop the whole native list with c.builder.if_then(c.builder.load(errorptr)): c.context.nrt.decref(c.builder, list_type, inst.value) index_obj = c.pyapi.object_getattr_string(val, "index") dataframe.index = _unbox_index_data(typ.index, index_obj, c).value c.pyapi.decref(index_obj) dataframe.parent = val return NativeValue(dataframe._getvalue(), is_error=c.builder.load(errorptr))
def unbox_series(typ, val, c): arr_obj = c.pyapi.object_getattr_string(val, "values") series = cgutils.create_struct_proxy(typ)(c.context, c.builder) series.data = _unbox_series_data(typ.dtype, typ.data, arr_obj, c).value # TODO: other indices if typ.index == string_array_type: index_obj = c.pyapi.object_getattr_string(val, "index") series.index = unbox_str_series(string_array_type, index_obj, c).value if typ.is_named: name_obj = c.pyapi.object_getattr_string(val, "name") series.name = numba.unicode.unbox_unicode_str( string_type, name_obj, c).value # TODO: handle index and name c.pyapi.decref(arr_obj) return NativeValue(series._getvalue())
def unbox_datetime_date_array(typ, val, c): # n = object_length(c, val) #cgutils.printf(c.builder, "len %d\n", n) arr_typ = types.Array(types.intp, 1, 'C') out_arr = _empty_nd_impl(c.context, c.builder, arr_typ, [n]) with cgutils.for_range(c.builder, n) as loop: dt_date = sequence_getitem(c, val, loop.index) int_date = unbox_datetime_date(datetime_date_type, dt_date, c).value dataptr, shapes, strides = basic_indexing( c.context, c.builder, arr_typ, out_arr, (types.intp,), (loop.index,)) store_item(c.context, c.builder, arr_typ, int_date, dataptr) is_error = cgutils.is_not_null(c.builder, c.pyapi.err_occurred()) return NativeValue(out_arr._getvalue(), is_error=is_error)
def unbox_int64_index(typ, val, c): # TODO: support index unboxing with reference to parent in Numba? int64_index = cgutils.create_struct_proxy(typ)(c.context, c.builder) index_data = c.pyapi.object_getattr_string(val, "_data") int64_index.data = unbox_array(typ.data, index_data, c).value c.pyapi.decref(index_data) if typ.is_named: name_obj = c.pyapi.object_getattr_string(val, "name") int64_index.name = numba.cpython.unicode.unbox_unicode_str( types.unicode_type, name_obj, c).value c.pyapi.decref(name_obj) is_error = cgutils.is_not_null(c.builder, c.pyapi.err_occurred()) return NativeValue(int64_index._getvalue(), is_error=is_error)
def _unbox_series_data(dtype, data_typ, arr_obj, c): if data_typ == string_array_type: return unbox_str_series(string_array_type, arr_obj, c) elif dtype == datetime_date_type: return unbox_datetime_date_array(data_typ, arr_obj, c) elif data_typ == list_string_array_type: return _unbox_array_list_str(arr_obj, c) elif data_typ == string_array_split_view_type: # XXX dummy unboxing to avoid errors in _get_dataframe_data() out_view = c.context.make_helper(c.builder, string_array_split_view_type) return NativeValue(out_view._getvalue()) elif isinstance(dtype, PDCategoricalDtype): return unbox_categorical_array(data_typ, arr_obj, c) # TODO: error handling like Numba callwrappers.py return unbox_array(data_typ, arr_obj, c)
def unbox_unicode_str(typ, obj, c): """ Convert a unicode str object to a native unicode structure. """ ok, data, length, kind = c.pyapi.string_as_string_size_and_kind(obj) uni_str = cgutils.create_struct_proxy(typ)(c.context, c.builder) uni_str.data = data uni_str.length = length uni_str.kind = kind uni_str.meminfo = c.pyapi.nrt_meminfo_new_from_pyobject( data, # the borrowed data pointer obj, # the owner pyobject; the call will incref it. ) uni_str.parent = obj is_error = cgutils.is_not_null(c.builder, c.pyapi.err_occurred()) return NativeValue(uni_str._getvalue(), is_error=is_error)
def _unbox_array_list_str(obj, c): # typ = list_string_array_type # from unbox_list errorptr = cgutils.alloca_once_value(c.builder, cgutils.false_bit) listptr = cgutils.alloca_once(c.builder, c.context.get_value_type(typ)) # get size of array arr_size_fnty = LLType.function(c.pyapi.py_ssize_t, [c.pyapi.pyobj]) arr_size_fn = c.pyapi._get_function(arr_size_fnty, name="array_size") size = c.builder.call(arr_size_fn, [obj]) # cgutils.printf(c.builder, 'size %d\n', size) _python_array_obj_to_native_list(typ, obj, c, size, listptr, errorptr) return NativeValue(c.builder.load(listptr), is_error=c.builder.load(errorptr))
def unbox_series(typ, val, c): arr_obj = c.pyapi.object_getattr_string(val, "values") series = cgutils.create_struct_proxy(typ)(c.context, c.builder) series.data = _unbox_series_data(typ.dtype, typ.data, arr_obj, c).value index_obj = c.pyapi.object_getattr_string(val, "index") series.index = _unbox_index_data(typ.index, index_obj, c).value if typ.is_named: name_obj = c.pyapi.object_getattr_string(val, "name") series.name = numba.cpython.unicode.unbox_unicode_str( string_type, name_obj, c).value c.pyapi.decref(name_obj) c.pyapi.decref(arr_obj) c.pyapi.decref(index_obj) return NativeValue(series._getvalue())