From 11532379bac209881d905d9f7c86950c83c1d50c Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Mon, 31 May 2010 22:30:08 +0200 Subject: [PATCH] support setting language level (2 or 3) for source code - currently configures string literals and print() --- Cython/Compiler/Main.py | 17 +++++++++++++++-- Cython/Compiler/Options.py | 1 + Cython/Compiler/Parsing.py | 3 +++ runtests.py | 3 ++- tests/run/cython3.pyx | 19 +++++++++++++++++++ 5 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 tests/run/cython3.pyx diff --git a/Cython/Compiler/Main.py b/Cython/Compiler/Main.py index 1847df0b..74e33b13 100644 --- a/Cython/Compiler/Main.py +++ b/Cython/Compiler/Main.py @@ -65,8 +65,9 @@ class Context(object): # modules {string : ModuleScope} # include_directories [string] # 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()} import Builtin, CythonScope self.modules = {"__builtin__" : Builtin.builtin_scope} @@ -82,6 +83,15 @@ class Context(object): os.path.join(os.path.dirname(__file__), os.path.pardir, 'Includes'))) 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): from Visitor import PrintTree from ParseTreeTransforms import WithTransform, NormalizeTree, PostParse, PxdPostParse @@ -541,7 +551,8 @@ def create_default_resultobj(compilation_source, options): def run_pipeline(source, options, full_module_name = None): # 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 cwd = os.getcwd() @@ -596,6 +607,7 @@ class CompilationOptions(object): quiet boolean Don't print source names in recursive mode compiler_directives dict Overrides for pragma options (see Options.py) 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 """ @@ -774,4 +786,5 @@ default_options = dict( compiler_directives = {}, evaluate_tree_assertions = False, emit_linenums = False, + language_level = 2, ) diff --git a/Cython/Compiler/Options.py b/Cython/Compiler/Options.py index c97e0b25..db85d423 100644 --- a/Cython/Compiler/Options.py +++ b/Cython/Compiler/Options.py @@ -66,6 +66,7 @@ directive_defaults = { 'infer_types': None, 'infer_types.verbose': False, 'autotestdict': True, + 'language_level': 2, 'warn': None, 'warn.undeclared': False, diff --git a/Cython/Compiler/Parsing.py b/Cython/Compiler/Parsing.py index c310a616..49c774a1 100644 --- a/Cython/Compiler/Parsing.py +++ b/Cython/Compiler/Parsing.py @@ -2589,6 +2589,9 @@ def p_module(s, pxd, full_module_name): directive_comments = p_compiler_directive_comments(s) s.parse_comments = False + if 'language_level' in directive_comments: + s.context.set_language_level('language_level') + doc = p_doc_string(s) if pxd: level = 'module_pxd' diff --git a/runtests.py b/runtests.py index 891f0b69..3e1ea1ca 100644 --- a/runtests.py +++ b/runtests.py @@ -53,7 +53,8 @@ VER_DEP_MODULES = { # (2,4) : (operator.le, ...) excludes ... when PyVer <= 2.4.x (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', 'compile.extsetslice', diff --git a/tests/run/cython3.pyx b/tests/run/cython3.pyx new file mode 100644 index 00000000..37442e92 --- /dev/null +++ b/tests/run/cython3.pyx @@ -0,0 +1,19 @@ +# 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 -- 2.26.2