gh-102832: IDLE - remove use of deprecated sys.last_xyzs for stackviewer (#103339)

This commit is contained in:
Irit Katriel 2023-06-08 21:19:05 +01:00 committed by GitHub
parent 68dfa49627
commit 3ee921d84f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 51 deletions

View File

@ -6,19 +6,12 @@ from test.support import requires
from tkinter import Tk
from idlelib.tree import TreeNode, ScrolledCanvas
import sys
class StackBrowserTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
svs = stackviewer.sys
try:
abc
except NameError:
svs.last_type, svs.last_value, svs.last_traceback = (
sys.exc_info())
requires('gui')
cls.root = Tk()
@ -26,8 +19,6 @@ class StackBrowserTest(unittest.TestCase):
@classmethod
def tearDownClass(cls):
svs = stackviewer.sys
del svs.last_traceback, svs.last_type, svs.last_value
cls.root.update_idletasks()
## for id in cls.root.tk.call('after', 'info'):
@ -36,7 +27,10 @@ class StackBrowserTest(unittest.TestCase):
del cls.root
def test_init(self):
sb = stackviewer.StackBrowser(self.root)
try:
abc
except NameError as exc:
sb = stackviewer.StackBrowser(self.root, exc)
isi = self.assertIsInstance
isi(stackviewer.sc, ScrolledCanvas)
isi(stackviewer.item, stackviewer.StackTreeItem)

View File

@ -1363,19 +1363,19 @@ class PyShell(OutputWindow):
self.text.tag_remove(self.user_input_insert_tags, index_before)
self.shell_sidebar.update_sidebar()
def open_stack_viewer(self, event=None):
def open_stack_viewer(self, event=None): # -n mode only
if self.interp.rpcclt:
return self.interp.remote_stack_viewer()
from idlelib.stackviewer import StackBrowser
try:
sys.last_traceback
StackBrowser(self.root, sys.last_value, self.flist)
except:
messagebox.showerror("No stack trace",
"There is no stack trace yet.\n"
"(sys.last_traceback is not defined)",
"(sys.last_value is not defined)",
parent=self.text)
return
from idlelib.stackviewer import StackBrowser
StackBrowser(self.root, self.flist)
return None
def view_restart_mark(self, event=None):
self.text.see("iomark")

View File

@ -622,7 +622,7 @@ class Executive:
def stackviewer(self, flist_oid=None):
if self.user_exc_info:
typ, val, tb = self.user_exc_info
_, exc, tb = self.user_exc_info
else:
return None
flist = None
@ -630,9 +630,8 @@ class Executive:
flist = self.rpchandler.get_remote_proxy(flist_oid)
while tb and tb.tb_frame.f_globals["__name__"] in ["rpc", "run"]:
tb = tb.tb_next
sys.last_type = typ
sys.last_value = val
item = stackviewer.StackTreeItem(flist, tb)
exc.__traceback__ = tb
item = stackviewer.StackTreeItem(exc, flist)
return debugobj_r.remote_object_tree_item(item)

View File

@ -1,33 +1,30 @@
import linecache
import os
import sys
import tkinter as tk
from idlelib.debugobj import ObjectTreeItem, make_objecttreeitem
from idlelib.tree import TreeNode, TreeItem, ScrolledCanvas
def StackBrowser(root, flist=None, tb=None, top=None):
def StackBrowser(root, exc, flist=None, top=None):
global sc, item, node # For testing.
if top is None:
top = tk.Toplevel(root)
sc = ScrolledCanvas(top, bg="white", highlightthickness=0)
sc.frame.pack(expand=1, fill="both")
item = StackTreeItem(flist, tb)
item = StackTreeItem(exc, flist)
node = TreeNode(sc.canvas, None, item)
node.expand()
class StackTreeItem(TreeItem):
def __init__(self, flist=None, tb=None):
def __init__(self, exc, flist=None):
self.flist = flist
self.stack = self.get_stack(tb)
self.text = self.get_exception()
self.stack = self.get_stack(None if exc is None else exc.__traceback__)
self.text = f"{type(exc).__name__}: {str(exc)}"
def get_stack(self, tb):
if tb is None:
tb = sys.last_traceback
stack = []
if tb and tb.tb_frame is None:
tb = tb.tb_next
@ -36,17 +33,7 @@ class StackTreeItem(TreeItem):
tb = tb.tb_next
return stack
def get_exception(self):
type = sys.last_type
value = sys.last_value
if hasattr(type, "__name__"):
type = type.__name__
s = str(type)
if value is not None:
s = s + ": " + str(value)
return s
def GetText(self):
def GetText(self): # Titlecase names are overrides.
return self.text
def GetSubList(self):
@ -133,19 +120,9 @@ def _stack_viewer(parent): # htest #
flist = PyShellFileList(top)
try: # to obtain a traceback object
intentional_name_error
except NameError:
exc_type, exc_value, exc_tb = sys.exc_info()
# inject stack trace to sys
sys.last_type = exc_type
sys.last_value = exc_value
sys.last_traceback = exc_tb
except NameError as e:
StackBrowser(top, e, flist=flist, top=top)
StackBrowser(top, flist=flist, top=top, tb=exc_tb)
# restore sys to original state
del sys.last_type
del sys.last_value
del sys.last_traceback
if __name__ == '__main__':
from unittest import main