diff options
author | Oleg Iarygin <oleg@arhadthedev.net> | 2022-03-30 15:28:20 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-30 15:28:20 +0300 |
commit | a03a09e068435f47d02649dda93988dc44ffaaf1 (patch) | |
tree | 864a523218399cc068ec68567359e616740bac9b | |
parent | f08a191882f75bb79d42a49039892105b2212fb9 (diff) | |
download | cpython-a03a09e068435f47d02649dda93988dc44ffaaf1.tar.gz cpython-a03a09e068435f47d02649dda93988dc44ffaaf1.zip |
Replace with_traceback() with exception chaining and reraising (GH-32074)
-rw-r--r-- | Lib/test/support/socket_helper.py | 2 | ||||
-rw-r--r-- | Lib/urllib/parse.py | 5 | ||||
-rw-r--r-- | Lib/urllib/request.py | 10 | ||||
-rw-r--r-- | Lib/wsgiref/handlers.py | 3 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2022-03-23-13-55-41.bpo-47099.P6quRP.rst | 3 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2022-03-23-14-16-38.bpo-47099.2raait.rst | 5 |
6 files changed, 16 insertions, 12 deletions
diff --git a/Lib/test/support/socket_helper.py b/Lib/test/support/socket_helper.py index b51677383eb..0ee7a5d69a1 100644 --- a/Lib/test/support/socket_helper.py +++ b/Lib/test/support/socket_helper.py @@ -256,7 +256,7 @@ def transient_internet(resource_name, *, timeout=_NOT_SET, errnos=()): err = a[0] # The error can also be wrapped as args[1]: # 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): err = a[1] else: diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index 67ba308c409..d70a6943f0a 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -940,10 +940,9 @@ def urlencode(query, doseq=False, safe='', encoding=None, errors=None, # but that's a minor nit. Since the original implementation # allowed empty dicts that type of behavior probably should be # preserved for consistency - except TypeError: - ty, va, tb = sys.exc_info() + except TypeError as err: raise TypeError("not a valid non-string sequence " - "or mapping object").with_traceback(tb) + "or mapping object") from err l = [] if not doseq: diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 02f96265a89..84997f268c9 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -1579,8 +1579,7 @@ class FTPHandler(BaseHandler): headers = email.message_from_string(headers) return addinfourl(fp, headers, req.full_url) except ftplib.all_errors as exp: - exc = URLError('ftp error: %r' % exp) - raise exc.with_traceback(sys.exc_info()[2]) + raise URLError(f'ftp error: {exp}') from exp def connect_ftp(self, user, passwd, host, port, dirs, timeout): return ftpwrapper(user, passwd, host, port, dirs, timeout, @@ -1791,7 +1790,7 @@ class URLopener: except (HTTPError, URLError): raise 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): """Overridable interface to open unknown URL type.""" @@ -2093,7 +2092,7 @@ class URLopener: headers = email.message_from_string(headers) return addinfourl(fp, headers, "ftp:" + url) 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): """Use "data" URL.""" @@ -2443,8 +2442,7 @@ class ftpwrapper: conn, retrlen = self.ftp.ntransfercmd(cmd) except ftplib.error_perm as reason: if str(reason)[:3] != '550': - raise URLError('ftp error: %r' % reason).with_traceback( - sys.exc_info()[2]) + raise URLError(f'ftp error: {reason}') from reason if not conn: # Set transfer mode to ASCII! self.ftp.voidcmd('TYPE A') diff --git a/Lib/wsgiref/handlers.py b/Lib/wsgiref/handlers.py index 31360e58785..6623b700537 100644 --- a/Lib/wsgiref/handlers.py +++ b/Lib/wsgiref/handlers.py @@ -228,8 +228,7 @@ class BaseHandler: if exc_info: try: if self.headers_sent: - # Re-raise original exception if headers sent - raise exc_info[0](exc_info[1]).with_traceback(exc_info[2]) + raise finally: exc_info = None # avoid dangling circular ref elif self.headers is not None: diff --git a/Misc/NEWS.d/next/Library/2022-03-23-13-55-41.bpo-47099.P6quRP.rst b/Misc/NEWS.d/next/Library/2022-03-23-13-55-41.bpo-47099.P6quRP.rst new file mode 100644 index 00000000000..fa2c87e941e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-03-23-13-55-41.bpo-47099.P6quRP.rst @@ -0,0 +1,3 @@ +Exception chaining is changed from +:func:`Exception.with_traceback`/:func:`sys.exc_info` to :pep:`3134`. +Patch by Oleg Iarygin. diff --git a/Misc/NEWS.d/next/Library/2022-03-23-14-16-38.bpo-47099.2raait.rst b/Misc/NEWS.d/next/Library/2022-03-23-14-16-38.bpo-47099.2raait.rst new file mode 100644 index 00000000000..785e53c123f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-03-23-14-16-38.bpo-47099.2raait.rst @@ -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. |