本文整理汇总了Python中numba.nodes.const函数的典型用法代码示例。如果您正苦于以下问题:Python const函数的具体用法?Python const怎么用?Python const使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了const函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: str_to_int
def str_to_int(self, dst_type, node):
# TODO: int <-> string conversions are explicit, this should not
# TODO: be a coercion
if self.nopython:
node = nodes.CoercionNode(
function_util.external_call(
self.context,
self.llvm_module,
('atol' if dst_type.is_int else 'atof'),
args=[node.node]),
dst_type, name=node.name, )
else:
if dst_type.is_int:
cvtobj = function_util.external_call(
self.context,
self.llvm_module,
'PyInt_FromString' if not PY3 else 'PyLong_FromString',
args=[node.node, nodes.NULL,
nodes.const(10, int_)])
else:
cvtobj = function_util.external_call(
self.context,
self.llvm_module,
'PyFloat_FromString',
args=[node.node,
nodes.const(0, Py_ssize_t)])
node = nodes.CoerceToNative(nodes.ObjectTempNode(cvtobj),
dst_type, name=node.name)
result = self.visit(node)
return result
示例2: _c_string_slice
def _c_string_slice(self, node):
ret_val = node
logger.debug(node.slice)
node_slice = node.slice
if isinstance(node_slice, nodes.ObjectInjectNode):
node_slice = node.slice.object
lower, upper, step = (
value if value is None else nodes.const(value, size_t)
for value in (node_slice.start, node_slice.stop,
node_slice.step))
else:
lower, upper, step = (node_slice.lower, node_slice.upper,
node_slice.step)
if step is None:
node_value = self.visit(node.value)
if lower is None:
lower = nodes.const(0, size_t)
if upper is None:
ret_val = nodes.LLMacroNode(
macros.c_string_slice_1.__signature__,
macros.c_string_slice_1, self.visit(node.value),
self.visit(lower))
else:
ret_val = nodes.LLMacroNode(
macros.c_string_slice_2.__signature__,
macros.c_string_slice_2, self.visit(node.value),
self.visit(lower), self.visit(upper))
logger.debug(ret_val)
else:
raise NotImplementedError('String slices where step != None.')
return ret_val
示例3: visit_ArrayNewNode
def visit_ArrayNewNode(self, node):
if self.nopython:
# Give the codegen (subclass) a chance to handle this
self.generic_visit(node)
return node
PyArray_Type = nodes.ObjectInjectNode(np.ndarray)
descr = nodes.ObjectInjectNode(node.type.dtype.get_dtype()).cloneable
ndim = nodes.const(node.type.ndim, int_)
flags = nodes.const(0, int_)
args = [PyArray_Type, descr.clone, ndim,
node.shape, node.strides, node.data, flags]
incref_descr = nodes.IncrefNode(descr)
incref_base = None
setbase = None
if node.base is None:
args.append(nodes.NULL_obj)
else:
base = nodes.CloneableNode(node.base)
incref_base = nodes.IncrefNode(base)
args.append(base.clone)
array = nodes.PyArray_NewFromDescr(args).cloneable
body = [incref_descr, incref_base, array, setbase]
if node.base is not None:
body.append(nodes.PyArray_SetBaseObject([array.clone, base.clone]))
# TODO: PyArray_UpdateFlags()
result = nodes.ExpressionNode(filter(None, body), array.clone)
return self.visit(result)
示例4: visit_ArrayNewNode
def visit_ArrayNewNode(self, node):
if self.nopython:
raise error.NumbaError(
node, "Cannot yet allocate new array in nopython context")
PyArray_Type = nodes.ObjectInjectNode(np.ndarray)
descr = nodes.ObjectInjectNode(node.type.dtype.get_dtype()).cloneable
ndim = nodes.const(node.type.ndim, int_)
flags = nodes.const(0, int_)
args = [PyArray_Type, descr.clone, ndim,
node.shape, node.strides, node.data, flags]
incref_descr = nodes.IncrefNode(descr)
incref_base = None
setbase = None
if node.base is None:
args.append(nodes.NULL_obj)
else:
base = nodes.CloneableNode(node.base)
incref_base = nodes.IncrefNode(base)
args.append(base.clone)
array = nodes.PyArray_NewFromDescr(args)
array = nodes.ObjectTempNode(array).cloneable
body = [incref_descr, incref_base, array, setbase]
if node.base is not None:
body.append(nodes.PyArray_SetBaseObject([array.clone, base.clone]))
# TODO: PyArray_UpdateFlags()
result = nodes.ExpressionNode(filter(None, body), array.clone)
return self.visit(result)
示例5: create_numba_function
def create_numba_function(self, node, translator):
closure_scope = self.ast.cur_scope
if closure_scope is None:
closure_scope = nodes.NULL
scope_type = void.pointer()
else:
assert node.func_def.args.args[0].variable.type
scope_type = closure_scope.type
node.wrapper_func, node.wrapper_lfunc, methoddef = (
translator.build_wrapper_function(get_lfunc=True))
# Keep methoddef alive
# assert methoddef in node.py_func.live_objects
modname = self.module_name
self.keep_alive(modname)
# Create function signature with closure scope at runtime
create_numbafunc_signature = node.type(
void.pointer(), # PyMethodDef *ml
object_, # PyObject *module
void.pointer(), # PyObject *code
scope_type, # PyObject *closure
void.pointer(), # void *native_func
object_, # PyObject *native_signature
object_, # PyObject *keep_alive
)
# Create function with closure scope at runtime
create_numbafunc = nodes.ptrfromint(
extension_types.NumbaFunction_NewEx_pointer,
create_numbafunc_signature.pointer())
methoddef_p = ctypes.cast(ctypes.byref(methoddef),
ctypes.c_void_p).value
args = [
nodes.const(methoddef_p, void.pointer()),
nodes.const(modname, object_),
nodes.NULL,
closure_scope,
nodes.const(node.lfunc_pointer, void.pointer()),
nodes.const(node.type.signature, object_),
nodes.NULL, # nodes.const(node.py_func, object_),
]
func_call = nodes.NativeFunctionCallNode(
signature=create_numbafunc_signature,
function_node=create_numbafunc,
args=args)
result = func_call
#stats = [nodes.inject_print(nodes.const("calling...", c_string_type)),
# result]
#result = ast.Suite(body=stats)
result = self.visit(result)
return result
示例6: visit_ArrayNewEmptyNode
def visit_ArrayNewEmptyNode(self, node):
if self.nopython:
raise error.NumbaError(
node, "Cannot yet allocate new empty array in nopython context")
ndim = nodes.const(node.type.ndim, int_)
dtype = nodes.const(node.type.dtype.get_dtype(), object_).cloneable
is_fortran = nodes.const(node.is_fortran, int_)
result = nodes.PyArray_Empty([ndim, node.shape, dtype, is_fortran])
result = nodes.ObjectTempNode(result)
incref_descr = nodes.IncrefNode(dtype)
return self.visit(nodes.ExpressionNode([incref_descr], result))
示例7: unpack_range_args
def unpack_range_args(node):
start, stop, step = (nodes.const(0, Py_ssize_t),
None,
nodes.const(1, Py_ssize_t))
if len(node.args) == 0:
raise error.NumbaError(node, "Expected at least one argument")
elif len(node.args) == 1:
stop, = node.args
elif len(node.args) == 2:
start, stop = node.args
else:
start, stop, step = node.args
return [start, stop, step]
示例8: next
def next(self, context, for_node, llvm_module):
"Index element and update index"
index = self.index.load
value = nodes.CloneableNode(index(for_node.iter, index))
add = ast.BinOp(index, ast.Add(), nodes.const(1, Py_ssize_t))
return nodes.ExpressionNode(stmts=[value, assign(self.index.store, add)], expr=value.clone)
示例9: coerce_to_function_pointer
def coerce_to_function_pointer(self, node, jit_func_type, func_pointer_type):
jit_func = jit_func_type.jit_func
if jit_func.signature != func_pointer_type.base_type:
raise error.NumbaError(node,
"Cannot coerce jit funcion %s to function of type %s" % (
jit_func, func_pointer_type))
pointer = self.env.llvm_context.get_pointer_to_function(jit_func.lfunc)
new_node = nodes.const(pointer, func_pointer_type)
return new_node
示例10: parse_signature
def parse_signature(node, func_type):
types = []
for arg in node.args:
if not arg.variable.type.is_cast:
raise error.NumbaError(arg, "Expected a numba type")
else:
types.append(arg.variable.type)
signature = func_type.dst_type(*types)
new_node = nodes.const(signature, typesystem.CastType(signature))
return new_node
示例11: visit_CoercionNode
def visit_CoercionNode(self, node):
if not isinstance(node, nodes.CoercionNode):
# CoercionNode.__new__ returns the node to be coerced if it doesn't
# need coercion
return node
node_type = node.node.type
dst_type = node.dst_type
if __debug__ and self.env and self.env.debug_coercions:
logger.debug('coercion: %s --> %s\n%s',
node_type, dst_type, utils.pformat_ast(node))
# TODO: the below is a problem due to implicit string <-> int coercions!
if (node_type.is_string and dst_type.is_numeric and not
(node_type.is_pointer or node_type.is_null)):
if dst_type.typename in ('char', 'uchar'):
raise error.NumbaError(
node, "Conversion from string to (u)char not yet supported")
result = self.str_to_int(dst_type, node)
elif self.nopython and (is_obj(node_type) ^ is_obj(dst_type)):
raise error.NumbaError(node, "Cannot coerce to or from object in "
"nopython context")
elif is_obj(node.dst_type) and not is_obj(node_type):
node = nodes.ObjectTempNode(nodes.CoerceToObject(
node.node, node.dst_type, name=node.name))
result = self.visit(node)
elif is_obj(node_type) and not is_obj(node.dst_type):
node = nodes.CoerceToNative(node.node, node.dst_type,
name=node.name)
result = self.visit(node)
elif node_type.is_null:
if not dst_type.is_pointer:
raise error.NumbaError(node.node,
"NULL must be cast or implicitly "
"coerced to a pointer type")
result = self.visit(nodes.NULL.coerce(dst_type))
elif node_type.is_numeric and dst_type.is_bool:
to_bool = ast.Compare(node.node, [ast.NotEq()],
[nodes.const(0, node_type)])
to_bool = nodes.typednode(to_bool, bool_)
result = self.visit(to_bool)
else:
self.generic_visit(node)
if dst_type == node.node.type:
result = node.node
else:
result = node
if __debug__ and self.env and self.env.debug_coercions:
logger.debug('result = %s', utils.pformat_ast(result))
return result
示例12: visit_Print
def visit_Print(self, node):
if self.nopython:
printfunc = self._print_nopython
dst_type = string_
else:
printfunc = self._print
dst_type = object_
result = []
if node.values:
print_space = printfunc(nodes.const(" ", dst_type), node.dest)
for value in node.values:
result.append(printfunc(value, node.dest))
result.append(print_space)
if node.nl:
result.pop() # pop last space
if node.nl:
result.append(printfunc(nodes.const("\n", dst_type), node.dest))
return ast.Suite(body=self.visitlist(result))
示例13: lookup
def lookup(self, env, always_present, node, args):
"""
:param node: ExtensionMethodNode
:param args: [vtable_node, prehash_node]
:return: The virtual method as a Node
"""
from numba.utility import virtuallookup
if always_present and False:
lookup = virtuallookup.lookup_method
else:
lookup = virtuallookup.lookup_and_assert_method
args.append(nodes.const(node.attr, c_string_type))
vmethod = call_jit(lookup, args)
return vmethod
示例14: single_compare_objects
def single_compare_objects(self, node):
op = type(node.ops[0])
if op not in opmap:
raise error.NumbaError(
node, "%s comparisons not yet implemented" % (op,))
# Build arguments for PyObject_RichCompareBool
operator = nodes.const(opmap[op], int_)
args = [node.left, node.comparators[0], operator]
# Call PyObject_RichCompareBool
compare = function_util.external_call(self.context,
self.llvm_module,
'PyObject_RichCompareBool',
args=args)
# Coerce int result to bool
return nodes.CoercionNode(compare, bool_)
示例15: visit_Name
def visit_Name(self, node):
if (is_obj(node.type) and isinstance(node.ctx, ast.Load) and
getattr(node, 'cf_maybe_null', False)):
# Check for unbound objects and raise UnboundLocalError if so
value = nodes.LLVMValueRefNode(Py_uintptr_t, None)
node.loaded_name = value
exc_msg = node.variable.name
if hasattr(node, 'lineno'):
exc_msg = '%s%s' % (error.format_pos(node), exc_msg)
check_unbound = nodes.CheckErrorNode(
value, badval=nodes.const(0, Py_uintptr_t),
exc_type=UnboundLocalError,
exc_msg=exc_msg)
node.check_unbound = self.visit(check_unbound)
return node