This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Meaning of tracebacklimit differs between sys.tracebacklimit and traceback module
Type: Stage:
Components: Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Carl.Friedrich.Bolz, iritkatriel
Priority: normal Keywords:

Created on 2019-09-17 10:34 by Carl.Friedrich.Bolz, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
x.py Carl.Friedrich.Bolz, 2019-09-17 10:34
Messages (3)
msg352628 - (view) Author: Carl Friedrich Bolz-Tereick (Carl.Friedrich.Bolz) * Date: 2019-09-17 10:34
The meaning of sys.tracebacklimit seems to be different than the meaning of the various limit parameters in the traceback module. One shows the top n stack frames, the other the bottom n.

Is this intentional, and if yes, is that difference documented somewhere? (it came up because PyPy just uses the traceback module and has no equivalent of PyTraceBack_Print).

See the attached script to understand the problem. The script formats the same exception twice, once with the traceback module, once by the interpreter. I would have expected them to look the same for all limits, but instead:

$ ./python /tmp/x.py 3
limit 3
from traceback module:
Traceback (most recent call last):
  File "/tmp/x.py", line 19, in <module>
    main()
  File "/tmp/x.py", line 16, in main
    x3()
  File "/tmp/x.py", line 14, in x3
    x2()
ZeroDivisionError: division by zero

from interpreter:
Traceback (most recent call last):
  File "/tmp/x.py", line 14, in x3
    x2()
  File "/tmp/x.py", line 12, in x2
    x1()
  File "/tmp/x.py", line 10, in x1
    1 / 0
ZeroDivisionError: division by zero
msg380352 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-11-04 19:41
If you change your script to do
sys.tracebacklimit = abs(limit)
and run it with arg -3, then you get the output you expect:

C:\Users\User\src\cpython>python.bat x.py -3
Running Release|Win32 interpreter...
limit -3
from traceback module:
Traceback (most recent call last):
  File "C:\Users\User\src\cpython\x.py", line 14, in x3
    x2()
  File "C:\Users\User\src\cpython\x.py", line 12, in x2
    x1()
  File "C:\Users\User\src\cpython\x.py", line 10, in x1
    1 / 0
ZeroDivisionError: division by zero

from interpreter:
Traceback (most recent call last):
  File "C:\Users\User\src\cpython\x.py", line 14, in x3
    x2()
  File "C:\Users\User\src\cpython\x.py", line 12, in x2
    x1()
  File "C:\Users\User\src\cpython\x.py", line 10, in x1
    1 / 0
ZeroDivisionError: division by zero


The documentation for traceback mentions the possibility of using negative limits and their meaning:

Print up to limit stack trace entries from traceback object tb (starting from the caller’s frame) if limit is positive. Otherwise, print the last abs(limit) entries. 

https://docs.python.org/3/library/traceback.html#traceback.print_tb
msg380355 - (view) Author: Carl Friedrich Bolz-Tereick (Carl.Friedrich.Bolz) * Date: 2020-11-04 19:48
It's still inconsistent between the two ways to get a traceback, and the inconsistency is not documented.
History
Date User Action Args
2022-04-11 14:59:20adminsetgithub: 82378
2020-11-04 19:48:39Carl.Friedrich.Bolzsetmessages: + msg380355
2020-11-04 19:41:49iritkatrielsetnosy: + iritkatriel

messages: + msg380352
versions: + Python 3.8, Python 3.9, Python 3.10, - Python 3.7
2019-09-17 10:34:55Carl.Friedrich.Bolzcreate