[pypy-svn] r55375 - in pypy/dist/pypy/translator/goal: . test2

arigo at codespeak.net arigo at codespeak.net
Wed May 28 22:12:22 CEST 2008


Author: arigo
Date: Wed May 28 22:12:21 2008
New Revision: 55375

Modified:
   pypy/dist/pypy/translator/goal/app_main.py
   pypy/dist/pypy/translator/goal/test2/test_app_main.py
Log:
issue372 testing

When stdout is not a tty, use full buffering instead of
line buffering.


Modified: pypy/dist/pypy/translator/goal/app_main.py
==============================================================================
--- pypy/dist/pypy/translator/goal/app_main.py	(original)
+++ pypy/dist/pypy/translator/goal/app_main.py	Wed May 28 22:12:21 2008
@@ -141,6 +141,9 @@
     sys.stdout = sys.__stdout__ = os.fdopen(1, 'wb', 0)
     sys.stderr = sys.__stderr__ = os.fdopen(2, 'wb', 0)
 
+def set_fully_buffered_io():
+    sys.stdout = sys.__stdout__ = os.fdopen(1, 'w')
+
 # ____________________________________________________________
 # Main entry point
 
@@ -207,6 +210,7 @@
     run_module = False
     run_stdin = False
     oldstyle_classes = False
+    unbuffered = False
     while i < len(argv):
         arg = argv[i]
         if not arg.startswith('-'):
@@ -220,7 +224,7 @@
             run_command = True
             break
         elif arg == '-u':
-            set_unbuffered_io()
+            unbuffered = True
         elif arg == '-O':
             pass
         elif arg == '--version':
@@ -262,6 +266,12 @@
     # but we need more in the translated PyPy for the compiler package 
     sys.setrecursionlimit(5000)
 
+    if unbuffered:
+        set_unbuffered_io()
+    elif not sys.stdout.isatty():
+        set_fully_buffered_io()
+
+
     mainmodule = type(sys)('__main__')
     sys.modules['__main__'] = mainmodule
 

Modified: pypy/dist/pypy/translator/goal/test2/test_app_main.py
==============================================================================
--- pypy/dist/pypy/translator/goal/test2/test_app_main.py	(original)
+++ pypy/dist/pypy/translator/goal/test2/test_app_main.py	Wed May 28 22:12:21 2008
@@ -418,3 +418,22 @@
                                  expect_prompt=True, expect_banner=False)
         assert 'hello world\n' in data
         assert '42\n' in data
+
+    def test_non_interactive_stdout_fully_buffered(self):
+        path = getscript(r"""
+            import sys, time
+            sys.stdout.write('\x00(STDOUT)\n\x00')   # stays in buffers
+            time.sleep(1)
+            sys.stderr.write('\x00[STDERR]\n\x00')
+            time.sleep(1)
+            # stdout flushed automatically here
+            """)
+        cmdline = '%s -u "%s" %s' % (sys.executable, app_main, path)
+        print 'POPEN:', cmdline
+        child_in, child_out_err = os.popen4(cmdline)
+        data = child_out_err.read(11)
+        assert data == '\x00[STDERR]\n\x00'    # from stderr
+        child_in.close()
+        data = child_out_err.read(11)
+        assert data == '\x00(STDOUT)\n\x00'    # from stdout
+        child_out_err.close()



More information about the Pypy-commit mailing list