[pypy-commit] pypy py3.6: another corner case of PyTraceBack_Print with tracebacklimit

cfbolz pypy.commits at gmail.com
Wed Sep 18 15:27:30 EDT 2019


Author: Carl Friedrich Bolz-Tereick <cfbolz at gmx.de>
Branch: py3.6
Changeset: r97526:67edce2ce705
Date: 2019-09-18 21:26 +0200
http://bitbucket.org/pypy/pypy/changeset/67edce2ce705/

Log:	another corner case of PyTraceBack_Print with tracebacklimit

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
@@ -21,7 +21,7 @@
         pass
 
     try:
-        from traceback import print_exception
+        from traceback import print_exception, format_exception_only
         limit = getattr(sys, 'tracebacklimit', None)
         if isinstance(limit, int):
             # ok, this is bizarre, but, the meaning of sys.tracebacklimit is
@@ -30,7 +30,16 @@
             # https://bugs.python.org/issue38197
             # one is counting from the top, the other from the bottom of the
             # stack. so reverse polarity here
-            print_exception(exctype, value, traceback, limit=-limit)
+            if limit:
+                print_exception(exctype, value, traceback, limit=-limit)
+            else:
+                # the limit is 0. PyTraceBack_Print does not print
+                # Traceback (most recent call last):
+                # because there is indeed no traceback.
+                # the traceback module don't care
+                for line in format_exception_only(exctype, value):
+                    print(line, end="")
+
         else:
             print_exception(exctype, value, traceback)
     except:
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
@@ -400,13 +400,22 @@
 
         try:
             f1()
-        except ValueError as exc:
+        except ValueError:
             eh(*sys.exc_info())
         msg = err.getvalue()
-        print(msg)
         # should be removed by the limit
         assert "f1" not in msg
 
+        err = _io.StringIO()
+        sys.stderr = err
+        sys.tracebacklimit = 0
+        try:
+            f1()
+        except ValueError:
+            eh(*sys.exc_info())
+        msg = err.getvalue()
+        assert "Traceback (most recent call last):" not in msg
+
         sys.stderr = savestderr
         del sys.tracebacklimit
 


More information about the pypy-commit mailing list