[pypy-svn] r75500 - in pypy/branch/fast-forward/pypy: interpreter/astcompiler interpreter/pyparser interpreter/test module/__builtin__ module/__builtin__/test

benjamin at codespeak.net benjamin at codespeak.net
Tue Jun 22 17:57:46 CEST 2010


Author: benjamin
Date: Tue Jun 22 17:57:45 2010
New Revision: 75500

Modified:
   pypy/branch/fast-forward/pypy/interpreter/astcompiler/consts.py
   pypy/branch/fast-forward/pypy/interpreter/pyparser/pygram.py
   pypy/branch/fast-forward/pypy/interpreter/pyparser/pyparse.py
   pypy/branch/fast-forward/pypy/interpreter/test/test_syntax.py
   pypy/branch/fast-forward/pypy/module/__builtin__/__init__.py
   pypy/branch/fast-forward/pypy/module/__builtin__/app_io.py
   pypy/branch/fast-forward/pypy/module/__builtin__/test/test_builtin.py
Log:
add print() function and print_function __future__

Modified: pypy/branch/fast-forward/pypy/interpreter/astcompiler/consts.py
==============================================================================
--- pypy/branch/fast-forward/pypy/interpreter/astcompiler/consts.py	(original)
+++ pypy/branch/fast-forward/pypy/interpreter/astcompiler/consts.py	Tue Jun 22 17:57:45 2010
@@ -15,6 +15,7 @@
 CO_FUTURE_DIVISION = 0x2000
 CO_FUTURE_ABSOLUTE_IMPORT = 0x4000
 CO_FUTURE_WITH_STATEMENT = 0x8000
+CO_FUTURE_PRINT_FUNCTION = 0x10000
 
 PyCF_SOURCE_IS_UTF8 = 0x0100
 PyCF_DONT_IMPLY_DEDENT = 0x0200

Modified: pypy/branch/fast-forward/pypy/interpreter/pyparser/pygram.py
==============================================================================
--- pypy/branch/fast-forward/pypy/interpreter/pyparser/pygram.py	(original)
+++ pypy/branch/fast-forward/pypy/interpreter/pyparser/pygram.py	Tue Jun 22 17:57:45 2010
@@ -19,6 +19,9 @@
 
 
 python_grammar = _get_python_grammar()
+python_grammar_no_print = python_grammar.shared_copy()
+python_grammar_no_print.keyword_ids = python_grammar_no_print.keyword_ids.copy()
+del python_grammar_no_print.keyword_ids["print"]
 
 class _Tokens(object):
     pass

Modified: pypy/branch/fast-forward/pypy/interpreter/pyparser/pyparse.py
==============================================================================
--- pypy/branch/fast-forward/pypy/interpreter/pyparser/pyparse.py	(original)
+++ pypy/branch/fast-forward/pypy/interpreter/pyparser/pyparse.py	Tue Jun 22 17:57:45 2010
@@ -129,7 +129,10 @@
 
         flags = compile_info.flags
 
-        self.grammar = pygram.python_grammar
+        if flags & consts.CO_FUTURE_PRINT_FUNCTION:
+            self.grammar = pygram.python_grammar_no_print
+        else:
+            self.grammar = pygram.python_grammar
 
         # The tokenizer is very picky about how it wants its input.
         source_lines = textsrc.splitlines(True)

Modified: pypy/branch/fast-forward/pypy/interpreter/test/test_syntax.py
==============================================================================
--- pypy/branch/fast-forward/pypy/interpreter/test/test_syntax.py	(original)
+++ pypy/branch/fast-forward/pypy/interpreter/test/test_syntax.py	Tue Jun 22 17:57:45 2010
@@ -315,6 +315,27 @@
         assert record == [2, 1]
 
 
+class AppTestPrintFunction:
+
+    def test_simple_print(self):
+        import __builtin__
+        s = """from __future__ import print_function
+x = print
+"""
+        ns = {}
+        exec s in ns
+        assert ns["x"] is getattr(__builtin__, "print")
+
+    def test_print(self):
+        s = """from __future__ import print_function
+import StringIO
+s = StringIO.StringIO()
+print("Hello,", "person", file=s)
+"""
+        ns = {}
+        exec s in ns
+        assert ns["s"].getvalue() == "Hello, person\n"
+
 class AppTestWith:
     def test_with_simple(self):
 

