Author: Armin Rigo <ar...@tunes.org>
Branch: 
Changeset: r94804:96023743535d
Date: 2018-07-05 16:47 +0200
http://bitbucket.org/pypy/pypy/changeset/96023743535d/

Log:    sys.excepthook() should add a newline to stdout if needed, in
        addition to flushing it

diff --git a/pypy/module/sys/app.py b/pypy/module/sys/app.py
--- a/pypy/module/sys/app.py
+++ b/pypy/module/sys/app.py
@@ -11,6 +11,11 @@
 
     # Flush stdout as well, both files may refer to the same file
     try:
+        if sys.stdout.softspace:
+            print
+    except:
+        pass
+    try:
         sys.stdout.flush()
     except:
         pass
diff --git a/pypy/module/sys/test/test_sysmodule.py 
b/pypy/module/sys/test/test_sysmodule.py
--- a/pypy/module/sys/test/test_sysmodule.py
+++ b/pypy/module/sys/test/test_sysmodule.py
@@ -256,6 +256,30 @@
             print repr(err.getvalue())
             assert err.getvalue().endswith("ValueError: %s\n" % expectedoutput)
 
+    def test_excepthook_flushes_stdout(self):
+        import sys, cStringIO
+        savestdout = sys.stdout
+        out = cStringIO.StringIO()
+        sys.stdout = out
+
+        eh = sys.__excepthook__
+
+        try:
+            raise ValueError(42)
+        except ValueError as exc:
+            print "hello"     # with end-of-line
+            eh(*sys.exc_info())
+        try:
+            raise ValueError(42)
+        except ValueError as exc:
+            print 123, 456,     # no end-of-line here
+            assert sys.stdout.softspace
+            eh(*sys.exc_info())
+            assert not sys.stdout.softspace
+
+        sys.stdout = savestdout
+        assert out.getvalue() == 'hello\n123 456\n'   # with a final \n added
+
     # FIXME: testing the code for a lost or replaced excepthook in
     # Python/pythonrun.c::PyErr_PrintEx() is tricky.
 
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to