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.settimeout(0.001)
s.connect(('8.8.8.8', 9999))
try:
raises_socket_timeout()
except Exception:
print "print_exc():"
traceback.print_exc()
print "-------------"
print "uncaught:"
raise
Results in this output:
print_exc():
Traceback (most recent call last):
File "test.py", line 11, in <module>
raises_socket_timeout()
File "test.py", line 8, in raises_socket_timeout
s.connect(('8.8.8.8', 9999))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
timeout: timed out
-------------
uncaught:
Traceback (most recent call last):
File "test.py", line 11, in <module>
raises_socket_timeout()
File "test.py", line 8, in raises_socket_timeout
s.connect(('8.8.8.8', 9999))
File "/usr/lib/python2.7/socket.py", 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 logging.info(..., exc_info=True) and friends.
|