8000 update stdlib · python/cpython@242708e · GitHub
[go: up one dir, main page]

Skip to content

Commit 242708e

Browse files
committed
update stdlib
1 parent 008355f commit 242708e

File tree

12 files changed

+56
-19
lines changed

12 files changed

+56
-19
lines changed

Lib/code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def showsyntaxerror(self, filename=None):
120120
else:
121121
# Stuff in the right filename
122122
value = SyntaxError(msg, (filename, lineno, offset, line))
123-
sys.last_value = value
123+
sys.last_exc = sys.last_value = value
124124
if sys.excepthook is sys.__excepthook__:
125125
lines = traceback.format_exception_only(type, value)
126126
self.write(''.join(lines))
10000

Lib/dis.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ def distb(tb=None, *, file=None, show_caches=False, adaptive=False):
118118
"""Disassemble a traceback (default: last traceback)."""
119119
if tb is None:
120120
try:
121-
tb = sys.last_traceback
121+
if hasattr(sys, 'last_exc'):
122+
tb = sys.last_exc.__traceback__
123+
else:
124+
tb = sys.last_traceback
122125
except AttributeError:
123126
raise RuntimeError("no last traceback to disassemble") from None
124127
while tb.tb_next: tb = tb.tb_next

Lib/idlelib/idle_test/test_stackviewer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def setUpClass(cls):
1919
except NameError:
2020
svs.last_type, svs.last_value, svs.last_traceback = (
2121
sys.exc_info())
22+
svs.last_exc = svs.last_value
2223

2324
requires('gui')
2425
cls.root = Tk()
@@ -27,7 +28,7 @@ def setUpClass(cls):
2728
@classmethod
2829
def tearDownClass(cls):
2930
svs = stackviewer.sys
30-
del svs.last_traceback, svs.last_type, svs.last_value
31+
del svs.last_exc, svs.last_traceback, svs.last_type, svs.last_value
3132

3233
cls.root.update_idletasks()
3334
## for id in cls.root.tk.call('after', 'info'):

Lib/idlelib/pyshell.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,11 +1367,14 @@ def open_stack_viewer(self, event=None):
13671367
if self.interp.rpcclt:
13681368
return self.interp.remote_stack_viewer()
13691369
try:
1370-
sys.last_traceback
1370+
if hasattr(sys, 'last_exc'):
1371+
sys.last_exc.__traceback__
1372+
else:
1373+
sys.last_traceback
13711374
except:
13721375
messagebox.showerror("No stack trace",
13731376
"There is no stack trace yet.\n"
1374-
"(sys.last_traceback is not defined)",
1377+
"(sys.last_exc and sys.last_traceback are not defined)",
13751378
parent=self.text)
13761379
return
13771380
from idlelib.stackviewer import StackBrowser

Lib/idlelib/run.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ def print_exception():
239239
efile = sys.stderr
240240
typ, val, tb = excinfo = sys.exc_info()
241241
sys.last_type, sys.last_value, sys.last_traceback = excinfo
242+
sys.last_exc = val
242243
seen = set()
243244

244245
def print_exc(typ, exc, tb):
@@ -629,6 +630,7 @@ def stackviewer(self, flist_oid=None):
629630
flist = self.rpchandler.get_remote_proxy(flist_oid)
630631
while tb and tb.tb_frame.f_globals["__name__"] in ["rpc", "run"]:
631632
tb = tb.tb_next
633+
sys.last_exc = val
632634
sys.last_type = typ
633635
sys.last_value = val
634636
item = stackviewer.StackTreeItem(flist, tb)

Lib/idlelib/stackviewer.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ def __init__(self, flist=None, tb=None):
2727

2828
def get_stack(self, tb):
2929
if tb is None:
30-
tb = sys.last_traceback
30+
if hasattr(sys, 'last_exc'):
31+
tb = sys.last_exc.__traceback__
32+
else:
33+
tb = sys.last_traceback
3134
stack = []
3235
if tb and tb.tb_frame is None:
3336
tb = tb.tb_next
@@ -37,8 +40,12 @@ def get_stack(self, tb):
3740
return stack
3841

3942
def get_exception(self):
40-
type = sys.last_type
41-
value = sys.last_value
43+
if hasattr(sys, 'last_exc'):
44+
type = type(sys.last_exc)
45+
value = sys.last_exc
46+
else:
47+
type = sys.last_type
48+
value = sys.last_value
4249
if hasattr(type, "__name__"):
4350
type = type.__name__
4451
s = str(type)
@@ -136,13 +143,15 @@ def _stack_viewer(parent): # htest #
136143
except NameError:
137144
exc_type, exc_value, exc_tb = sys.exc_info()
138145
# inject stack trace to sys
146+
sys.last_exc = exc_value
139147
sys.last_type = exc_type
140148
sys.last_value = exc_value
141149
sys.last_traceback = exc_tb
142150

143151
StackBrowser(top, flist=flist, top=top, tb=exc_tb)
144152

145153
# restore sys to original state
154+
del sys.last_exc
146155
del sys.last_type
147156
del sys.last_value
148157
del sys.last_traceback

Lib/pdb.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1739,7 +1739,11 @@ def post_mortem(t=None):
17391739

17401740
def pm():
17411741
"""Enter post-mortem debugging of the traceback found in sys.last_traceback."""
1742-
post_mortem(sys.last_traceback)
1742+
if hasattr(sys, 'last_exc'):
1743+
tb = sys.last_exc.__traceback__
1744+
else:
1745+
tb = sys.last_traceback
1746+
post_mortem(tb)
17431747

17441748

17451749
# Main program for testing

Lib/pydoc_data/topics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4799,7 +4799,7 @@
47994799
'pdb.pm()\n'
48004800
'\n'
48014801
' Enter post-mortem debugging of the traceback found in\n'
4802-
' "sys.last_traceback".\n'
4802+
' "sys.last_exc".\n'
48034803
'\n'
48044804
'The "run*" functions and "set_trace()" are aliases for '
48054805
'instantiating\n'
@@ -13858,7 +13858,7 @@
1385813858
'if\n'
1385913859
' the interpreter is interactive, it is also made available to '
1386013860
'the\n'
13861-
' user as "sys.last_traceback".\n'
13861+
' user as "sys.last_exc".\n'
1386213862
'\n'
1386313863
' For explicitly created tracebacks, it is up to the creator '
1386413864
'of\n'

Lib/test/test_dis.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,10 @@ def test_disassemble_try_finally(self):
10261026
self.do_disassembly_test(_tryfinallyconst, dis_tryfinallyconst)
10271027

10281028
def test_dis_none(self):
1029+
try:
1030+
del sys.last_exc
1031+
except AttributeError:
1032+
pass
10291033
try:
10301034
del sys.last_traceback
10311035
except AttributeError:
@@ -1043,7 +1047,7 @@ def test_dis_traceback(self):
10431047
1/0
10441048
except Exception as e:
10451049
tb = e.__traceback__
1046-
sys.last_traceback = tb
1050+
sys.last_exc = e
10471051

10481052
tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
10491053
self.do_disassembly_test(None, tb_dis, True)
@@ -1900,6 +1904,10 @@ def test_findlabels(self):
19001904

19011905
class TestDisTraceback(DisTestBase):
19021906
def setUp(self) -> None:
1907+
try: # We need to clean up existing tracebacks
1908+
del sys.last_exc
1909+
except AttributeError:
1910+
pass
19031911
try: # We need to clean up existing tracebacks
19041912
del sys.last_traceback
19051913
except AttributeError:

Lib/test/test_ttk/test_extensions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ def test_widget_destroy(self):
4545
# value which causes the tracing callback to be called and then
4646
# it tries calling instance attributes not yet defined.
4747
ttk.LabeledScale(self.root, variable=myvar)
48-
if hasattr(sys, 'last_type'):
48+
if hasattr(sys, 'last_exc'):
49+
self.assertNotEqual(type(sys.last_exc), tkinter.TclError)
50+
elif hasattr(sys, 'last_type'):
4951
self.assertNotEqual(sys.last_type, tkinter.TclError)
5052

5153
def test_initialization(self):

Lib/tkinter/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,6 +2400,7 @@ def report_callback_exception(self, exc, val, tb):
24002400
should when sys.stderr is None."""
24012401
import traceback
24022402
print("Exception in Tkinter callback", file=sys.stderr)
2403+
sys.last_exc = val
24032404
sys.last_type = exc
24042405
sys.last_value = val
24052406
sys.last_traceback = tb

Lib/traceback.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,24 @@ def _safe_string(value, what, func=str):
179179
# --
180180

181181
def print_exc(limit=None, file=None, chain=True):
182-
"""Shorthand for 'print_exception(*sys.exc_info(), limit, file)'."""
182+
"""Shorthand for 'print_exception(*sys.exc_info(), limit, file, chain)'."""
183183
print_exception(*sys.exc_info(), limit=limit, file=file, chain=chain)
184184

185185
def format_exc(limit=None, chain=True):
186186
"""Like print_exc() but return a string."""
187187
return "".join(format_exception(*sys.exc_info(), limit=limit, chain=chain))
188188

189189
def print_last(limit=None, file=None, chain=True):
190-
"""This is a shorthand for 'print_exception(sys.last_type,
191-
sys.last_value, sys.last_traceback, limit, file)'."""
192-
if not hasattr(sys, "last_type"):
190+
"""This is a shorthand for 'print_exception(sys.last_exc, limit, file, chain)'."""
191+
if not hasattr(sys, "last_exc") and not hasattr(sys, "last_type"):
193192
raise ValueError("no last exception")
194-
print_exception(sys.last_type, sys.last_value, sys.last_traceback,
195-
limit, file, chain)
193+
194+
if hasattr(sys, "last_exc"):
195+
print_exception(sys.last_exc, limit, file, chain)
196+
else:
197+
print_exception(sys.last_type, sys.last_value, sys.last_traceback,
198+
limit, file, chain)
199+
196200

197201
#
198202
# Printing and Extracting Stacks.

0 commit comments

Comments
 (0)
0