/usr/lib/python2.7/dist-packages/instant/inlining.py is in python-instant 1.3.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | """This module contains the inline* functions, which allows easy inlining of C/C++ functions."""
from .output import instant_assert, instant_warning, instant_error
from .build import build_module, build_module_vtk, build_module_vmtk
def get_func_name(c_code):
# TODO: Something more robust? Regexp?
try:
func = c_code[:c_code.index('(')]
ret, func_name = func.split()
except:
instant_error("Failed to extract function name from c_code.")
return func_name
def inline(c_code, **kwargs):
"""This is a short wrapper around the build_module function in instant.
It creates a module given that
the input is a valid C function. It is only possible
to inline one C function each time.
Usage:
>>> from instant import inline
>>> add_func = inline("double add(double a, double b){ return a+b; }")
>>> print "The sum of 3 and 4.5 is ", add_func(3, 4.5)
"""
instant_assert("code" not in kwargs, "Cannot specify code twice.")
kwargs["code"] = c_code
func_name = get_func_name(c_code)
module = build_module(**kwargs)
if hasattr(module, func_name):
return getattr(module, func_name)
else:
instant_warning("Didn't find function '%s', returning module." % func_name)
return module
def inline_module(c_code, **kwargs):
"""This is a short wrapper around the build_module function in instant.
It creates a module given that
the input is a valid C function. It is only possible
to inline one C function each time.
Usage:
>>> from instant import inline
>>> add_func = inline("double add(double a, double b){ return a+b; }")
>>> print "The sum of 3 and 4.5 is ", add_func(3, 4.5)
"""
instant_assert("code" not in kwargs, "Cannot specify code twice.")
kwargs["code"] = c_code
module = build_module(**kwargs)
return module
def inline_with_numpy(c_code, **kwargs):
'''This is a short wrapper around the build_module function in instant.
It creates a module given that
the input is a valid C function. It is only possible
to inline one C function each time. The difference between
this function and the inline function is that C-arrays can be used.
The following example illustrates that.
Usage:
>>> import numpy
>>> import time
>>> from instant import inline_with_numpy
>>> c_code = """
double sum (int n1, double* array1){
double tmp = 0.0;
for (int i=0; i<n1; i++) {
tmp += array1[i];
}
return tmp;
}
"""
>>> sum_func = inline_with_numpy(c_code, arrays = [['n1', 'array1']])
>>> a = numpy.arange(10000000); a = numpy.sin(a)
>>> sum_func(a)
'''
import numpy
instant_assert("code" not in kwargs, "Cannot specify code twice.")
kwargs["code"] = c_code
kwargs["init_code"] = kwargs.get("init_code","") + "\nimport_array();\n"
kwargs["system_headers"] = kwargs.get("system_headers",[]) + ["numpy/arrayobject.h"]
kwargs["include_dirs"] = kwargs.get("include_dirs",[]) + ["%s" %numpy.get_include()]
func_name = get_func_name(c_code)
module = build_module(**kwargs)
if hasattr(module, func_name):
return getattr(module, func_name)
else:
instant_warning("Didn't find function '%s', returning module." % func_name)
return module
def inline_module_with_numpy(c_code, **kwargs):
'''This is a short wrapper around the build_module function in instant.
It creates a module given that
the input is a valid C function. It is only possible
to inline one C function each time. The difference between
this function and the inline function is that C-arrays can be used.
The following example illustrates that.
Usage:
>>> import numpy
>>> import time
>>> from instant import inline_with_numpy
>>> c_code = """
double sum (int n1, double* array1){
double tmp = 0.0;
for (int i=0; i<n1; i++) {
tmp += array1[i];
}
return tmp;
}
"""
>>> sum_func = inline_with_numpy(c_code, arrays = [['n1', 'array1']])
>>> a = numpy.arange(10000000); a = numpy.sin(a)
>>> sum_func(a)
'''
import numpy
instant_assert("code" not in kwargs, "Cannot specify code twice.")
kwargs["code"] = c_code
kwargs["init_code"] = kwargs.get("init_code","") + "\nimport_array();\n"
kwargs["system_headers"] = kwargs.get("system_headers",[]) + ["numpy/arrayobject.h"]
kwargs["include_dirs"] = kwargs.get("include_dirs",[]) + ["%s" % numpy.get_include()]
module = build_module(**kwargs)
return module
def inline_vtk(c_code, cache_dir=None):
module = build_module_vtk(c_code)
func_name = get_func_name(c_code)
if hasattr(module, func_name):
return getattr(module, func_name)
else:
instant_warning("Didn't find function '%s', returning module." % func_name)
return module
def inline_vmtk(c_code, cache_dir=None):
module = build_module_vmtk(c_code)
func_name = get_func_name(c_code)
if hasattr(module, func_name):
return getattr(module, func_name)
else:
instant_warning("Didn't find function '%s', returning module." % func_name)
return module
|