Commit 11532379 authored by Stefan Behnel's avatar Stefan Behnel

support setting language level (2 or 3) for source code - currently configures...

support setting language level (2 or 3) for source code - currently configures string literals and print()
parent d05616c3
...@@ -65,8 +65,9 @@ class Context(object): ...@@ -65,8 +65,9 @@ class Context(object):
# modules {string : ModuleScope} # modules {string : ModuleScope}
# include_directories [string] # include_directories [string]
# future_directives [object] # future_directives [object]
# language_level int currently 2 or 3 for Python 2/3
def __init__(self, include_directories, compiler_directives, cpp=False): def __init__(self, include_directories, compiler_directives, cpp=False, language_level=2):
#self.modules = {"__builtin__" : BuiltinScope()} #self.modules = {"__builtin__" : BuiltinScope()}
import Builtin, CythonScope import Builtin, CythonScope
self.modules = {"__builtin__" : Builtin.builtin_scope} self.modules = {"__builtin__" : Builtin.builtin_scope}
...@@ -82,6 +83,15 @@ class Context(object): ...@@ -82,6 +83,15 @@ class Context(object):
os.path.join(os.path.dirname(__file__), os.path.pardir, 'Includes'))) os.path.join(os.path.dirname(__file__), os.path.pardir, 'Includes')))
self.include_directories = include_directories + [standard_include_path] self.include_directories = include_directories + [standard_include_path]
self.set_language_level(language_level)
def set_language_level(self, level):
self.language_level = level
if level >= 3:
from Future import print_function, unicode_literals
self.future_directives.add(print_function)
self.future_directives.add(unicode_literals)
def create_pipeline(self, pxd, py=False): def create_pipeline(self, pxd, py=False):
from Visitor import PrintTree from Visitor import PrintTree
from ParseTreeTransforms import WithTransform, NormalizeTree, PostParse, PxdPostParse from ParseTreeTransforms import WithTransform, NormalizeTree, PostParse, PxdPostParse
...@@ -541,7 +551,8 @@ def create_default_resultobj(compilation_source, options): ...@@ -541,7 +551,8 @@ def create_default_resultobj(compilation_source, options):
def run_pipeline(source, options, full_module_name = None): def run_pipeline(source, options, full_module_name = None):
# Set up context # Set up context
context = Context(options.include_path, options.compiler_directives, options.cplus) context = Context(options.include_path, options.compiler_directives,
options.cplus, options.language_level)
# Set up source object # Set up source object
cwd = os.getcwd() cwd = os.getcwd()
...@@ -596,6 +607,7 @@ class CompilationOptions(object): ...@@ -596,6 +607,7 @@ class CompilationOptions(object):
quiet boolean Don't print source names in recursive mode quiet boolean Don't print source names in recursive mode
compiler_directives dict Overrides for pragma options (see Options.py) compiler_directives dict Overrides for pragma options (see Options.py)
evaluate_tree_assertions boolean Test support: evaluate parse tree assertions evaluate_tree_assertions boolean Test support: evaluate parse tree assertions
language_level integer The Python language level: 2 or 3
cplus boolean Compile as c++ code cplus boolean Compile as c++ code
""" """
...@@ -774,4 +786,5 @@ default_options = dict( ...@@ -774,4 +786,5 @@ default_options = dict(
compiler_directives = {}, compiler_directives = {},
evaluate_tree_assertions = False, evaluate_tree_assertions = False,
emit_linenums = False, emit_linenums = False,
language_level = 2,
) )
...@@ -66,6 +66,7 @@ directive_defaults = { ...@@ -66,6 +66,7 @@ directive_defaults = {
'infer_types': None, 'infer_types': None,
'infer_types.verbose': False, 'infer_types.verbose': False,
'autotestdict': True, 'autotestdict': True,
'language_level': 2,
'warn': None, 'warn': None,
'warn.undeclared': False, 'warn.undeclared': False,
......
...@@ -2589,6 +2589,9 @@ def p_module(s, pxd, full_module_name): ...@@ -2589,6 +2589,9 @@ def p_module(s, pxd, full_module_name):
directive_comments = p_compiler_directive_comments(s) directive_comments = p_compiler_directive_comments(s)
s.parse_comments = False s.parse_comments = False
if 'language_level' in directive_comments:
s.context.set_language_level('language_level')
doc = p_doc_string(s) doc = p_doc_string(s)
if pxd: if pxd:
level = 'module_pxd' level = 'module_pxd'
......
...@@ -53,7 +53,8 @@ VER_DEP_MODULES = { ...@@ -53,7 +53,8 @@ VER_DEP_MODULES = {
# (2,4) : (operator.le, ...) excludes ... when PyVer <= 2.4.x # (2,4) : (operator.le, ...) excludes ... when PyVer <= 2.4.x
(2,4) : (operator.le, lambda x: x in ['run.extern_builtins_T258' (2,4) : (operator.le, lambda x: x in ['run.extern_builtins_T258'
]), ]),
(2,6) : (operator.lt, lambda x: x in ['run.print_function' (2,6) : (operator.lt, lambda x: x in ['run.print_function',
'run.cython3',
]), ]),
(3,): (operator.ge, lambda x: x in ['run.non_future_division', (3,): (operator.ge, lambda x: x in ['run.non_future_division',
'compile.extsetslice', 'compile.extsetslice',
......
# cython: language_level=3
def print_function(*args):
"""
>>> print_function(1,2,3)
1 2 3
"""
print(*args) # this isn't valid Py2 syntax
ustring = "abcdefg"
def unicode_literals():
"""
>>> print( unicode_literals() )
True
abcdefg
"""
print(isinstance(ustring, unicode) or type(ustring))
return ustring
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