support setting language level (2 or 3) for source code - currently configures string...
authorStefan Behnel <scoder@users.berlios.de>
Mon, 31 May 2010 20:30:08 +0000 (22:30 +0200)
committerStefan Behnel <scoder@users.berlios.de>
Mon, 31 May 2010 20:30:08 +0000 (22:30 +0200)
Cython/Compiler/Main.py
Cython/Compiler/Options.py
Cython/Compiler/Parsing.py
runtests.py
tests/run/cython3.pyx [new file with mode: 0644]

index 1847df0b6dd79d2b0d163d9ed824266f3b2faa37..74e33b1361d42ccf49e8df534b3f98eec12a5255 100644 (file)
@@ -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,
 )
index c97e0b25dbfadf5b77c585ee18b4e0a832c86767..db85d423ec64ca2815c0657a6adfa0f72b5562d5 100644 (file)
@@ -66,6 +66,7 @@ directive_defaults = {
     'infer_types': None,
     'infer_types.verbose': False,
     'autotestdict': True,
+    'language_level': 2,
     
     'warn': None,
     'warn.undeclared': False,
index c310a616b61c4b59d943a9ca40d07a402e676b6d..49c774a1bc7d33015dabefbe8cd30dec926e3eaf 100644 (file)
@@ -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'
index 891f0b69aa5ecdecfd676b717c2deeef3053588e..3e1ea1ca2eb4f7967d8f91bb30d776bd3bb330a7 100644 (file)
@@ -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 (file)
index 0000000..37442e9
--- /dev/null
@@ -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