Fix testFileNameFire on Windows: NamedTemporaryFile can't be reopened while open - #695
Open
lucassouzaalff-lang wants to merge 1 commit into
Open
Conversation
… while open NamedTemporaryFile defaults to delete-on-close, and on Windows the underlying file cannot be opened a second time while it is still open. MainModuleFileTest keeps self.file/self.file2 open in setUp(), but __main__.main() (via exec_module) and testFileNameModuleDuplication() both need to reopen them by path, which raises PermissionError on Windows. Fix by creating the temp files with delete=False, closing them right after writing, and removing them explicitly via addCleanup.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes half of #693:
MainModuleFileTest.testFileNameFirefails on Windows withPermissionError: [Errno 13] Permission denied.Root cause:
NamedTemporaryFilekeeps the file open with its defaultdelete=Truebehavior. On Windows the underlying file cannot be opened a second time while it's still open.setUp()keepsself.fileopen, but__main__.main()(viaexec_module) has to reopen it by path to import it, which raisesPermissionErroron Windows only (POSIX allows reopening an already-open file).Fix: create the temp files with
delete=False, close them right after writing, and remove them explicitly withself.addCleanup(os.unlink, ...). This keeps cleanup guaranteed (even on test failure) while allowing the file to be reopened by path on Windows.Verified locally on Windows 11 / Python 3.13.15:
The one remaining failure (
testArgPassing, an unescaped-regex issue) is a separate bug already fixed by #679 — not touched here to keep this PR focused.Full suite (
pytest fire/) after this change:1 failed, 260 passed(same single pre-existing failure, no new regressions).