本文整理汇总了Python中numba.cgutils.is_not_null函数的典型用法代码示例。如果您正苦于以下问题:Python is_not_null函数的具体用法?Python is_not_null怎么用?Python is_not_null使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了is_not_null函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: codegen
def codegen(context, builder, signature, args):
instance, = args
# TODO: probably a more general way to do this
second_element = builder.extract_value(instance, [1])
result = cgutils.is_not_null(builder, second_element)
return result
示例2: raise_error
def raise_error(self, builder, api, status):
"""
Given a non-ok *status*, raise the corresponding Python exception.
"""
bbend = builder.function.append_basic_block()
with builder.if_then(status.is_user_exc):
# Unserialize user exception.
# Make sure another error may not interfere.
api.err_clear()
exc = api.unserialize(status.excinfoptr)
with cgutils.if_likely(builder,
cgutils.is_not_null(builder, exc)):
api.raise_object(exc) # steals ref
builder.branch(bbend)
with builder.if_then(status.is_stop_iteration):
api.err_set_none("PyExc_StopIteration")
builder.branch(bbend)
with builder.if_then(status.is_python_exc):
# Error already raised => nothing to do
builder.branch(bbend)
api.err_set_string("PyExc_SystemError",
"unknown error when calling native function")
builder.branch(bbend)
builder.position_at_end(bbend)
示例3: print_item_impl
def print_item_impl(context, builder, sig, args):
"""
Print a single native value by boxing it in a Python object and
invoking the Python interpreter's print routine.
"""
ty, = sig.args
val, = args
pyapi = context.get_python_api(builder)
if context.enable_nrt:
context.nrt.incref(builder, ty, val)
# XXX unfortunately, we don't have access to the env manager from here
obj = pyapi.from_native_value(ty, val)
with builder.if_else(cgutils.is_not_null(builder, obj), likely=True) as (if_ok, if_error):
with if_ok:
pyapi.print_object(obj)
pyapi.decref(obj)
with if_error:
cstr = context.insert_const_string(builder.module,
"the print() function")
strobj = pyapi.string_from_string(cstr)
pyapi.err_write_unraisable(strobj)
pyapi.decref(strobj)
res = context.get_dummy_value()
return impl_ret_untracked(context, builder, sig.return_type, res)
示例4: set_iter_valid
def set_iter_valid(self, state, item):
iterstate = PyIterState(self.context, self.builder, ref=state)
iterstate.valid = cgutils.as_bool_byte(self.builder,
cgutils.is_not_null(self.builder,
item))
with cgutils.if_unlikely(self.builder, self.is_null(item)):
self.check_occurred()
示例5: list_pack
def list_pack(self, items):
n = len(items)
seq = self.list_new(self.context.get_constant(types.intp, n))
not_null = cgutils.is_not_null(self.builder, seq)
with cgutils.if_likely(self.builder, not_null):
for i in range(n):
idx = self.context.get_constant(types.intp, i)
self.incref(items[i])
self.list_setitem(seq, idx, items[i])
return seq
示例6: check_lapack_return
def check_lapack_return(context, builder, res):
"""
Check the integer error return from one of the LAPACK wrappers in
_helperlib.c.
"""
with builder.if_then(cgutils.is_not_null(builder, res), likely=False):
# Those errors shouldn't happen, it's easier to just abort the process
pyapi = context.get_python_api(builder)
pyapi.gil_ensure()
pyapi.fatal_error("LAPACK wrapper returned with an error")
示例7: to_native_array
def to_native_array(self, ary, typ):
# TODO check matching dtype.
# currently, mismatching dtype will still work and causes
# potential memory corruption
nativearycls = self.context.make_array(typ)
nativeary = nativearycls(self.context, self.builder)
aryptr = nativeary._getpointer()
ptr = self.builder.bitcast(aryptr, self.voidptr)
errcode = self.numba_array_adaptor(ary, ptr)
failed = cgutils.is_not_null(self.builder, errcode)
return self.builder.load(aryptr), failed
示例8: dict_pack
def dict_pack(self, keyvalues):
"""
Args
-----
keyvalues: iterable of (str, llvm.Value of PyObject*)
"""
dictobj = self.dict_new()
not_null = cgutils.is_not_null(self.builder, dictobj)
with cgutils.if_likely(self.builder, not_null):
for k, v in keyvalues:
self.dict_setitem_string(dictobj, k, v)
return dictobj
示例9: store
def store(retval):
is_ok = cgutils.is_not_null(builder, retval)
# If an error is raised by the object mode ufunc, it will
# simply get caught by the Numpy ufunc machinery.
with builder.if_then(is_ok, likely=True):
# Unbox
native = pyapi.to_native_value(signature.return_type, retval)
assert native.cleanup is None
# Store
out.store_direct(native.value, builder.load(store_offset))
# Release owned reference
pyapi.decref(retval)
示例10: is_leap_year
def is_leap_year(builder, year_val):
"""
Return a predicate indicating whether *year_val* (offset by 1970) is a
leap year.
"""
actual_year = builder.add(year_val, Constant.int(DATETIME64, 1970))
multiple_of_4 = cgutils.is_null(
builder, builder.and_(actual_year, Constant.int(DATETIME64, 3)))
not_multiple_of_100 = cgutils.is_not_null(
builder, builder.srem(actual_year, Constant.int(DATETIME64, 100)))
multiple_of_400 = cgutils.is_null(
builder, builder.srem(actual_year, Constant.int(DATETIME64, 400)))
return builder.and_(multiple_of_4,
builder.or_(not_multiple_of_100, multiple_of_400))
示例11: to_native_array
def to_native_array(self, typ, ary):
# TODO check matching dtype.
# currently, mismatching dtype will still work and causes
# potential memory corruption
voidptr = Type.pointer(Type.int(8))
nativearycls = self.context.make_array(typ)
nativeary = nativearycls(self.context, self.builder)
aryptr = nativeary._getpointer()
ptr = self.builder.bitcast(aryptr, voidptr)
errcode = self.numba_array_adaptor(ary, ptr)
failed = cgutils.is_not_null(self.builder, errcode)
with cgutils.if_unlikely(self.builder, failed):
# TODO
self.builder.unreachable()
return self.builder.load(aryptr)
示例12: make_exception_switch
def make_exception_switch(self, api, builder, status):
"""
Handle user exceptions. Unserialize the exception info and raise it.
"""
code = status.code
# Handle user exceptions
with builder.if_then(status.is_user_exc):
exc = api.unserialize(status.excinfoptr)
with cgutils.if_likely(builder, cgutils.is_not_null(builder, exc)):
api.raise_object(exc) # steals ref
builder.ret(api.get_null_object())
with builder.if_then(status.is_stop_iteration):
api.err_set_none("PyExc_StopIteration")
builder.ret(api.get_null_object())
msg = "unknown error in native function: %s" % self.fndesc.mangled_name
api.err_set_string("PyExc_SystemError", msg)
示例13: to_native_buffer
def to_native_buffer(self, obj, typ):
buf = self.alloca_buffer()
res = self.get_buffer(obj, buf)
is_error = cgutils.is_not_null(self.builder, res)
nativearycls = self.context.make_array(typ)
nativeary = nativearycls(self.context, self.builder)
aryptr = nativeary._getpointer()
with cgutils.if_likely(self.builder, self.builder.not_(is_error)):
ptr = self.builder.bitcast(aryptr, self.voidptr)
self.numba_buffer_adaptor(buf, ptr)
def cleanup():
self.release_buffer(buf)
return NativeValue(self.builder.load(aryptr), is_error=is_error,
cleanup=cleanup)
示例14: unbox_unicode_str
def unbox_unicode_str(typ, obj, c):
"""
Convert a unicode str object to a native unicode structure.
"""
ok, data, length, kind, hashv = 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.hash = hashv
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)
示例15: _emit_python_wrapper
def _emit_python_wrapper(self, llvm_module):
# Figure out the Python C API module creation function, and
# get a LLVM function for it.
create_module_fn = llvm_module.add_function(*self.module_create_definition)
create_module_fn.linkage = lc.LINKAGE_EXTERNAL
# Define a constant string for the module name.
mod_name_const = self.context.insert_const_string(llvm_module,
self.module_name)
mod_def_base_init = lc.Constant.struct(
(lt._pyobject_head_init, # PyObject_HEAD
lc.Constant.null(self.m_init_ty), # m_init
lc.Constant.null(lt._llvm_py_ssize_t), # m_index
lc.Constant.null(lt._pyobject_head_p), # m_copy
)
)
mod_def_base = llvm_module.add_global_variable(mod_def_base_init.type,
'.module_def_base')
mod_def_base.initializer = mod_def_base_init
mod_def_base.linkage = lc.LINKAGE_INTERNAL
method_array = self._emit_method_array(llvm_module)
mod_def_init = lc.Constant.struct(
(mod_def_base_init, # m_base
mod_name_const, # m_name
lc.Constant.null(self._char_star), # m_doc
lc.Constant.int(lt._llvm_py_ssize_t, -1), # m_size
method_array, # m_methods
lc.Constant.null(self.inquiry_ty), # m_reload
lc.Constant.null(self.traverseproc_ty), # m_traverse
lc.Constant.null(self.inquiry_ty), # m_clear
lc.Constant.null(self.freefunc_ty) # m_free
)
)
# Define a constant string for the module name.
mod_def = llvm_module.add_global_variable(mod_def_init.type,
'.module_def')
mod_def.initializer = mod_def_init
mod_def.linkage = lc.LINKAGE_INTERNAL
# Define the module initialization function.
mod_init_fn = llvm_module.add_function(*self.module_init_definition)
entry = mod_init_fn.append_basic_block('Entry')
builder = lc.Builder(entry)
pyapi = self.context.get_python_api(builder)
mod = builder.call(create_module_fn,
(mod_def,
lc.Constant.int(lt._int32, sys.api_version)))
# Test if module has been created correctly.
# (XXX for some reason comparing with the NULL constant fails llvm
# with an assertion in pydebug mode)
with builder.if_then(cgutils.is_null(builder, mod)):
builder.ret(NULL.bitcast(mod_init_fn.type.pointee.return_type))
env_array = self._emit_environment_array(llvm_module, builder, pyapi)
ret = self._emit_module_init_code(llvm_module, builder, mod,
method_array, env_array)
if ret is not None:
with builder.if_then(cgutils.is_not_null(builder, ret)):
# Init function errored out
builder.ret(lc.Constant.null(mod.type))
builder.ret(mod)
self.dll_exports.append(mod_init_fn.name)