Modified: pypy/branch/fast-forward/pypy/module/__builtin__/__init__.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/__builtin__/__init__.py	(original)
+++ pypy/branch/fast-forward/pypy/module/__builtin__/__init__.py	Tue Jun 22 17:57:45 2010
@@ -27,6 +27,7 @@
         'execfile'      : 'app_io.execfile',
         'raw_input'     : 'app_io.raw_input',
         'input'         : 'app_io.input',
+        'print'         : 'app_io.print_',
 
         'apply'         : 'app_functional.apply',
         #'range'         : 'app_functional.range',

Modified: pypy/branch/fast-forward/pypy/module/__builtin__/app_io.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/__builtin__/app_io.py	(original)
+++ pypy/branch/fast-forward/pypy/module/__builtin__/app_io.py	Tue Jun 22 17:57:45 2010
@@ -69,3 +69,48 @@
 def input(prompt=None):
     """Equivalent to eval(raw_input(prompt))."""
     return eval(raw_input(prompt))
+
+def print_(*args, **kwargs):
+    """The new-style print function from py3k."""
+    fp = kwargs.pop("file", sys.stdout)
+    if fp is None:
+        return
+    def write(data):
+        if not isinstance(data, basestring):
+            data = str(data)
+        fp.write(data)
+    want_unicode = False
+    sep = kwargs.pop("sep", None)
+    if sep is not None:
+        if isinstance(sep, unicode):
+            want_unicode = True
+        elif not isinstance(sep, str):
+            raise TypeError("sep must be None or a string")
+    end = kwargs.pop("end", None)
+    if end is not None:
+        if isinstance(end, unicode):
+            want_unicode = True
+        elif not isinstance(end, str):
+            raise TypeError("end must be None or a string")
+    if kwargs:
+        raise TypeError("invalid keyword arguments to print()")
+    if not want_unicode:
+        for arg in args:
+            if isinstance(arg, unicode):
+                want_unicode = True
+                break
+    if want_unicode:
+        newline = u"\n"
+        space = u" "
+    else:
+        newline = "\n"
+        space = " "
+    if sep is None:
+        sep = space
+    if end is None:
+        end = newline
+    for i, arg in enumerate(args):
+        if i:
+            write(sep)
+        write(arg)
+    write(end)

Modified: pypy/branch/fast-forward/pypy/module/__builtin__/test/test_builtin.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/__builtin__/test/test_builtin.py	(original)
+++ pypy/branch/fast-forward/pypy/module/__builtin__/test/test_builtin.py	Tue Jun 22 17:57:45 2010
@@ -495,6 +495,44 @@
         firstlineno = co.co_firstlineno
         assert firstlineno == 2
 
+    def test_print_function(self):
+        import __builtin__
+        import sys
+        import StringIO
+        pr = getattr(__builtin__, "print")
+        save = sys.stdout
+        out = sys.stdout = StringIO.StringIO()
+        try:
+            pr("Hello,", "person!")
+        finally:
+            sys.stdout = save
+        assert out.getvalue() == "Hello, person!\n"
+        out = StringIO.StringIO()
+        pr("Hello,", "person!", file=out)
+        assert out.getvalue() == "Hello, person!\n"
+        out = StringIO.StringIO()
+        pr("Hello,", "person!", file=out, end="")
+        assert out.getvalue() == "Hello, person!"
+        out = StringIO.StringIO()
+        pr("Hello,", "person!", file=out, sep="X")
+        assert out.getvalue() == "Hello,Xperson!\n"
+        out = StringIO.StringIO()
+        pr(u"Hello,", u"person!", file=out)
+        result = out.getvalue()
+        assert isinstance(result, unicode)
+        assert result == u"Hello, person!\n"
+        pr("Hello", file=None) # This works.
+        out = StringIO.StringIO()
+        pr(None, file=out)
+        assert out.getvalue == "None"
+
+    def test_print_exceptions(self):
+        import __builtin__
+        pr = getattr(__builtin__, "print")
+        raises(TypeError, pr, x=3)
+        raises(TypeError, pr, end=3)
+        raises(TypeError, pr, sep=42)
+
 class AppTestBuiltinOptimized(object):
     def setup_class(cls):
         from pypy.conftest import gettestobjspace



More information about the Pypy-commit mailing list