Title: traceback module leaves off module name in last line of formatted tracebacks
Type: behavior Stage: resolved
Components: Versions: Python 2.7
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, iritkatriel, terry.reedy, waltermundt
Priority: normal Keywords:

Created on 2013-01-03 21:49 by waltermundt, last changed 2020-11-04 16:45 by iritkatriel. This issue is now closed.

Messages (5)
msg178991 - (view) Author: Walter Mundt (waltermundt) Date: 2013-01-03 21:49
The documentation for the traceback module states that it "exactly mimics the behavior of the Python interpreter when it prints a stack trace."  However, this does not seem to be the case.  In Python 2.7.3, executing the following:

    import socket
    import sys
    import traceback
    def raises_socket_timeout():
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect(('', 9999))
    except Exception:
        print "print_exc():"
        print "-------------"
        print "uncaught:"

Results in this output:

    Traceback (most recent call last):
      File "", line 11, in <module>
      File "", line 8, in raises_socket_timeout
        s.connect(('', 9999))
      File "/usr/lib/python2.7/", line 224, in meth
        return getattr(self._sock,name)(*args)
    timeout: timed out
    Traceback (most recent call last):
      File "", line 11, in <module>
      File "", line 8, in raises_socket_timeout
        s.connect(('', 9999))
      File "/usr/lib/python2.7/", line 224, in meth
        return getattr(self._sock,name)(*args)
    socket.timeout: timed out

Note that the last line of the former message is "timeout: timed out" while the latter is "socket.timeout: timed out" (much more informative).  I've run into this specific difference in trying to debug production systems that use the traceback module to log exceptions.  It also affects traceback.format_exc(), traceback.format_exception_only(), logging.exception(), and, exc_info=True) and friends.
msg179105 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-01-05 04:06
On Win7, 2.7.3 gives me the same output. (Running in IDLE, I don't get the 'socket.' prefix even on the second traceback.) With 3.3, I get socket.timeout both times (console or IDLE). So the problem seems to be 2.x only.

print_exc calls print_exception, which calls print_tb and format_exception_only, which is responsible for the last line.

The 2.7 code
    if (isinstance(etype, BaseException) or
        isinstance(etype, types.InstanceType) or
        etype is None or type(etype) is str):
        return [_format_final_exc_line(etype, value)]

is simplified in 3.x to
    if etype is None:
        return [_format_final_exc_line(etype, value)]

    stype = etype.__name__

3.3 (and 3.x, I presume) adds

    smod = etype.__module__
    if smod not in ("__main__", "builtins"):
        stype = smod + '.' + stype

That is where the 'socket' prefix is added in 3.3, so perhaps that should be added to 2.7.

Note: indenting code makes it harder to cut, paste, and run ;-(
Using 'print (x)', which works fine in 2.7, instead of 'print x' would also greatly help testing in 3.x, which is needed for any bug report.)
msg179131 - (view) Author: Walter Mundt (waltermundt) Date: 2013-01-05 17:12
Thanks for looking into the version applicability.  I'd bet that under the covers IDLE is relying on the traceback module too.  Anyone have a Windows CPython 2.7.3 and care to check the output of the test code in a cmd window?  I originally ran it unindented out of a file if that helps.

I will remember your comments on code indenting and use of cross-version print idioms in test code for future reference.  Others wanting to paste it into a file or interpreter can add an "if True:" prefix as a workaround for this bug.
msg179149 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-01-05 23:12
console = cmd window. I did run it there and that is where I duplicated your 2.7.3 result. I don't understand the different result with 2.7.3 IDLE, since it runs in a separate pythonw process that *should* give the same result as the python cmd window. But given that all in well in 3.3, I don't care to try to investigate. Who knows, maybe the fix you want would change both 2.7 IDLE results.
msg380342 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-11-04 16:45
This is a python 2 only issue.
Date User Action Args
2020-11-04 16:45:10iritkatrielsetstatus: open -> closed

nosy: + iritkatriel
messages: + msg380342

resolution: out of date
stage: resolved
2013-01-05 23:12:36terry.reedysetmessages: + msg179149
2013-01-05 17:12:42waltermundtsetmessages: + msg179131
2013-01-05 04:06:23terry.reedysetnosy: + georg.brandl, terry.reedy
messages: + msg179105
2013-01-03 21:49:05waltermundtcreate