From 600ffe1bf2c3b33fbc6a17a703fa52f84edbe84c Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Mon, 11 Nov 2024 07:52:27 +0000 Subject: [PATCH 1/7] gh-126639: Add ResourceWarning to NamedTemporaryFile --- Lib/tempfile.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/Lib/tempfile.py b/Lib/tempfile.py index b5a15f7b72c872..a84751b8e07469 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -437,11 +437,19 @@ class _TemporaryFileCloser: cleanup_called = False close_called = False - def __init__(self, file, name, delete=True, delete_on_close=True): + def __init__( + self, + file, + name, + delete=True, + delete_on_close=True, + warn_message="Implicitly cleaning up unknown file", + ): self.file = file self.name = name self.delete = delete self.delete_on_close = delete_on_close + self.warn_message = warn_message def cleanup(self, windows=(_os.name == 'nt'), unlink=_os.unlink): if not self.cleanup_called: @@ -469,7 +477,10 @@ def close(self): self.cleanup() def __del__(self): + close_called = self.close_called self.cleanup() + if not close_called: + _warnings.warn(self.warn_message, ResourceWarning) class _TemporaryFileWrapper: @@ -483,8 +494,17 @@ class _TemporaryFileWrapper: def __init__(self, file, name, delete=True, delete_on_close=True): self.file = file self.name = name - self._closer = _TemporaryFileCloser(file, name, delete, - delete_on_close) + self._closer = _TemporaryFileCloser( + file, + name, + delete, + delete_on_close, + warn_message="Implicitly cleaning up {self!r}", + ) + + def __repr__(self): + file = self.__dict__['file'] + return f"<{type(self).__name__} {file=}>" def __getattr__(self, name): # Attribute lookups are delegated to the underlying file From 4932cd3454672770aaa27f6404d84186dac233c0 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 11 Nov 2024 07:56:08 +0000 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-11-11-07-56-03.gh-issue-126639.AmVSt-.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-11-11-07-56-03.gh-issue-126639.AmVSt-.rst diff --git a/Misc/NEWS.d/next/Library/2024-11-11-07-56-03.gh-issue-126639.AmVSt-.rst b/Misc/NEWS.d/next/Library/2024-11-11-07-56-03.gh-issue-126639.AmVSt-.rst new file mode 100644 index 00000000000000..c702d09fd6071d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-11-11-07-56-03.gh-issue-126639.AmVSt-.rst @@ -0,0 +1 @@ +Add :exc:`ResourceWarning` to :class:`tempfile.NamedTemporaryFile` From 136ce715bd58d973215caad919cb59ca749b76b0 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Mon, 11 Nov 2024 15:39:39 +0000 Subject: [PATCH 3/7] test that NamedTemporaryFile issues a ResourceWarning --- Lib/test/test_tempfile.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index a5e182cef23dc5..ab936e9c4f9dfc 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -1116,11 +1116,14 @@ def my_func(dir): # Testing extreme case, where the file is not explicitly closed # f.close() return tmp_name - # Make sure that the garbage collector has finalized the file object. - gc.collect() dir = tempfile.mkdtemp() try: - tmp_name = my_func(dir) + with self.assertWarnsRegex( + ResourceWarning, + msg=r"Implicitly cleaning up <_TemporaryFileWrapper file=.*>", + ): + tmp_name = my_func(dir) + support.gc_collect() self.assertFalse(os.path.exists(tmp_name), f"NamedTemporaryFile {tmp_name!r} " f"exists after finalizer ") From b6e91c187ff6d4e068c50eef9a323487780fb69f Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Mon, 11 Nov 2024 15:42:30 +0000 Subject: [PATCH 4/7] Update Misc/NEWS.d/next/Library/2024-11-11-07-56-03.gh-issue-126639.AmVSt-.rst --- .../next/Library/2024-11-11-07-56-03.gh-issue-126639.AmVSt-.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-11-11-07-56-03.gh-issue-126639.AmVSt-.rst b/Misc/NEWS.d/next/Library/2024-11-11-07-56-03.gh-issue-126639.AmVSt-.rst index c702d09fd6071d..9c8bc4270e54c1 100644 --- a/Misc/NEWS.d/next/Library/2024-11-11-07-56-03.gh-issue-126639.AmVSt-.rst +++ b/Misc/NEWS.d/next/Library/2024-11-11-07-56-03.gh-issue-126639.AmVSt-.rst @@ -1 +1 @@ -Add :exc:`ResourceWarning` to :class:`tempfile.NamedTemporaryFile` +:class:`tempfile.NamedTemporaryFile` will now issue a :exc:`ResourceWarning` when it is finalized by the garbage collector before without being explicitly closed. From 9498d7408876e6a80d9805cdbc547d448fb54ec5 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Mon, 11 Nov 2024 15:54:46 +0000 Subject: [PATCH 5/7] Update Lib/test/test_tempfile.py --- Lib/test/test_tempfile.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index ab936e9c4f9dfc..c55075219445f7 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -1119,8 +1119,8 @@ def my_func(dir): dir = tempfile.mkdtemp() try: with self.assertWarnsRegex( - ResourceWarning, - msg=r"Implicitly cleaning up <_TemporaryFileWrapper file=.*>", + expected_warning=ResourceWarning, + expected_regex=r"Implicitly cleaning up <_TemporaryFileWrapper file=.*>", ): tmp_name = my_func(dir) support.gc_collect() From 7db921a9a7532432d036c54f2a1ca54cc284554f Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Mon, 11 Nov 2024 16:03:14 +0000 Subject: [PATCH 6/7] Update Lib/tempfile.py --- Lib/tempfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/tempfile.py b/Lib/tempfile.py index a84751b8e07469..0eb9ddeb6ac377 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -499,7 +499,7 @@ def __init__(self, file, name, delete=True, delete_on_close=True): name, delete, delete_on_close, - warn_message="Implicitly cleaning up {self!r}", + warn_message=f"Implicitly cleaning up {self!r}", ) def __repr__(self): From dcde1d6fcad23f483ee06f74eba7ead1831f9bc9 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Mon, 11 Nov 2024 16:03:48 +0000 Subject: [PATCH 7/7] Update Misc/NEWS.d/next/Library/2024-11-11-07-56-03.gh-issue-126639.AmVSt-.rst --- .../next/Library/2024-11-11-07-56-03.gh-issue-126639.AmVSt-.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-11-11-07-56-03.gh-issue-126639.AmVSt-.rst b/Misc/NEWS.d/next/Library/2024-11-11-07-56-03.gh-issue-126639.AmVSt-.rst index 9c8bc4270e54c1..0b75e5858de731 100644 --- a/Misc/NEWS.d/next/Library/2024-11-11-07-56-03.gh-issue-126639.AmVSt-.rst +++ b/Misc/NEWS.d/next/Library/2024-11-11-07-56-03.gh-issue-126639.AmVSt-.rst @@ -1 +1 @@ -:class:`tempfile.NamedTemporaryFile` will now issue a :exc:`ResourceWarning` when it is finalized by the garbage collector before without being explicitly closed. +:class:`tempfile.NamedTemporaryFile` will now issue a :exc:`ResourceWarning` when it is finalized by the garbage collector without being explicitly closed.