Replace with_traceback() with exception chaining and reraising (GH-32074)

This commit is contained in:
Oleg Iarygin 2022-03-30 15:28:20 +03:00 committed by GitHub
parent f08a191882
commit a03a09e068
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 16 additions and 12 deletions

View file

@ -256,7 +256,7 @@ def transient_internet(resource_name, *, timeout=_NOT_SET, errnos=()):
err = a[0] err = a[0]
# The error can also be wrapped as args[1]: # The error can also be wrapped as args[1]:
# except socket.error as msg: # except socket.error as msg:
# raise OSError('socket error', msg).with_traceback(sys.exc_info()[2]) # raise OSError('socket error', msg) from msg
elif len(a) >= 2 and isinstance(a[1], OSError): elif len(a) >= 2 and isinstance(a[1], OSError):
err = a[1] err = a[1]
else: else:

View file

@ -940,10 +940,9 @@ def urlencode(query, doseq=False, safe='', encoding=None, errors=None,
# but that's a minor nit. Since the original implementation # but that's a minor nit. Since the original implementation
# allowed empty dicts that type of behavior probably should be # allowed empty dicts that type of behavior probably should be
# preserved for consistency # preserved for consistency
except TypeError: except TypeError as err:
ty, va, tb = sys.exc_info()
raise TypeError("not a valid non-string sequence " raise TypeError("not a valid non-string sequence "
"or mapping object").with_traceback(tb) "or mapping object") from err
l = [] l = []
if not doseq: if not doseq:

View file

@ -1579,8 +1579,7 @@ class FTPHandler(BaseHandler):
headers = email.message_from_string(headers) headers = email.message_from_string(headers)
return addinfourl(fp, headers, req.full_url) return addinfourl(fp, headers, req.full_url)
except ftplib.all_errors as exp: except ftplib.all_errors as exp:
exc = URLError('ftp error: %r' % exp) raise URLError(f'ftp error: {exp}') from exp
raise exc.with_traceback(sys.exc_info()[2])
def connect_ftp(self, user, passwd, host, port, dirs, timeout): def connect_ftp(self, user, passwd, host, port, dirs, timeout):
return ftpwrapper(user, passwd, host, port, dirs, timeout, return ftpwrapper(user, passwd, host, port, dirs, timeout,
@ -1791,7 +1790,7 @@ class URLopener:
except (HTTPError, URLError): except (HTTPError, URLError):
raise raise
except OSError as msg: except OSError as msg:
raise OSError('socket error', msg).with_traceback(sys.exc_info()[2]) raise OSError('socket error', msg) from msg
def open_unknown(self, fullurl, data=None): def open_unknown(self, fullurl, data=None):
"""Overridable interface to open unknown URL type.""" """Overridable interface to open unknown URL type."""
@ -2093,7 +2092,7 @@ class URLopener:
headers = email.message_from_string(headers) headers = email.message_from_string(headers)
return addinfourl(fp, headers, "ftp:" + url) return addinfourl(fp, headers, "ftp:" + url)
except ftperrors() as exp: except ftperrors() as exp:
raise URLError('ftp error %r' % exp).with_traceback(sys.exc_info()[2]) raise URLError(f'ftp error: {exp}') from exp
def open_data(self, url, data=None): def open_data(self, url, data=None):
"""Use "data" URL.""" """Use "data" URL."""
@ -2443,8 +2442,7 @@ class ftpwrapper:
conn, retrlen = self.ftp.ntransfercmd(cmd) conn, retrlen = self.ftp.ntransfercmd(cmd)
except ftplib.error_perm as reason: except ftplib.error_perm as reason:
if str(reason)[:3] != '550': if str(reason)[:3] != '550':
raise URLError('ftp error: %r' % reason).with_traceback( raise URLError(f'ftp error: {reason}') from reason
sys.exc_info()[2])
if not conn: if not conn:
# Set transfer mode to ASCII! # Set transfer mode to ASCII!
self.ftp.voidcmd('TYPE A') self.ftp.voidcmd('TYPE A')

View file

@ -228,8 +228,7 @@ class BaseHandler:
if exc_info: if exc_info:
try: try:
if self.headers_sent: if self.headers_sent:
# Re-raise original exception if headers sent raise
raise exc_info[0](exc_info[1]).with_traceback(exc_info[2])
finally: finally:
exc_info = None # avoid dangling circular ref exc_info = None # avoid dangling circular ref
elif self.headers is not None: elif self.headers is not None:

View file

@ -0,0 +1,3 @@
Exception chaining is changed from
:func:`Exception.with_traceback`/:func:`sys.exc_info` to :pep:`3134`.
Patch by Oleg Iarygin.

View file

@ -0,0 +1,5 @@
All :exc:`URLError` exception messages raised in
:class:`urllib.request.URLopener` now contain a colon between ``ftp error``
and the rest of the message. Previously,
:func:`~urllib.request.URLopener.open_ftp` missed the colon. Patch by Oleg
Iarygin.