mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
#17442: Add chained traceback support to InteractiveInterpreter.
Patch by Claudiu Popa.
This commit is contained in:
parent
4d75a01798
commit
c31e6227f9
5 changed files with 70 additions and 12 deletions
34
Lib/code.py
34
Lib/code.py
|
@ -137,25 +137,35 @@ class InteractiveInterpreter:
|
|||
The output is written by self.write(), below.
|
||||
|
||||
"""
|
||||
sys.last_type, sys.last_value, last_tb = ei = sys.exc_info()
|
||||
sys.last_traceback = last_tb
|
||||
try:
|
||||
type, value, tb = sys.exc_info()
|
||||
sys.last_type = type
|
||||
sys.last_value = value
|
||||
sys.last_traceback = tb
|
||||
tblist = traceback.extract_tb(tb)
|
||||
del tblist[:1]
|
||||
lines = traceback.format_list(tblist)
|
||||
if lines:
|
||||
lines.insert(0, "Traceback (most recent call last):\n")
|
||||
lines.extend(traceback.format_exception_only(type, value))
|
||||
lines = []
|
||||
for value, tb in traceback._iter_chain(*ei[1:]):
|
||||
if isinstance(value, str):
|
||||
lines.append(value)
|
||||
lines.append('\n')
|
||||
continue
|
||||
if tb:
|
||||
tblist = traceback.extract_tb(tb)
|
||||
if tb is last_tb:
|
||||
# The last traceback includes the frame we
|
||||
# exec'd in
|
||||
del tblist[:1]
|
||||
tblines = traceback.format_list(tblist)
|
||||
if tblines:
|
||||
lines.append("Traceback (most recent call last):\n")
|
||||
lines.extend(tblines)
|
||||
lines.extend(traceback.format_exception_only(type(value),
|
||||
value))
|
||||
finally:
|
||||
tblist = tb = None
|
||||
tblist = last_tb = ei = None
|
||||
if sys.excepthook is sys.__excepthook__:
|
||||
self.write(''.join(lines))
|
||||
else:
|
||||
# If someone has set sys.excepthook, we let that take precedence
|
||||
# over self.write
|
||||
sys.excepthook(type, value, tb)
|
||||
sys.excepthook(type, value, last_tb)
|
||||
|
||||
def write(self, data):
|
||||
"""Write a string.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue