cython-devel
changeset 3068:f9baa86d0372
support 'from __future__ import print_function' in Py2.6+
| author | Stefan Behnel <scoder@users.berlios.de> |
|---|---|
| date | Wed Mar 10 08:46:58 2010 +0100 (3 years ago) |
| parents | 8c62d4ae1f04 |
| children | edf737ca860f |
| files | Cython/Compiler/Future.py Cython/Compiler/Scanning.py runtests.py tests/run/print_function.pyx |
line diff
1.1 --- a/Cython/Compiler/Future.py Wed Mar 10 08:06:33 2010 +0100
1.2 +++ b/Cython/Compiler/Future.py Wed Mar 10 08:46:58 2010 +0100
1.3 @@ -9,5 +9,6 @@
1.4 unicode_literals = _get_feature("unicode_literals")
1.5 with_statement = _get_feature("with_statement")
1.6 division = _get_feature("division")
1.7 +print_function = _get_feature("print_function")
1.8
1.9 del _get_feature
2.1 --- a/Cython/Compiler/Scanning.py Wed Mar 10 08:06:33 2010 +0100
2.2 +++ b/Cython/Compiler/Scanning.py Wed Mar 10 08:46:58 2010 +0100
2.3 @@ -10,13 +10,15 @@
2.4 from time import time
2.5
2.6 import cython
2.7 -cython.declare(EncodedString=object, string_prefixes=object, raw_prefixes=object, IDENT=object)
2.8 +cython.declare(EncodedString=object, string_prefixes=object, raw_prefixes=object, IDENT=object,
2.9 + print_function=object)
2.10
2.11 from Cython import Plex, Utils
2.12 from Cython.Plex.Scanners import Scanner
2.13 from Cython.Plex.Errors import UnrecognizedInput
2.14 from Errors import CompileError, error
2.15 from Lexicon import string_prefixes, raw_prefixes, make_lexicon, IDENT
2.16 +from Future import print_function
2.17
2.18 from StringEncoding import EncodedString
2.19
2.20 @@ -345,7 +347,11 @@
2.21 self.error("Unrecognized character")
2.22 if sy == IDENT:
2.23 if systring in resword_dict:
2.24 - sy = systring
2.25 + if systring == 'print' and \
2.26 + print_function in self.context.future_directives:
2.27 + systring = EncodedString(systring)
2.28 + else:
2.29 + sy = systring
2.30 else:
2.31 systring = EncodedString(systring)
2.32 self.sy = sy
3.1 --- a/runtests.py Wed Mar 10 08:06:33 2010 +0100
3.2 +++ b/runtests.py Wed Mar 10 08:46:58 2010 +0100
3.3 @@ -48,8 +48,12 @@
3.4 ]
3.5
3.6 VER_DEP_MODULES = {
3.7 + # tests are excluded if 'CurrentPythonVersion OP VersionTuple', i.e.
3.8 + # (2,4) : (operator.le, ...) excludes ... when PyVer <= 2.4.x
3.9 (2,4) : (operator.le, lambda x: x in ['run.extern_builtins_T258'
3.10 ]),
3.11 + (2,6) : (operator.lt, lambda x: x in ['run.print_function'
3.12 + ]),
3.13 (3,): (operator.ge, lambda x: x in ['run.non_future_division',
3.14 'compile.extsetslice',
3.15 'compile.extdelslice']),
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/tests/run/print_function.pyx Wed Mar 10 08:46:58 2010 +0100
4.3 @@ -0,0 +1,64 @@
4.4 +
4.5 +# Py2.6 and later only!
4.6 +from __future__ import print_function
4.7 +
4.8 +def print_to_stdout(a, b):
4.9 + """
4.10 + >>> print_to_stdout(1, 'test')
4.11 + <BLANKLINE>
4.12 + 1
4.13 + 1 test
4.14 + 1 test
4.15 + 1 test 42 spam
4.16 + """
4.17 + print()
4.18 + print(a)
4.19 + print(a, end=' ')
4.20 + print(b)
4.21 + print(a, b)
4.22 + print(a, b, end=' ')
4.23 + print(42, u"spam")
4.24 +
4.25 +def print_assign(a, b):
4.26 + """
4.27 + >>> print_assign(1, 'test')
4.28 + <BLANKLINE>
4.29 + 1
4.30 + 1 test
4.31 + 1 test
4.32 + 1 test 42 spam
4.33 + """
4.34 + x = print
4.35 + x()
4.36 + x(a)
4.37 + x(a, end=' ')
4.38 + x(b)
4.39 + x(a, b)
4.40 + x(a, b, end=' ')
4.41 + x(42, u"spam")
4.42 +
4.43 +
4.44 +try:
4.45 + from StringIO import StringIO
4.46 +except ImportError:
4.47 + from io import StringIO
4.48 +
4.49 +def print_to_stringio(stream, a, b):
4.50 + """
4.51 + >>> stream = StringIO()
4.52 + >>> print_to_stringio(stream, 1, 'test')
4.53 + >>> print(stream.getvalue())
4.54 + <BLANKLINE>
4.55 + 1
4.56 + 1 test
4.57 + 1 test
4.58 + 1 test 42 spam
4.59 + <BLANKLINE>
4.60 + """
4.61 + print(file=stream)
4.62 + print(a, file=stream)
4.63 + print(a, end=' ', file=stream)
4.64 + print(b, file=stream)
4.65 + print(a, b, file=stream)
4.66 + print(a, b, end=' ', file=stream)
4.67 + print(42, u"spam", file=stream)
