From 67b915ec1375bfaa2309049460bd85718dfd5ac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Fri, 17 Nov 2023 13:19:28 +0200 Subject: [PATCH 1/2] gh-66944: Note no need to use `contextlib.closing` with `urlopen` --- Doc/library/contextlib.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Doc/library/contextlib.rst b/Doc/library/contextlib.rst index f6ebbfacfba509..a29064287cc136 100644 --- a/Doc/library/contextlib.rst +++ b/Doc/library/contextlib.rst @@ -182,6 +182,11 @@ Functions and classes provided: without needing to explicitly close ``page``. Even if an error occurs, ``page.close()`` will be called when the :keyword:`with` block is exited. + .. note:: + + As of Python 3, :func:`urllib.request.urlopen` return values can be + used as context managers directly; there is no need to use ``closing`` + with them. The example above is just for illustration purposes. .. function:: aclosing(thing) From e4163c03c6ab4336af6c9678c51de858d3f101a8 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Date: Tue, 23 Jan 2024 01:54:10 +0100 Subject: [PATCH 2/2] Update contextlib.rst --- Doc/library/contextlib.rst | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Doc/library/contextlib.rst b/Doc/library/contextlib.rst index a29064287cc136..c68ad559945cbb 100644 --- a/Doc/library/contextlib.rst +++ b/Doc/library/contextlib.rst @@ -184,9 +184,12 @@ Functions and classes provided: .. note:: - As of Python 3, :func:`urllib.request.urlopen` return values can be - used as context managers directly; there is no need to use ``closing`` - with them. The example above is just for illustration purposes. + Most types managing resources support the :term:`context manager` protocol, + which closes *thing* on leaving the :keyword:`with` statment. + As such, :func:`!closing` is most useful for third party types that don't + support context managers. + This example is purely for illustration purposes, + as :func:`~urllib.request.urlopen` would normally be used in a context manager. .. function:: aclosing(thing)