Skip to content

Commit a4b1625

Browse files
committed
pythongh-102832: [idle] reduce use of deprecated sys.last_xyzs
1 parent 059bb04 commit a4b1625

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

Lib/idlelib/stackviewer.py

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,26 @@
77
from idlelib.debugobj import ObjectTreeItem, make_objecttreeitem
88
from idlelib.tree import TreeNode, TreeItem, ScrolledCanvas
99

10-
def StackBrowser(root, flist=None, tb=None, top=None):
10+
def StackBrowser(root, flist=None, exc=None, top=None):
1111
global sc, item, node # For testing.
1212
if top is None:
1313
top = tk.Toplevel(root)
1414
sc = ScrolledCanvas(top, bg="white", highlightthickness=0)
1515
sc.frame.pack(expand=1, fill="both")
16-
item = StackTreeItem(flist, tb)
16+
item = StackTreeItem(flist, exc)
1717
node = TreeNode(sc.canvas, None, item)
1818
node.expand()
1919

2020

2121
class StackTreeItem(TreeItem):
2222

23-
def __init__(self, flist=None, tb=None):
23+
def __init__(self, flist=None, exc=None):
24+
assert isinstance(exc, BaseException)
2425
self.flist = flist
25-
self.stack = self.get_stack(tb)
26-
self.text = self.get_exception()
26+
self.stack = self.get_stack(exc.__traceback__)
27+
self.text = self.get_exception(exc)
2728

2829
def get_stack(self, tb):
29-
if tb is None:
30-
tb = sys.last_traceback
3130
stack = []
3231
if tb and tb.tb_frame is None:
3332
tb = tb.tb_next
@@ -36,14 +35,12 @@ def get_stack(self, tb):
3635
tb = tb.tb_next
3736
return stack
3837

39-
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)
45-
if value is not None:
46-
s = s + ": " + str(value)
38+
def get_exception(self, exc):
39+
assert isinstance(exc, BaseException)
40+
typ = type(exc)
41+
if hasattr(typ, "__name__"):
42+
typ = typ.__name__
43+
s = str(typ) + ": " + str(exc)
4744
return s
4845

4946
def GetText(self):
@@ -133,19 +130,11 @@ def _stack_viewer(parent): # htest #
133130
flist = PyShellFileList(top)
134131
try: # to obtain a traceback object
135132
intentional_name_error
136-
except NameError:
137-
exc_type, exc_value, exc_tb = sys.exc_info()
138-
# inject stack trace to sys
139-
sys.last_type = exc_type
140-
sys.last_value = exc_value
141-
sys.last_traceback = exc_tb
142-
143-
StackBrowser(top, flist=flist, top=top, tb=exc_tb)
144-
145-
# restore sys to original state
146-
del sys.last_type
147-
del sys.last_value
148-
del sys.last_traceback
133+
except NameError as e:
134+
exc = e
135+
136+
StackBrowser(top, flist=flist, top=top, tb=exc)
137+
149138

150139
if __name__ == '__main__':
151140
from unittest import main

0 commit comments

Comments
 (0)