support 'from __future__ import print_function' in Py2.6+
authorStefan Behnel <scoder@users.berlios.de>
Wed, 10 Mar 2010 07:46:58 +0000 (08:46 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Wed, 10 Mar 2010 07:46:58 +0000 (08:46 +0100)
--HG--
rename : tests/run/print.pyx => tests/run/print_function.pyx

Cython/Compiler/Future.py
Cython/Compiler/Scanning.py
runtests.py
tests/run/print_function.pyx [new file with mode: 0644]

index c42e595f074c3e7d6dc60056cbc519fd42dd77cf..ef2c61f6f74dfaa197ecd8d9ce764cec69e2efcd 100644 (file)
@@ -9,5 +9,6 @@ def _get_feature(name):
 unicode_literals = _get_feature("unicode_literals")
 with_statement = _get_feature("with_statement")
 division = _get_feature("division")
+print_function = _get_feature("print_function")
 
 del _get_feature
index 3dd7c9167372581070a3df8931835ee8ded9d731..562ed962e6cea9b456f5971e389612888f2a6a2a 100644 (file)
@@ -10,13 +10,15 @@ import codecs
 from time import time
 
 import cython
-cython.declare(EncodedString=object, string_prefixes=object, raw_prefixes=object, IDENT=object)
+cython.declare(EncodedString=object, string_prefixes=object, raw_prefixes=object, IDENT=object,
+               print_function=object)
 
 from Cython import Plex, Utils
 from Cython.Plex.Scanners import Scanner
 from Cython.Plex.Errors import UnrecognizedInput
 from Errors import CompileError, error
 from Lexicon import string_prefixes, raw_prefixes, make_lexicon, IDENT
+from Future import print_function
 
 from StringEncoding import EncodedString
 
@@ -345,7 +347,11 @@ class PyrexScanner(Scanner):
             self.error("Unrecognized character")
         if sy == IDENT:
             if systring in resword_dict:
-                sy = systring
+                if systring == 'print' and \
+                       print_function in self.context.future_directives:
+                    systring = EncodedString(systring)
+                else:
+                    sy = systring
             else:
                 systring = EncodedString(systring)
         self.sy = sy
index 593614d22f901f8ccdc52f961f021589bea0abce..f7a7c69bafbbffc7c9a05389847423bbfa050919 100644 (file)
@@ -48,8 +48,12 @@ EXT_DEP_INCLUDES = [
 ]
 
 VER_DEP_MODULES = {
+    # tests are excluded if 'CurrentPythonVersion OP VersionTuple', i.e.
+    # (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'
+                                          ]),
     (3,): (operator.ge, lambda x: x in ['run.non_future_division',
                                         'compile.extsetslice',
                                         'compile.extdelslice']),
diff --git a/tests/run/print_function.pyx b/tests/run/print_function.pyx
new file mode 100644 (file)
index 0000000..35f5d96
--- /dev/null
@@ -0,0 +1,64 @@
+
+# Py2.6 and later only!
+from __future__ import print_function
+
+def print_to_stdout(a, b):
+    """
+    >>> print_to_stdout(1, 'test')
+    <BLANKLINE>
+    1
+    1 test
+    1 test
+    1 test 42 spam
+    """
+    print()
+    print(a)
+    print(a, end=' ')
+    print(b)
+    print(a, b)
+    print(a, b, end=' ')
+    print(42, u"spam")
+
+def print_assign(a, b):
+    """
+    >>> print_assign(1, 'test')
+    <BLANKLINE>
+    1
+    1 test
+    1 test
+    1 test 42 spam
+    """
+    x = print
+    x()
+    x(a)
+    x(a, end=' ')
+    x(b)
+    x(a, b)
+    x(a, b, end=' ')
+    x(42, u"spam")
+
+
+try:
+    from StringIO import StringIO
+except ImportError:
+    from io import StringIO
+
+def print_to_stringio(stream, a, b):
+    """
+    >>> stream = StringIO()
+    >>> print_to_stringio(stream, 1, 'test')
+    >>> print(stream.getvalue())
+    <BLANKLINE>
+    1
+    1 test
+    1 test
+    1 test 42 spam
+    <BLANKLINE>
+    """
+    print(file=stream)
+    print(a, file=stream)
+    print(a, end=' ', file=stream)
+    print(b, file=stream)
+    print(a, b, file=stream)
+    print(a, b, end=' ', file=stream)
+    print(42, u"spam", file=stream)