Inline.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. from __future__ import absolute_import
  2. import hashlib
  3. import inspect
  4. import os
  5. import re
  6. import sys
  7. from distutils.core import Distribution, Extension
  8. from distutils.command.build_ext import build_ext
  9. import Cython
  10. from ..Compiler.Main import Context, default_options
  11. from ..Compiler.Visitor import CythonTransform, EnvTransform
  12. from ..Compiler.ParseTreeTransforms import SkipDeclarations
  13. from ..Compiler.TreeFragment import parse_from_strings
  14. from ..Compiler.StringEncoding import _unicode
  15. from .Dependencies import strip_string_literals, cythonize, cached_function
  16. from ..Compiler import Pipeline
  17. from ..Utils import get_cython_cache_dir
  18. import cython as cython_module
  19. IS_PY3 = sys.version_info >= (3,)
  20. # A utility function to convert user-supplied ASCII strings to unicode.
  21. if not IS_PY3:
  22. def to_unicode(s):
  23. if isinstance(s, bytes):
  24. return s.decode('ascii')
  25. else:
  26. return s
  27. else:
  28. to_unicode = lambda x: x
  29. if sys.version_info < (3, 5):
  30. import imp
  31. def load_dynamic(name, module_path):
  32. return imp.load_dynamic(name, module_path)
  33. else:
  34. import importlib.util
  35. from importlib.machinery import ExtensionFileLoader
  36. def load_dynamic(name, path):
  37. spec = importlib.util.spec_from_file_location(name, loader=ExtensionFileLoader(name, path))
  38. module = importlib.util.module_from_spec(spec)
  39. spec.loader.exec_module(module)
  40. return module
  41. class UnboundSymbols(EnvTransform, SkipDeclarations):
  42. def __init__(self):
  43. CythonTransform.__init__(self, None)
  44. self.unbound = set()
  45. def visit_NameNode(self, node):
  46. if not self.current_env().lookup(node.name):
  47. self.unbound.add(node.name)
  48. return node
  49. def __call__(self, node):
  50. super(UnboundSymbols, self).__call__(node)
  51. return self.unbound
  52. @cached_function
  53. def unbound_symbols(code, context=None):
  54. code = to_unicode(code)
  55. if context is None:
  56. context = Context([], default_options)
  57. from ..Compiler.ParseTreeTransforms import AnalyseDeclarationsTransform
  58. tree = parse_from_strings('(tree fragment)', code)
  59. for phase in Pipeline.create_pipeline(context, 'pyx'):
  60. if phase is None:
  61. continue
  62. tree = phase(tree)
  63. if isinstance(phase, AnalyseDeclarationsTransform):
  64. break
  65. try:
  66. import builtins
  67. except ImportError:
  68. import __builtin__ as builtins
  69. return tuple(UnboundSymbols()(tree) - set(dir(builtins)))
  70. def unsafe_type(arg, context=None):
  71. py_type = type(arg)
  72. if py_type is int:
  73. return 'long'
  74. else:
  75. return safe_type(arg, context)
  76. def safe_type(arg, context=None):
  77. py_type = type(arg)
  78. if py_type in (list, tuple, dict, str):
  79. return py_type.__name__
  80. elif py_type is complex:
  81. return 'double complex'
  82. elif py_type is float:
  83. return 'double'
  84. elif py_type is bool:
  85. return 'bint'
  86. elif 'numpy' in sys.modules and isinstance(arg, sys.modules['numpy'].ndarray):
  87. return 'numpy.ndarray[numpy.%s_t, ndim=%s]' % (arg.dtype.name, arg.ndim)
  88. else:
  89. for base_type in py_type.__mro__:
  90. if base_type.__module__ in ('__builtin__', 'builtins'):
  91. return 'object'
  92. module = context.find_module(base_type.__module__, need_pxd=False)
  93. if module:
  94. entry = module.lookup(base_type.__name__)
  95. if entry.is_type:
  96. return '%s.%s' % (base_type.__module__, base_type.__name__)
  97. return 'object'
  98. def _get_build_extension():
  99. dist = Distribution()
  100. # Ensure the build respects distutils configuration by parsing
  101. # the configuration files
  102. config_files = dist.find_config_files()
  103. dist.parse_config_files(config_files)
  104. build_extension = build_ext(dist)
  105. build_extension.finalize_options()
  106. return build_extension
  107. @cached_function
  108. def _create_context(cython_include_dirs):
  109. return Context(list(cython_include_dirs), default_options)
  110. _cython_inline_cache = {}
  111. _cython_inline_default_context = _create_context(('.',))
  112. def _populate_unbound(kwds, unbound_symbols, locals=None, globals=None):
  113. for symbol in unbound_symbols:
  114. if symbol not in kwds:
  115. if locals is None or globals is None:
  116. calling_frame = inspect.currentframe().f_back.f_back.f_back
  117. if locals is None:
  118. locals = calling_frame.f_locals
  119. if globals is None:
  120. globals = calling_frame.f_globals
  121. if symbol in locals:
  122. kwds[symbol] = locals[symbol]
  123. elif symbol in globals:
  124. kwds[symbol] = globals[symbol]
  125. else:
  126. print("Couldn't find %r" % symbol)
  127. def _inline_key(orig_code, arg_sigs, language_level):
  128. key = orig_code, arg_sigs, sys.version_info, sys.executable, language_level, Cython.__version__
  129. return hashlib.sha1(_unicode(key).encode('utf-8')).hexdigest()
  130. def cython_inline(code, get_type=unsafe_type,
  131. lib_dir=os.path.join(get_cython_cache_dir(), 'inline'),
  132. cython_include_dirs=None, cython_compiler_directives=None,
  133. force=False, quiet=False, locals=None, globals=None, language_level=None, **kwds):
  134. if get_type is None:
  135. get_type = lambda x: 'object'
  136. ctx = _create_context(tuple(cython_include_dirs)) if cython_include_dirs else _cython_inline_default_context
  137. cython_compiler_directives = dict(cython_compiler_directives) if cython_compiler_directives else {}
  138. if language_level is None and 'language_level' not in cython_compiler_directives:
  139. language_level = '3str'
  140. if language_level is not None:
  141. cython_compiler_directives['language_level'] = language_level
  142. # Fast path if this has been called in this session.
  143. _unbound_symbols = _cython_inline_cache.get(code)
  144. if _unbound_symbols is not None:
  145. _populate_unbound(kwds, _unbound_symbols, locals, globals)
  146. args = sorted(kwds.items())
  147. arg_sigs = tuple([(get_type(value, ctx), arg) for arg, value in args])
  148. key_hash = _inline_key(code, arg_sigs, language_level)
  149. invoke = _cython_inline_cache.get((code, arg_sigs, key_hash))
  150. if invoke is not None:
  151. arg_list = [arg[1] for arg in args]
  152. return invoke(*arg_list)
  153. orig_code = code
  154. code = to_unicode(code)
  155. code, literals = strip_string_literals(code)
  156. code = strip_common_indent(code)
  157. if locals is None:
  158. locals = inspect.currentframe().f_back.f_back.f_locals
  159. if globals is None:
  160. globals = inspect.currentframe().f_back.f_back.f_globals
  161. try:
  162. _cython_inline_cache[orig_code] = _unbound_symbols = unbound_symbols(code)
  163. _populate_unbound(kwds, _unbound_symbols, locals, globals)
  164. except AssertionError:
  165. if not quiet:
  166. # Parsing from strings not fully supported (e.g. cimports).
  167. print("Could not parse code as a string (to extract unbound symbols).")
  168. cimports = []
  169. for name, arg in list(kwds.items()):
  170. if arg is cython_module:
  171. cimports.append('\ncimport cython as %s' % name)
  172. del kwds[name]
  173. arg_names = sorted(kwds)
  174. arg_sigs = tuple([(get_type(kwds[arg], ctx), arg) for arg in arg_names])
  175. key_hash = _inline_key(orig_code, arg_sigs, language_level)
  176. module_name = "_cython_inline_" + key_hash
  177. if module_name in sys.modules:
  178. module = sys.modules[module_name]
  179. else:
  180. build_extension = None
  181. if cython_inline.so_ext is None:
  182. # Figure out and cache current extension suffix
  183. build_extension = _get_build_extension()
  184. cython_inline.so_ext = build_extension.get_ext_filename('')
  185. module_path = os.path.join(lib_dir, module_name + cython_inline.so_ext)
  186. if not os.path.exists(lib_dir):
  187. os.makedirs(lib_dir)
  188. if force or not os.path.isfile(module_path):
  189. cflags = []
  190. c_include_dirs = []
  191. qualified = re.compile(r'([.\w]+)[.]')
  192. for type, _ in arg_sigs:
  193. m = qualified.match(type)
  194. if m:
  195. cimports.append('\ncimport %s' % m.groups()[0])
  196. # one special case
  197. if m.groups()[0] == 'numpy':
  198. import numpy
  199. c_include_dirs.append(numpy.get_include())
  200. # cflags.append('-Wno-unused')
  201. module_body, func_body = extract_func_code(code)
  202. params = ', '.join(['%s %s' % a for a in arg_sigs])
  203. module_code = """
  204. %(module_body)s
  205. %(cimports)s
  206. def __invoke(%(params)s):
  207. %(func_body)s
  208. return locals()
  209. """ % {'cimports': '\n'.join(cimports),
  210. 'module_body': module_body,
  211. 'params': params,
  212. 'func_body': func_body }
  213. for key, value in literals.items():
  214. module_code = module_code.replace(key, value)
  215. pyx_file = os.path.join(lib_dir, module_name + '.pyx')
  216. fh = open(pyx_file, 'w')
  217. try:
  218. fh.write(module_code)
  219. finally:
  220. fh.close()
  221. extension = Extension(
  222. name = module_name,
  223. sources = [pyx_file],
  224. include_dirs = c_include_dirs,
  225. extra_compile_args = cflags)
  226. if build_extension is None:
  227. build_extension = _get_build_extension()
  228. build_extension.extensions = cythonize(
  229. [extension],
  230. include_path=cython_include_dirs or ['.'],
  231. compiler_directives=cython_compiler_directives,
  232. quiet=quiet)
  233. build_extension.build_temp = os.path.dirname(pyx_file)
  234. build_extension.build_lib = lib_dir
  235. build_extension.run()
  236. module = load_dynamic(module_name, module_path)
  237. _cython_inline_cache[orig_code, arg_sigs, key_hash] = module.__invoke
  238. arg_list = [kwds[arg] for arg in arg_names]
  239. return module.__invoke(*arg_list)
  240. # Cached suffix used by cython_inline above. None should get
  241. # overridden with actual value upon the first cython_inline invocation
  242. cython_inline.so_ext = None
  243. _find_non_space = re.compile('[^ ]').search
  244. def strip_common_indent(code):
  245. min_indent = None
  246. lines = code.splitlines()
  247. for line in lines:
  248. match = _find_non_space(line)
  249. if not match:
  250. continue # blank
  251. indent = match.start()
  252. if line[indent] == '#':
  253. continue # comment
  254. if min_indent is None or min_indent > indent:
  255. min_indent = indent
  256. for ix, line in enumerate(lines):
  257. match = _find_non_space(line)
  258. if not match or not line or line[indent:indent+1] == '#':
  259. continue
  260. lines[ix] = line[min_indent:]
  261. return '\n'.join(lines)
  262. module_statement = re.compile(r'^((cdef +(extern|class))|cimport|(from .+ cimport)|(from .+ import +[*]))')
  263. def extract_func_code(code):
  264. module = []
  265. function = []
  266. current = function
  267. code = code.replace('\t', ' ')
  268. lines = code.split('\n')
  269. for line in lines:
  270. if not line.startswith(' '):
  271. if module_statement.match(line):
  272. current = module
  273. else:
  274. current = function
  275. current.append(line)
  276. return '\n'.join(module), ' ' + '\n '.join(function)
  277. try:
  278. from inspect import getcallargs
  279. except ImportError:
  280. def getcallargs(func, *arg_values, **kwd_values):
  281. all = {}
  282. args, varargs, kwds, defaults = inspect.getargspec(func)
  283. if varargs is not None:
  284. all[varargs] = arg_values[len(args):]
  285. for name, value in zip(args, arg_values):
  286. all[name] = value
  287. for name, value in list(kwd_values.items()):
  288. if name in args:
  289. if name in all:
  290. raise TypeError("Duplicate argument %s" % name)
  291. all[name] = kwd_values.pop(name)
  292. if kwds is not None:
  293. all[kwds] = kwd_values
  294. elif kwd_values:
  295. raise TypeError("Unexpected keyword arguments: %s" % list(kwd_values))
  296. if defaults is None:
  297. defaults = ()
  298. first_default = len(args) - len(defaults)
  299. for ix, name in enumerate(args):
  300. if name not in all:
  301. if ix >= first_default:
  302. all[name] = defaults[ix - first_default]
  303. else:
  304. raise TypeError("Missing argument: %s" % name)
  305. return all
  306. def get_body(source):
  307. ix = source.index(':')
  308. if source[:5] == 'lambda':
  309. return "return %s" % source[ix+1:]
  310. else:
  311. return source[ix+1:]
  312. # Lots to be done here... It would be especially cool if compiled functions
  313. # could invoke each other quickly.
  314. class RuntimeCompiledFunction(object):
  315. def __init__(self, f):
  316. self._f = f
  317. self._body = get_body(inspect.getsource(f))
  318. def __call__(self, *args, **kwds):
  319. all = getcallargs(self._f, *args, **kwds)
  320. if IS_PY3:
  321. return cython_inline(self._body, locals=self._f.__globals__, globals=self._f.__globals__, **all)
  322. else:
  323. return cython_inline(self._body, locals=self._f.func_globals, globals=self._f.func_globals, **all)