/usr/lib/python3/dist-packages/instant/inlining.py is in python3-instant 2017.2.0.0-2.
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | """This module contains the inline* functions, which allows easy inlining of C/C++ functions."""
# Copyright (C) 2008-2010 Kent-Andre Mardal
# Copyright (C) 2008-2010 Martin Sandve Alnes
#
# This file is part of Instant.
#
# Instant is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Instant is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Instant. If not, see <http://www.gnu.org/licenses/>.
#
# Alternatively, Instant may be distributed under the terms of the BSD license.
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
|