mirror of
https://github.com/python/cpython.git
synced 2025-12-10 11:00:14 +00:00
Add traceback.format_exc().
This commit is contained in:
parent
904ed86a77
commit
f607fc5395
4 changed files with 22 additions and 7 deletions
|
|
@ -48,6 +48,11 @@ information in a thread-safe way instead of using the deprecated
|
||||||
variables.)
|
variables.)
|
||||||
\end{funcdesc}
|
\end{funcdesc}
|
||||||
|
|
||||||
|
\begin{funcdesc}{format_exc}{\optional{limit\optional{, file}}}
|
||||||
|
This is like \code{print_exc(\var{limit})} but returns a string
|
||||||
|
instead of printing to a file.
|
||||||
|
\end{funcdesc}
|
||||||
|
|
||||||
\begin{funcdesc}{print_last}{\optional{limit\optional{, file}}}
|
\begin{funcdesc}{print_last}{\optional{limit\optional{, file}}}
|
||||||
This is a shorthand for \code{print_exception(sys.last_type,
|
This is a shorthand for \code{print_exception(sys.last_type,
|
||||||
sys.last_value, sys.last_traceback, \var{limit}, \var{file})}.
|
sys.last_value, sys.last_traceback, \var{limit}, \var{file})}.
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,8 @@ except ImportError:
|
||||||
del _sys.modules[__name__]
|
del _sys.modules[__name__]
|
||||||
raise
|
raise
|
||||||
|
|
||||||
from StringIO import StringIO as _StringIO
|
|
||||||
from time import time as _time, sleep as _sleep
|
from time import time as _time, sleep as _sleep
|
||||||
from traceback import print_exc as _print_exc
|
from traceback import format_exc as _format_exc
|
||||||
|
|
||||||
# Rename some stuff so "from threading import *" is safe
|
# Rename some stuff so "from threading import *" is safe
|
||||||
__all__ = ['activeCount', 'Condition', 'currentThread', 'enumerate', 'Event',
|
__all__ = ['activeCount', 'Condition', 'currentThread', 'enumerate', 'Event',
|
||||||
|
|
@ -440,10 +439,8 @@ class Thread(_Verbose):
|
||||||
except:
|
except:
|
||||||
if __debug__:
|
if __debug__:
|
||||||
self._note("%s.__bootstrap(): unhandled exception", self)
|
self._note("%s.__bootstrap(): unhandled exception", self)
|
||||||
s = _StringIO()
|
|
||||||
_print_exc(file=s)
|
|
||||||
_sys.stderr.write("Exception in thread %s:\n%s\n" %
|
_sys.stderr.write("Exception in thread %s:\n%s\n" %
|
||||||
(self.getName(), s.getvalue()))
|
(self.getName(), _format_exc()))
|
||||||
else:
|
else:
|
||||||
if __debug__:
|
if __debug__:
|
||||||
self._note("%s.__bootstrap(): normal return", self)
|
self._note("%s.__bootstrap(): normal return", self)
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,8 @@ import types
|
||||||
|
|
||||||
__all__ = ['extract_stack', 'extract_tb', 'format_exception',
|
__all__ = ['extract_stack', 'extract_tb', 'format_exception',
|
||||||
'format_exception_only', 'format_list', 'format_stack',
|
'format_exception_only', 'format_list', 'format_stack',
|
||||||
'format_tb', 'print_exc', 'print_exception', 'print_last',
|
'format_tb', 'print_exc', 'format_exc', 'print_exception',
|
||||||
'print_stack', 'print_tb', 'tb_lineno']
|
'print_last', 'print_stack', 'print_tb', 'tb_lineno']
|
||||||
|
|
||||||
def _print(file, str='', terminator='\n'):
|
def _print(file, str='', terminator='\n'):
|
||||||
file.write(str+terminator)
|
file.write(str+terminator)
|
||||||
|
|
@ -211,6 +211,16 @@ def print_exc(limit=None, file=None):
|
||||||
finally:
|
finally:
|
||||||
etype = value = tb = None
|
etype = value = tb = None
|
||||||
|
|
||||||
|
|
||||||
|
def format_exc(limit=None):
|
||||||
|
"""Like print_exc() but return a string."""
|
||||||
|
try:
|
||||||
|
etype, value, tb = sys.exc_info()
|
||||||
|
return ''.join(format_exception(etype, value, tb, limit))
|
||||||
|
finally:
|
||||||
|
etype = value = tb = None
|
||||||
|
|
||||||
|
|
||||||
def print_last(limit=None, file=None):
|
def print_last(limit=None, file=None):
|
||||||
"""This is a shorthand for 'print_exception(sys.last_type,
|
"""This is a shorthand for 'print_exception(sys.last_type,
|
||||||
sys.last_value, sys.last_traceback, limit, file)'."""
|
sys.last_value, sys.last_traceback, limit, file)'."""
|
||||||
|
|
|
||||||
|
|
@ -101,6 +101,9 @@ Extension modules
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- traceback.format_exc has been added (similar to print_exc but it returns
|
||||||
|
a string).
|
||||||
|
|
||||||
- xmlrpclib.MultiCall has been added.
|
- xmlrpclib.MultiCall has been added.
|
||||||
|
|
||||||
- poplib.POP3_SSL has been added.
|
- poplib.POP3_SSL has been added.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue