Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
optval = context.make_helper(builder, fromty, value=val)
validbit = cgutils.as_bool_bit(builder, optval.valid)
# Create uninitialized optional value
outoptval = context.make_helper(builder, toty)
with builder.if_else(validbit) as (is_valid, is_not_valid):
with is_valid:
# Cast internal value
outoptval.valid = cgutils.true_bit
outoptval.data = context.cast(builder, optval.data,
fromty.type, toty.type)
with is_not_valid:
# Store None to result
outoptval.valid = cgutils.false_bit
outoptval.data = cgutils.get_null_value(
outoptval.data.type)
return outoptval._getvalue()
def get_index_ptr(builder, state_ptr):
return cgutils.gep_inbounds(builder, state_ptr, 0, 0)
It dispatches to the appropriate 'print' implementations above
depending on the detected real types in the signature."""
vprint = nvvmutils.declare_vprint(builder.module)
formats = []
values = []
for i, (argtype, argval) in enumerate(zip(sig.args, args)):
argfmt, argvals = print_item(argtype, context, builder, argval)
formats.append(argfmt)
values.extend(argvals)
rawfmt = " ".join(formats) + "\n"
fmt = context.insert_string_const_addrspace(builder, rawfmt)
array = cgutils.make_anonymous_struct(builder, values)
arrayptr = cgutils.alloca_once_value(builder, array)
vprint = nvvmutils.declare_vprint(builder.module)
builder.call(vprint, (fmt, builder.bitcast(arrayptr, voidptr)))
return context.get_dummy_value()
indices = [inds] # just a single integer
indty = [indty]
else:
indices = cgutils.unpack_tuple(builder, inds, count=len(indty))
indices = [context.cast(builder, i, t, types.intp)
for t, i in zip(indty, indices)]
if dtype != valty:
raise TypeError("expecting %s but got %s" % (dtype, valty))
if aryty.ndim != len(indty):
raise TypeError("indexing %d-D array with %d-D index" %
(aryty.ndim, len(indty)))
lary = context.make_array(aryty)(context, builder, ary)
ptr = cgutils.get_item_pointer(context, builder, aryty, lary, indices)
return builder.atomic_rmw("add", ptr, val, ordering='monotonic')
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)
def _define_nrt_decref(module, atomic_decr):
"""
Implement NRT_decref in the module
"""
fn_decref = module.get_or_insert_function(incref_decref_ty,
name="NRT_decref")
# Cannot inline this for refcount pruning to work
fn_decref.attributes.add('noinline')
calldtor = module.add_function(ir.FunctionType(ir.VoidType(), [_pointer_type]),
name="NRT_MemInfo_call_dtor")
builder = ir.IRBuilder(fn_decref.append_basic_block())
[ptr] = fn_decref.args
is_null = builder.icmp_unsigned("==", ptr, cgutils.get_null_value(ptr.type))
with cgutils.if_unlikely(builder, is_null):
builder.ret_void()
if _debug_print:
cgutils.printf(builder, "*** NRT_Decref %zu [%p]\n", builder.load(ptr),
ptr)
if binding.get_process_triple().startswith("powerpc"):
builder.fence("release")
newrefct = builder.call(atomic_decr,
[builder.bitcast(ptr, atomic_decr.args[0].type)])
refct_eq_0 = builder.icmp_unsigned("==", newrefct,
ir.Constant(newrefct.type, 0))
with cgutils.if_unlikely(builder, refct_eq_0):
if binding.get_process_triple().startswith("powerpc"):
builder.fence("acquire")
def is_null(self, obj):
return cgutils.is_null(self.builder, obj)
def _conv_numba_struct_to_clang(builder, numba_arg, clang_arg_type):
stack_var = cgutils.alloca_once(builder, numba_arg.type)
builder.store(numba_arg, stack_var)
clang_arg = builder.bitcast(stack_var, clang_arg_type)
return clang_arg
def codegen(context, builder, signature, args):
in_tup = args[0]
data_arrs = [builder.extract_value(in_tup, i) for i in range(n_cols)]
index = builder.extract_value(in_tup, n_cols)
column_strs = [numba.unicode.make_string_from_constant(
context, builder, string_type, c) for c in column_names]
# create dataframe struct and store values
dataframe = cgutils.create_struct_proxy(
signature.return_type)(context, builder)
data_tup = context.make_tuple(
builder, types.Tuple(data_typs), data_arrs)
column_tup = context.make_tuple(
builder, types.UniTuple(string_type, n_cols), column_strs)
zero = context.get_constant(types.int8, 0)
unboxed_tup = context.make_tuple(
builder, types.UniTuple(types.int8, n_cols + 1), [zero] * (n_cols + 1))
dataframe.data = data_tup
dataframe.index = index
dataframe.columns = column_tup
dataframe.unboxed = unboxed_tup
dataframe.parent = context.get_constant_null(types.pyobject)
elif isinstance(ty, types.Array):
return self.context.make_constant_array(self.builder, ty,
value.value)
elif self.context.is_struct_type(ty):
return self.context.get_constant_struct(self.builder, ty,
value.value)
elif ty in types.number_domain:
return self.context.get_constant(ty, value.value)
elif isinstance(ty, types.UniTuple):
consts = [self.context.get_constant(t, v)
for t, v in zip(ty, value.value)]
return cgutils.pack_array(self.builder, consts)
elif self.context.is_struct_type(ty):
return self.context.get_constant_struct(self.builder, ty,
value.value)
else:
raise NotImplementedError('global', ty)
else:
raise NotImplementedError(type(value), value)