8000 gh-102778: add last_exc in idlelib, to replace sys.last_type/value/tr… · iritkatriel/cpython@6b74907 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6b74907

Browse files
committed
pythongh-102778: add last_exc in idlelib, to replace sys.last_type/value/traceback
1 parent 52bc2e7 commit 6b74907

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

Lib/idlelib/idle_test/test_stackviewer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ def setUpClass(cls):
1616
svs = stackviewer.sys
1717
try:
1818
abc
19-
except NameError:
19+
except NameError as exc:
20+
svs.last_exc = exc
2021
svs.last_type, svs.last_value, svs.last_traceback = (
2122
sys.exc_info())
2223

@@ -27,6 +28,7 @@ def setUpClass(cls):
2728
@classmethod
2829
def tearDownClass(cls):
2930
svs = stackviewer.sys
31+
del svs.last_exc
3032
del svs.last_traceback, svs.last_type, svs.last_value
3133

3234
cls.root.update_idletasks()

Lib/idlelib/pyshell.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,11 +1367,14 @@ def open_stack_viewer(self, event=None):
13671367
if self.interp.rpcclt:
13681368
return self.interp.remote_stack_viewer()
13691369
try:
1370-
sys.last_traceback
1370+
if hasattr(sys, 'last_exc'):
1371+
sys.last_exc.__traceback__
1372+
else:
1373+
sys.last_traceback
13711374
except:
13721375
messagebox.showerror("No stack trace",
13731376
"There is no stack trace yet.\n"
1374-
"(sys.last_traceback is not defined)",
1377+
"(sys.last_exc.__traceback__ or sys.last_traceback is not defined)",
13751378
parent=self.text)
13761379
return
13771380
from idlelib.stackviewer import StackBrowser

Lib/idlelib/run.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ def print_exception():
239239
efile = sys.stderr
240240
typ, val, tb = excinfo = sys.exc_info()
241241
sys.last_type, sys.last_value, sys.last_traceback = excinfo
242+
sys.last_exc = val
242243
seen = set()
243244

244245
def print_exc(typ, exc, tb):
@@ -631,6 +632,7 @@ def stackviewer(self, flist_oid=None):
631632
tb = tb.tb_next
632633
sys.last_type = typ
633634
sys.last_value = val
635+
sys.last_exc = val
634636
item = stackviewer.StackTreeItem(flist, tb)
635637
return debugobj_r.remote_object_tree_item(item)
636638

Lib/idlelib/stackviewer.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ def __init__(self, flist=None, tb=None):
2727

2828
def get_stack(self, tb):
2929
if tb is None:
30-
tb = sys.last_traceback
30+
if hasattr(sys, 'last_exc'):
31+
tb = sys.last_exc.__traceback__
32+
else:
33+
tb = sys.last_traceback
3134
stack = []
3235
if tb and tb.tb_frame is None:
3336
tb = tb.tb_next
@@ -37,11 +40,15 @@ def get_stack(self, tb):
3740
return stack
3841

3942
def get_exception(self):
40-
type = sys.last_type
41-
value = sys.last_value
42-
if hasattr(type, "__name__"):
43-
type = type.__name__
44-
s = str(type)
43+
if hasattr(sys, 'last_exc'):
44+
typ = None if sys.last_exc is None else type(sys.last_exc)
45+
value = sys.last_exc
46+
else:
47+
typ = sys.last_type
48+
value = sys.last_value
49+
if hasattr(typ, "__name__"):
50+
typ = typ.__name__
51+
s = str(typ)
4552
if value is not None:
4653
s = s + ": " + str(value)
4754
return s
@@ -139,13 +146,15 @@ def _stack_viewer(parent): # htest #
139146
sys.last_type = exc_type
140147
sys.last_value = exc_value
141148
sys.last_traceback = exc_tb
149+
sys.last_exc = exc_value
142150

143151
StackBrowser(top, flist=flist, top=top, tb=exc_tb)
144152

145153
# restore sys to original state
146154
del sys.last_type
147155
del sys.last_value
148156
del sys.last_traceback
157+
del sys.last_exc
149158

150159
if __name__ == '__main__':
151160
from unittest import main

0 commit comments

Comments
 (0)
0