Fix testFileNameFire on Windows: NamedTemporaryFile can't be reopened while open - #695
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. |
|
Verified on Windows 11 Home (build 26200), Python 3.13.13 and 3.14.7. The build matrix is macos + ubuntu only and the workflow here is still awaiting approval, so nothing has exercised this yet.
Non-blocking note: the new comment says |
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).