Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a03a09e

Browse files
authored
Replace with_traceback() with exception chaining and reraising (GH-32074)
1 parent f08a191 commit a03a09e

6 files changed

Lines changed: 16 additions & 12 deletions

File tree

Lib/test/support/socket_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def filter_error(err):
256256
err = a[0]
257257
# The error can also be wrapped as args[1]:
258258
# except socket.error as msg:
259-
# raise OSError('socket error', msg).with_traceback(sys.exc_info()[2])
259+
# raise OSError('socket error', msg) from msg
260260
elif len(a) >= 2 and isinstance(a[1], OSError):
261261
err = a[1]
262262
else:

Lib/urllib/parse.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -940,10 +940,9 @@ def urlencode(query, doseq=False, safe='', encoding=None, errors=None,
940940
# but that's a minor nit. Since the original implementation
941941
# allowed empty dicts that type of behavior probably should be
942942
# preserved for consistency
943-
except TypeError:
944-
ty, va, tb = sys.exc_info()
943+
except TypeError as err:
945944
raise TypeError("not a valid non-string sequence "
946-
"or mapping object").with_traceback(tb)
945+
"or mapping object") from err
947946

948947
l = []
949948
if not doseq:

Lib/urllib/request.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,8 +1579,7 @@ def ftp_open(self, req):
15791579
headers = email.message_from_string(headers)
15801580
return addinfourl(fp, headers, req.full_url)
15811581
except ftplib.all_errors as exp:
1582-
exc = URLError('ftp error: %r' % exp)
1583-
raise exc.with_traceback(sys.exc_info()[2])
1582+
raise URLError(f'ftp error: {exp}') from exp
15841583

15851584
def connect_ftp(self, user, passwd, host, port, dirs, timeout):
15861585
return ftpwrapper(user, passwd, host, port, dirs, timeout,
@@ -1791,7 +1790,7 @@ def open(self, fullurl, data=None):
17911790
except (HTTPError, URLError):
17921791
raise
17931792
except OSError as msg:
1794-
raise OSError('socket error', msg).with_traceback(sys.exc_info()[2])
1793+
raise OSError('socket error', msg) from msg
17951794

17961795
def open_unknown(self, fullurl, data=None):
17971796
"""Overridable interface to open unknown URL type."""
@@ -2093,7 +2092,7 @@ def open_ftp(self, url):
20932092
headers = email.message_from_string(headers)
20942093
return addinfourl(fp, headers, "ftp:" + url)
20952094
except ftperrors() as exp:
2096-
raise URLError('ftp error %r' % exp).with_traceback(sys.exc_info()[2])
2095+
raise URLError(f'ftp error: {exp}') from exp
20972096

20982097
def open_data(self, url, data=None):
20992098
"""Use "data" URL."""
@@ -2443,8 +2442,7 @@ def retrfile(self, file, type):
24432442
conn, retrlen = self.ftp.ntransfercmd(cmd)
24442443
except ftplib.error_perm as reason:
24452444
if str(reason)[:3] != '550':
2446-
raise URLError('ftp error: %r' % reason).with_traceback(
2447-
sys.exc_info()[2])
2445+
raise URLError(f'ftp error: {reason}') from reason
24482446
if not conn:
24492447
# Set transfer mode to ASCII!
24502448
self.ftp.voidcmd('TYPE A')

Lib/wsgiref/handlers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,7 @@ def start_response(self, status, headers,exc_info=None):
228228
if exc_info:
229229
try:
230230
if self.headers_sent:
231-
# Re-raise original exception if headers sent
232-
raise exc_info[0](exc_info[1]).with_traceback(exc_info[2])
231+
raise
233232
finally:
234233
exc_info = None # avoid dangling circular ref
235234
elif self.headers is not None:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Exception chaining is changed from
2+
:func:`Exception.with_traceback`/:func:`sys.exc_info` to :pep:`3134`.
3+
Patch by Oleg Iarygin.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
All :exc:`URLError` exception messages raised in
2+
:class:`urllib.request.URLopener` now contain a colon between ``ftp error``
3+
and the rest of the message. Previously,
4+
:func:`~urllib.request.URLopener.open_ftp` missed the colon. Patch by Oleg
5+
Iarygin.

0 commit comments

Comments
 (0)