本文整理汇总了Python中numba.nodes.const函数的典型用法代码示例。如果您正苦于以下问题:Python const函数的具体用法?Python const怎么用?Python const使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了const函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
开发者ID:FrancescAlted,项目名称:numba,代码行数:30,代码来源:transforms.py
示例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
开发者ID:FrancescAlted,项目名称:numba,代码行数:31,代码来源:transforms.py
示例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)
开发者ID:lizecillie,项目名称:numba,代码行数:33,代码来源:transforms.py
示例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)
开发者ID:FrancescAlted,项目名称:numba,代码行数:33,代码来源:transforms.py
示例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
开发者ID:lizecillie,项目名称:numba,代码行数:59,代码来源:closures.py
示例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))
开发者ID:FrancescAlted,项目名称:numba,代码行数:12,代码来源:transforms.py
示例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]
开发者ID:ejmvar,项目名称:numba,代码行数:15,代码来源:loops.py
示例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)
开发者ID:meteogrid,项目名称:numba,代码行数:7,代码来源:loopimpl.py
示例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
开发者ID:FrancescAlted,项目名称:numba,代码行数:9,代码来源:transforms.py
示例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
开发者ID:hgrecco,项目名称:numba,代码行数:11,代码来源:infer_call.py
示例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
开发者ID:FrancescAlted,项目名称:numba,代码行数:53,代码来源:transforms.py
示例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))
开发者ID:FrancescAlted,项目名称:numba,代码行数:23,代码来源:transforms.py
示例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
开发者ID:shiquanwang,项目名称:numba,代码行数:16,代码来源:exttypes.py
示例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_)
开发者ID:KanzhiWu,项目名称:numba,代码行数:18,代码来源:comparisons.py
示例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
开发者ID:hgrecco,项目名称:numba,代码行数:18,代码来源:exceptions.py
示例16: _print_nopython
def _print_nopython(self, value, dest=None):
if dest is not None:
raise error.NumbaError(dest, "No file may be given in nopython mode")
# stdin, stdout, stderr = stdio_util.get_stdio_streams()
# stdout = stdio_util.get_stream_as_node(stdout)
format = codegen.get_printf_specifier(value.type)
if format is None:
raise error.NumbaError(
value, "Printing values of type '%s' is not supported "
"in nopython mode" % (value.type,))
return function_util.external_call(
self.context,
self.llvm_module,
'printf',
args=[nodes.const(format, c_string_type),
value])
开发者ID:FrancescAlted,项目名称:numba,代码行数:19,代码来源:transforms.py
示例17: get_closure_scope
def get_closure_scope(func_signature, func_obj):
"""
Retrieve the closure from the NumbaFunction from the func_closure
attribute.
func_signature:
signature of closure function
func_obj:
LLVM Value referencing the closure function as a Python object
"""
closure_scope_type = func_signature.args[0]
offset = numbawrapper.numbafunc_closure_field_offset
closure = nodes.LLVMValueRefNode(void.pointer(), func_obj)
closure = nodes.CoercionNode(closure, char.pointer())
closure_field = nodes.pointer_add(closure, nodes.const(offset, size_t))
closure_field = nodes.CoercionNode(closure_field, closure_scope_type.pointer())
closure_scope = nodes.DereferenceNode(closure_field)
return closure_scope
开发者ID:meteogrid,项目名称:numba,代码行数:19,代码来源:llvmwrapper.py
示例18: _trap
def _trap(self, body, node):
if node.exc_msg and node.print_on_trap:
pos = error.format_pos(node)
if node.exception_type:
exc_type = '%s: ' % node.exception_type.__name__
else:
exc_type = ''
msg = '%s%s%%s' % (exc_type, pos)
format = nodes.const(msg, c_string_type)
print_msg = function_util.external_call(self.context,
self.llvm_module,
'printf',
args=[format,
node.exc_msg])
body.append(print_msg)
trap = nodes.LLVMIntrinsicNode(signature=void(), args=[],
func_name='TRAP')
body.append(trap)
开发者ID:hgrecco,项目名称:numba,代码行数:20,代码来源:exceptions.py
示例19: typedcontainer_infer
def typedcontainer_infer(compile_typedcontainer, type_node, iterable_node):
"""
Type inferer for typed containers, register with numba.register_inferer().
:param compile_typedcontainer: item_type -> typed container extension class
:param type_node: type parameter to typed container constructor
:param iterable_node: value parameter to typed container constructor (optional)
"""
assert type_node is not None
type = get_type(type_node)
if type.is_cast:
elem_type = type.dst_type
# Pre-compile typed list implementation
typedcontainer_ctor = compile_typedcontainer(elem_type)
# Inject the typedlist directly to avoid runtime implementation lookup
iterable_node = iterable_node or nodes.const(None, object_)
result = nodes.call_pyfunc(typedcontainer_ctor, (iterable_node,))
return nodes.CoercionNode(result, typedcontainer_ctor.exttype)
return object_
开发者ID:lizecillie,项目名称:numba,代码行数:23,代码来源:orderedcontainer.py
示例20: visit_For
def visit_For(self, node):
while_node = make_while_from_for(node)
test = nodes.const(True, bool_)
while_node.test = test
impl = loopimpl.find_iterator_impl(node)
# Get the iterator, loop body, and the item
iter = impl.getiter(self.context, node, self.llvm_module)
body = impl.body(self.context, node, self.llvm_module)
item = impl.next(self.context, node, self.llvm_module)
# Coerce item to LHS and assign
item = nodes.CoercionNode(item, node.target.type)
target_assmnt = ast.Assign(targets=[node.target], value=item)
# Update While node body
body.insert(0, target_assmnt)
while_node.body = body
nodes.merge_cfg_in_while(while_node)
return ast.Suite(body=[iter, while_node])
开发者ID:ejmvar,项目名称:numba,代码行数:24,代码来源:loops.py
注:本文中的numba.nodes.const函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论