Commit 7b59e3b7 authored by Stefan Behnel's avatar Stefan Behnel

Clean up some code in cython.inline() implementation.

parent 4e785d42
from __future__ import absolute_import from __future__ import absolute_import
import sys, os, re, inspect
import imp
import hashlib import hashlib
import inspect
import os
import re
import sys
from distutils.core import Distribution, Extension from distutils.core import Distribution, Extension
from distutils.command.build_ext import build_ext from distutils.command.build_ext import build_ext
import Cython import Cython
from ..Compiler.Main import Context from ..Compiler.Main import Context
from ..Compiler.Options import CompilationOptions, default_options from ..Compiler.Options import default_options
from ..Compiler.ParseTreeTransforms import (CythonTransform, from ..Compiler.ParseTreeTransforms import CythonTransform, SkipDeclarations, EnvTransform
SkipDeclarations, AnalyseDeclarationsTransform, EnvTransform)
from ..Compiler.TreeFragment import parse_from_strings from ..Compiler.TreeFragment import parse_from_strings
from ..Compiler.StringEncoding import _unicode from ..Compiler.StringEncoding import _unicode
from .Dependencies import strip_string_literals, cythonize, cached_function from .Dependencies import strip_string_literals, cythonize, cached_function
from ..Compiler import Pipeline, Nodes from ..Compiler import Pipeline
from ..Utils import get_cython_cache_dir from ..Utils import get_cython_cache_dir
import cython as cython_module import cython as cython_module
IS_PY3 = sys.version_info >= (3, 0)
IS_PY3 = sys.version_info >= (3,)
# A utility function to convert user-supplied ASCII strings to unicode. # A utility function to convert user-supplied ASCII strings to unicode.
if sys.version_info[0] < 3: if not IS_PY3:
def to_unicode(s): def to_unicode(s):
if isinstance(s, bytes): if isinstance(s, bytes):
return s.decode('ascii') return s.decode('ascii')
...@@ -32,7 +34,8 @@ if sys.version_info[0] < 3: ...@@ -32,7 +34,8 @@ if sys.version_info[0] < 3:
else: else:
to_unicode = lambda x: x to_unicode = lambda x: x
if sys.version_info[:2] < (3, 3):
if sys.version_info < (3, 3):
import imp import imp
def load_dynamic(name, module_path): def load_dynamic(name, module_path):
return imp.load_dynamic(name, module_path) return imp.load_dynamic(name, module_path)
...@@ -130,6 +133,7 @@ def _create_context(cython_include_dirs): ...@@ -130,6 +133,7 @@ def _create_context(cython_include_dirs):
_cython_inline_cache = {} _cython_inline_cache = {}
_cython_inline_default_context = _create_context(('.',)) _cython_inline_default_context = _create_context(('.',))
def _populate_unbound(kwds, unbound_symbols, locals=None, globals=None): def _populate_unbound(kwds, unbound_symbols, locals=None, globals=None):
for symbol in unbound_symbols: for symbol in unbound_symbols:
if symbol not in kwds: if symbol not in kwds:
...@@ -146,10 +150,12 @@ def _populate_unbound(kwds, unbound_symbols, locals=None, globals=None): ...@@ -146,10 +150,12 @@ def _populate_unbound(kwds, unbound_symbols, locals=None, globals=None):
else: else:
print("Couldn't find %r" % symbol) print("Couldn't find %r" % symbol)
def _inline_key(orig_code, arg_sigs, language_level): def _inline_key(orig_code, arg_sigs, language_level):
key = orig_code, arg_sigs, sys.version_info, sys.executable, language_level, Cython.__version__ key = orig_code, arg_sigs, sys.version_info, sys.executable, language_level, Cython.__version__
return hashlib.sha1(_unicode(key).encode('utf-8')).hexdigest() return hashlib.sha1(_unicode(key).encode('utf-8')).hexdigest()
def cython_inline(code, get_type=unsafe_type, def cython_inline(code, get_type=unsafe_type,
lib_dir=os.path.join(get_cython_cache_dir(), 'inline'), lib_dir=os.path.join(get_cython_cache_dir(), 'inline'),
cython_include_dirs=None, cython_compiler_directives=None, cython_include_dirs=None, cython_compiler_directives=None,
...@@ -159,7 +165,7 @@ def cython_inline(code, get_type=unsafe_type, ...@@ -159,7 +165,7 @@ def cython_inline(code, get_type=unsafe_type,
get_type = lambda x: 'object' get_type = lambda x: 'object'
ctx = _create_context(tuple(cython_include_dirs)) if cython_include_dirs else _cython_inline_default_context ctx = _create_context(tuple(cython_include_dirs)) if cython_include_dirs else _cython_inline_default_context
cython_compiler_directives = dict(cython_compiler_directives or {}) cython_compiler_directives = dict(cython_compiler_directives) if cython_compiler_directives else {}
if language_level is None and 'language_level' not in cython_compiler_directives: if language_level is None and 'language_level' not in cython_compiler_directives:
language_level = '3str' language_level = '3str'
if language_level is not None: if language_level is not None:
...@@ -284,6 +290,7 @@ def __invoke(%(params)s): ...@@ -284,6 +290,7 @@ def __invoke(%(params)s):
arg_list = [kwds[arg] for arg in arg_names] arg_list = [kwds[arg] for arg in arg_names]
return module.__invoke(*arg_list) return module.__invoke(*arg_list)
# Cached suffix used by cython_inline above. None should get # Cached suffix used by cython_inline above. None should get
# overridden with actual value upon the first cython_inline invocation # overridden with actual value upon the first cython_inline invocation
cython_inline.so_ext = None cython_inline.so_ext = None
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment