diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 61462357c7283e..b61dade16b0c7a 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -442,7 +442,7 @@ def close(self, unlink=_os.unlink): try: self.file.close() finally: - if self.delete: + if self.delete and _os.path.isfile(self.name): unlink(self.name) # Need to ensure the file is deleted on __del__ diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index 51df1ecd7d18e6..56021ad1368077 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -957,6 +957,13 @@ def test_bad_mode(self): tempfile.NamedTemporaryFile(mode=2, dir=dir) self.assertEqual(os.listdir(dir), []) + def test_if_temp_file_already_removed(self): + tmpdir = tempfile.mkdtemp() + self.addCleanup(support.rmtree, tmpdir) + with tempfile.NamedTemporaryFile(delete=True, dir=tmpdir) as fp: + os.system('rm {}'.format(fp.name)) + self.assertEqual(os.listdir(tmpdir), []) + # How to test the mode and bufsize parameters? class TestSpooledTemporaryFile(BaseTestCase): pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy