FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-29573: Fixed unlinking already removed NamedTemporaryFile. by andrewnester · Pull Request #134 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 1 addition & 1 deletion Lib/tempfile.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_tempfile.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Should add a self.addCleanup on that tmpdir—see the test above (test_bad_mode) for an example, just to make sure that if this test ever does fail that we don't have a temp dir that doesn't get cleaned up by us.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@briancurtin right, thanks! fixed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@briancurtin could you please review it again, it's fixed now

self.addCleanup(support.rmtree, tmpdir)
with tempfile.NamedTemporaryFile(delete=True, dir=tmpdir) as fp:
os.system('rm {}'.format(fp.name))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Why does this use os.system to call rm, rather than calling os.unlink? 'rm' is not exactly cross-platform. This test will also fail on Windows (and other systems where you can't delete files that are currently in use). It'll have to be skipped on those platforms.

self.assertEqual(os.listdir(tmpdir), [])

# How to test the mode and bufsize parameters?

class TestSpooledTemporaryFile(BaseTestCase):
Expand Down

Back | FazBrowse Home | New Git URL