From 44739ff78af12d7993a8ae4668fc25820feaa5ab Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Wed, 10 Mar 2010 08:46:58 +0100 Subject: [PATCH] support 'from __future__ import print_function' in Py2.6+ --HG-- rename : tests/run/print.pyx => tests/run/print_function.pyx --- Cython/Compiler/Future.py | 1 + Cython/Compiler/Scanning.py | 10 ++++-- runtests.py | 4 +++ tests/run/print_function.pyx | 64 ++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 tests/run/print_function.pyx diff --git a/Cython/Compiler/Future.py b/Cython/Compiler/Future.py index c42e595f..ef2c61f6 100644 --- a/Cython/Compiler/Future.py +++ b/Cython/Compiler/Future.py @@ -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 diff --git a/Cython/Compiler/Scanning.py b/Cython/Compiler/Scanning.py index 3dd7c916..562ed962 100644 --- a/Cython/Compiler/Scanning.py +++ b/Cython/Compiler/Scanning.py @@ -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 diff --git a/runtests.py b/runtests.py index 593614d2..f7a7c69b 100644 --- a/runtests.py +++ b/runtests.py @@ -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 index 00000000..35f5d967 --- /dev/null +++ b/tests/run/print_function.pyx @@ -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') + + 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') + + 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()) + + 1 + 1 test + 1 test + 1 test 42 spam + + """ + 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) -- 2.26.2