From 3f0aaa124fc153d57595388136b88cb2fc2c77e6 Mon Sep 17 00:00:00 2001 From: Senthil Kumaran Date: Wed, 28 Oct 2026 08:55:44 -0700 Subject: [PATCH] gh-57945: Document exceptions not wrapped by urlopen() (GH-160830) Exceptions raised while reading or parsing the HTTP response, such as http.client.HTTPException subclasses and OSError subclasses like TimeoutError, propagate from urlopen() without being wrapped in URLError. Document this, and that ValueError is raised for an invalid URL. (cherry picked from commit 87f8c0d8a3214eafad87d2648102644a501452fa) Co-authored-by: Senthil Kumaran Co-authored-by: Claude Opus 5.5 --- Doc/library/urllib.request.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst index 80d4cbb83bfebbe..1df66903cb4eb1b 100644 --- a/Doc/library/urllib.request.rst +++ b/Doc/library/urllib.request.rst @@ -72,6 +72,16 @@ The :mod:`!urllib.request` module defines the following functions: Raises :exc:`~urllib.error.URLError` on protocol errors. + Exceptions raised while reading or parsing an HTTP response are not + wrapped in :exc:`~urllib.error.URLError` and propagate unchanged. + These include subclasses of :exc:`http.client.HTTPException`, such as + :exc:`~http.client.BadStatusLine` for a malformed status line, and + subclasses of :exc:`OSError`, such as :exc:`TimeoutError` if the server + does not respond within *timeout* seconds, or + :exc:`~http.client.RemoteDisconnected` if the server closes the + connection without sending a response. :exc:`ValueError` is raised if + *url* is not a valid URL. + Note that ``None`` may be returned if no handler handles the request (though the default installed global :class:`OpenerDirector` uses :class:`UnknownHandler` to ensure this never happens).