mirror of
https://github.com/python/cpython.git
synced 2025-07-08 03:45:36 +00:00
gh-102778: Add sys.last_exc, deprecate sys.last_type, sys.last_value,sys.last_traceback (#102779)
This commit is contained in:
parent
039714d00f
commit
e1e9bab006
22 changed files with 103 additions and 36 deletions
|
@ -27,7 +27,10 @@ class StackTreeItem(TreeItem):
|
|||
|
||||
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 @@ class StackTreeItem(TreeItem):
|
|||
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 = 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
|
||||
|
@ -136,6 +143,7 @@ def _stack_viewer(parent): # htest #
|
|||
except NameError:
|
||||
exc_type, exc_value, exc_tb = sys.exc_info()
|
||||
# inject stack trace to sys
|
||||
sys.last_exc = exc_value
|
||||
sys.last_type = exc_type
|
||||
sys.last_value = exc_value
|
||||
sys.last_traceback = exc_tb
|
||||
|
@ -143,6 +151,7 @@ def _stack_viewer(parent): # htest #
|
|||
StackBrowser(top, flist=flist, top=top, tb=exc_tb)
|
||||
|
||||
# restore sys to original state
|
||||
del sys.last_exc
|
||||
del sys.last_type
|
||||
del sys.last_value
|
||||
del sys.last_traceback
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue