summaryrefslogtreecommitdiff
path: root/Lib/code.py
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2014-09-29 11:25:00 -0400
committerR David Murray <rdmurray@bitdance.com>2014-09-29 11:25:00 -0400
commit73293efee5a03b7f349ea436d9a4b44c0a7e01e1 (patch)
treef5cab645efffe23f7ef42374cb2e0173ac8be407 /Lib/code.py
parentfb0ea434c663d28293bda00b80ab858f51cc65b4 (diff)
downloadcpython-73293efee5a03b7f349ea436d9a4b44c0a7e01e1.tar.gz
#17442: Add chained traceback support to InteractiveInterpreter.
Patch by Claudiu Popa.
Diffstat (limited to 'Lib/code.py')
-rw-r--r--Lib/code.py34
1 files changed, 22 insertions, 12 deletions
diff --git a/Lib/code.py b/Lib/code.py
index 6186e04140..86e1f03260 100644
--- a/Lib/code.py
+++ b/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.