Commit 5407899f authored by Stefan Behnel's avatar Stefan Behnel

Support a direct "language_level" argument in "cython.inline()", and fix a test by using it.

parent c8e6f375
...@@ -26,6 +26,9 @@ Features added ...@@ -26,6 +26,9 @@ Features added
* ``@cython.nogil`` is supported as a C-function decorator in Python code. * ``@cython.nogil`` is supported as a C-function decorator in Python code.
(Github issue #2557) (Github issue #2557)
* ``cython.inline()`` supports a direct ``language_level`` keyword argument that
was previously only available via a directive.
* In CPython 3.6 and later, looking up globals in the module dict is almost * In CPython 3.6 and later, looking up globals in the module dict is almost
as fast as looking up C globals. as fast as looking up C globals.
(Github issue #2313) (Github issue #2313)
......
...@@ -139,7 +139,7 @@ def _populate_unbound(kwds, unbound_symbols, locals=None, globals=None): ...@@ -139,7 +139,7 @@ def _populate_unbound(kwds, unbound_symbols, locals=None, globals=None):
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,
force=False, quiet=False, locals=None, globals=None, **kwds): force=False, quiet=False, locals=None, globals=None, language_level=None, **kwds):
if get_type is None: if get_type is None:
get_type = lambda x: 'object' get_type = lambda x: 'object'
...@@ -171,6 +171,11 @@ def cython_inline(code, get_type=unsafe_type, ...@@ -171,6 +171,11 @@ def cython_inline(code, get_type=unsafe_type,
if not quiet: if not quiet:
# Parsing from strings not fully supported (e.g. cimports). # Parsing from strings not fully supported (e.g. cimports).
print("Could not parse code as a string (to extract unbound symbols).") print("Could not parse code as a string (to extract unbound symbols).")
cython_compiler_directives = dict(cython_compiler_directives or {})
if language_level is not None:
cython_compiler_directives['language_level'] = language_level
cimports = [] cimports = []
for name, arg in list(kwds.items()): for name, arg in list(kwds.items()):
if arg is cython_module: if arg is cython_module:
...@@ -178,7 +183,7 @@ def cython_inline(code, get_type=unsafe_type, ...@@ -178,7 +183,7 @@ def cython_inline(code, get_type=unsafe_type,
del kwds[name] del kwds[name]
arg_names = sorted(kwds) arg_names = sorted(kwds)
arg_sigs = tuple([(get_type(kwds[arg], ctx), arg) for arg in arg_names]) arg_sigs = tuple([(get_type(kwds[arg], ctx), arg) for arg in arg_names])
key = orig_code, arg_sigs, sys.version_info, sys.executable, Cython.__version__ key = orig_code, arg_sigs, sys.version_info, sys.executable, language_level, Cython.__version__
module_name = "_cython_inline_" + hashlib.md5(_unicode(key).encode('utf-8')).hexdigest() module_name = "_cython_inline_" + hashlib.md5(_unicode(key).encode('utf-8')).hexdigest()
if module_name in sys.modules: if module_name in sys.modules:
...@@ -238,7 +243,7 @@ def __invoke(%(params)s): ...@@ -238,7 +243,7 @@ def __invoke(%(params)s):
build_extension.extensions = cythonize( build_extension.extensions = cythonize(
[extension], [extension],
include_path=cython_include_dirs or ['.'], include_path=cython_include_dirs or ['.'],
compiler_directives=cython_compiler_directives or {}, compiler_directives=cython_compiler_directives,
quiet=quiet) quiet=quiet)
build_extension.build_temp = os.path.dirname(pyx_file) build_extension.build_temp = os.path.dirname(pyx_file)
build_extension.build_lib = lib_dir build_extension.build_lib = lib_dir
......
...@@ -76,7 +76,7 @@ def exec(code_string, l, g): ...@@ -76,7 +76,7 @@ def exec(code_string, l, g):
old_stderr = sys.stderr old_stderr = sys.stderr
try: try:
sys.stderr = StringIO() sys.stderr = StringIO()
ns = inline(code_string, locals=l, globals=g, lib_dir=os.path.dirname(__file__)) ns = inline(code_string, locals=l, globals=g, lib_dir=os.path.dirname(__file__), language_level=3)
finally: finally:
sys.stderr = old_stderr sys.stderr = old_stderr
g.update(ns) g.update(ns)
......
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