From 6b74907c443408ba6891b772eee729273cd6ba2f Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Thu, 6 Apr 2023 16:51:43 +0100 Subject: [PATCH 1/4] gh-102778: add last_exc in idlelib, to replace sys.last_type/value/traceback --- Lib/idlelib/idle_test/test_stackviewer.py | 4 +++- Lib/idlelib/pyshell.py | 7 +++++-- Lib/idlelib/run.py | 2 ++ Lib/idlelib/stackviewer.py | 21 +++++++++++++++------ 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Lib/idlelib/idle_test/test_stackviewer.py b/Lib/idlelib/idle_test/test_stackviewer.py index 98f53f9537bb25..a670e5ca411104 100644 --- a/Lib/idlelib/idle_test/test_stackviewer.py +++ b/Lib/idlelib/idle_test/test_stackviewer.py @@ -16,7 +16,8 @@ def setUpClass(cls): svs = stackviewer.sys try: abc - except NameError: + except NameError as exc: + svs.last_exc = exc svs.last_type, svs.last_value, svs.last_traceback = ( sys.exc_info()) @@ -27,6 +28,7 @@ def setUpClass(cls): @classmethod def tearDownClass(cls): svs = stackviewer.sys + del svs.last_exc del svs.last_traceback, svs.last_type, svs.last_value cls.root.update_idletasks() diff --git a/Lib/idlelib/pyshell.py b/Lib/idlelib/pyshell.py index e68233a5a4131e..c671d764536624 100755 --- a/Lib/idlelib/pyshell.py +++ b/Lib/idlelib/pyshell.py @@ -1367,11 +1367,14 @@ def open_stack_viewer(self, event=None): if self.interp.rpcclt: return self.interp.remote_stack_viewer() try: - sys.last_traceback + if hasattr(sys, 'last_exc'): + sys.last_exc.__traceback__ + else: + sys.last_traceback except: messagebox.showerror("No stack trace", "There is no stack trace yet.\n" - "(sys.last_traceback is not defined)", + "(sys.last_exc.__traceback__ or sys.last_traceback is not defined)", parent=self.text) return from idlelib.stackviewer import StackBrowser diff --git a/Lib/idlelib/run.py b/Lib/idlelib/run.py index 577c49eb67b20d..079922b163ec6c 100644 --- a/Lib/idlelib/run.py +++ b/Lib/idlelib/run.py @@ -239,6 +239,7 @@ def print_exception(): efile = sys.stderr typ, val, tb = excinfo = sys.exc_info() sys.last_type, sys.last_value, sys.last_traceback = excinfo + sys.last_exc = val seen = set() def print_exc(typ, exc, tb): @@ -631,6 +632,7 @@ def stackviewer(self, flist_oid=None): tb = tb.tb_next sys.last_type = typ sys.last_value = val + sys.last_exc = val item = stackviewer.StackTreeItem(flist, tb) return debugobj_r.remote_object_tree_item(item) diff --git a/Lib/idlelib/stackviewer.py b/Lib/idlelib/stackviewer.py index 94ffb4eff4dd26..e6031ddbf172e1 100644 --- a/Lib/idlelib/stackviewer.py +++ b/Lib/idlelib/stackviewer.py @@ -27,7 +27,10 @@ def __init__(self, flist=None, tb=None): def get_stack(self, tb): if tb is None: - tb = sys.last_traceback + if hasattr(sys, 'last_exc'): + tb = sys.last_exc.__traceback__ + else: + tb = sys.last_traceback stack = [] if tb and tb.tb_frame is None: tb = tb.tb_next @@ -37,11 +40,15 @@ def get_stack(self, tb): 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 hasattr(sys, 'last_exc'): + typ = None if sys.last_exc is None else type(sys.last_exc) + value = sys.last_exc + else: + typ = sys.last_type + value = sys.last_value + if hasattr(typ, "__name__"): + typ = typ.__name__ + s = str(typ) if value is not None: s = s + ": " + str(value) return s @@ -139,6 +146,7 @@ def _stack_viewer(parent): # htest # sys.last_type = exc_type sys.last_value = exc_value sys.last_traceback = exc_tb + sys.last_exc = exc_value StackBrowser(top, flist=flist, top=top, tb=exc_tb) @@ -146,6 +154,7 @@ def _stack_viewer(parent): # htest # del sys.last_type del sys.last_value del sys.last_traceback + del sys.last_exc if __name__ == '__main__': from unittest import main From ff74713eaaee009cb0c14463b54e97f0e1caff7f Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Thu, 6 Apr 2023 16:56:01 +0100 Subject: [PATCH 2/4] add news --- .../next/Library/2023-04-06-16-55-51.gh-issue-102778.BWeAmE.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-04-06-16-55-51.gh-issue-102778.BWeAmE.rst diff --git a/Misc/NEWS.d/next/Library/2023-04-06-16-55-51.gh-issue-102778.BWeAmE.rst b/Misc/NEWS.d/next/Library/2023-04-06-16-55-51.gh-issue-102778.BWeAmE.rst new file mode 100644 index 00000000000000..64ae5b5b6d564b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-04-06-16-55-51.gh-issue-102778.BWeAmE.rst @@ -0,0 +1 @@ +Support ``sys.last_exc`` in :mod:`idlelib`. From 6e6f60bada5f2bba7c6c2e08b75eca19aef247aa Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 7 Apr 2023 23:09:38 +0100 Subject: [PATCH 3/4] revert most changes --- Lib/idlelib/idle_test/test_stackviewer.py | 4 +--- Lib/idlelib/pyshell.py | 7 ++----- Lib/idlelib/stackviewer.py | 21 ++++++--------------- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/Lib/idlelib/idle_test/test_stackviewer.py b/Lib/idlelib/idle_test/test_stackviewer.py index a670e5ca411104..98f53f9537bb25 100644 --- a/Lib/idlelib/idle_test/test_stackviewer.py +++ b/Lib/idlelib/idle_test/test_stackviewer.py @@ -16,8 +16,7 @@ def setUpClass(cls): svs = stackviewer.sys try: abc - except NameError as exc: - svs.last_exc = exc + except NameError: svs.last_type, svs.last_value, svs.last_traceback = ( sys.exc_info()) @@ -28,7 +27,6 @@ def setUpClass(cls): @classmethod def tearDownClass(cls): svs = stackviewer.sys - del svs.last_exc del svs.last_traceback, svs.last_type, svs.last_value cls.root.update_idletasks() diff --git a/Lib/idlelib/pyshell.py b/Lib/idlelib/pyshell.py index c671d764536624..e68233a5a4131e 100755 --- a/Lib/idlelib/pyshell.py +++ b/Lib/idlelib/pyshell.py @@ -1367,14 +1367,11 @@ def open_stack_viewer(self, event=None): if self.interp.rpcclt: return self.interp.remote_stack_viewer() try: - if hasattr(sys, 'last_exc'): - sys.last_exc.__traceback__ - else: - sys.last_traceback + sys.last_traceback except: messagebox.showerror("No stack trace", "There is no stack trace yet.\n" - "(sys.last_exc.__traceback__ or sys.last_traceback is not defined)", + "(sys.last_traceback is not defined)", parent=self.text) return from idlelib.stackviewer import StackBrowser diff --git a/Lib/idlelib/stackviewer.py b/Lib/idlelib/stackviewer.py index e6031ddbf172e1..94ffb4eff4dd26 100644 --- a/Lib/idlelib/stackviewer.py +++ b/Lib/idlelib/stackviewer.py @@ -27,10 +27,7 @@ def __init__(self, flist=None, tb=None): def get_stack(self, tb): if tb is None: - if hasattr(sys, 'last_exc'): - tb = sys.last_exc.__traceback__ - else: - tb = sys.last_traceback + tb = sys.last_traceback stack = [] if tb and tb.tb_frame is None: tb = tb.tb_next @@ -40,15 +37,11 @@ def get_stack(self, tb): return stack def get_exception(self): - if hasattr(sys, 'last_exc'): - typ = None if sys.last_exc is None else type(sys.last_exc) - value = sys.last_exc - else: - typ = sys.last_type - value = sys.last_value - if hasattr(typ, "__name__"): - typ = typ.__name__ - s = str(typ) + 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 @@ -146,7 +139,6 @@ def _stack_viewer(parent): # htest # sys.last_type = exc_type sys.last_value = exc_value sys.last_traceback = exc_tb - sys.last_exc = exc_value StackBrowser(top, flist=flist, top=top, tb=exc_tb) @@ -154,7 +146,6 @@ def _stack_viewer(parent): # htest # del sys.last_type del sys.last_value del sys.last_traceback - del sys.last_exc if __name__ == '__main__': from unittest import main From d407a35f78eb7fecd06982185f4ada383d608b27 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 7 Apr 2023 23:12:20 +0100 Subject: [PATCH 4/4] remove one more --- Lib/idlelib/run.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/idlelib/run.py b/Lib/idlelib/run.py index 079922b163ec6c..04ce615621ee7c 100644 --- a/Lib/idlelib/run.py +++ b/Lib/idlelib/run.py @@ -632,7 +632,6 @@ def stackviewer(self, flist_oid=None): tb = tb.tb_next sys.last_type = typ sys.last_value = val - sys.last_exc = val item = stackviewer.StackTreeItem(flist, tb) return debugobj_r.remote_object_tree_item(item)