Skip to content

Commit

Permalink
[3.11] gh-102832: IDLE - remove use of deprecated sys.last_xyzs for s…
Browse files Browse the repository at this point in the history
…tackviewer (GH-103339) (#105527)

gh-102832: IDLE - remove use of deprecated sys.last_xyzs for stackviewer (GH-103339)
(cherry picked from commit 3ee921d)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
  • Loading branch information
miss-islington and iritkatriel committed Jun 8, 2023
1 parent b073b30 commit c2df09f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 52 deletions.
14 changes: 4 additions & 10 deletions Lib/idlelib/idle_test/test_stackviewer.py
Expand Up @@ -6,28 +6,19 @@
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()
cls.root.withdraw()

@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'):
Expand All @@ -36,7 +27,10 @@ def tearDownClass(cls):
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)
Expand Down
12 changes: 6 additions & 6 deletions Lib/idlelib/pyshell.py
Expand Up @@ -1363,19 +1363,19 @@ def runit(self):
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")
Expand Down
7 changes: 3 additions & 4 deletions Lib/idlelib/run.py
Expand Up @@ -621,17 +621,16 @@ def get_the_completion_list(self, what, mode):

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
if flist_oid is not None:
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)


Expand Down
41 changes: 9 additions & 32 deletions Lib/idlelib/stackviewer.py
@@ -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
Expand All @@ -36,17 +33,7 @@ def get_stack(self, tb):
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):
Expand Down Expand Up @@ -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

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
except NameError as e:
StackBrowser(top, e, flist=flist, top=top)


if __name__ == '__main__':
from unittest import main
Expand Down

0 comments on commit c2df09f

Please sign in to comment.