Skip to content

Fix testFileNameFire on Windows: NamedTemporaryFile can't be reopened while open - #695

Open
lucassouzaalff-lang wants to merge 1 commit into
google:masterfrom
lucassouzaalff-lang:fix-windows-namedtemporaryfile-test
Open

Fix testFileNameFire on Windows: NamedTemporaryFile can't be reopened while open#695
lucassouzaalff-lang wants to merge 1 commit into
google:masterfrom
lucassouzaalff-lang:fix-windows-namedtemporaryfile-test

Conversation

@lucassouzaalff-lang

Copy link
Copy Markdown

Fixes half of #693: MainModuleFileTest.testFileNameFire fails on Windows with PermissionError: [Errno 13] Permission denied.

Root cause: NamedTemporaryFile keeps the file open with its default delete=True behavior. On Windows the underlying file cannot be opened a second time while it's still open. setUp() keeps self.file open, but __main__.main() (via exec_module) has to reopen it by path to import it, which raises PermissionError on 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 with self.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:

before:  fire/main_test.py  ->  2 failed, 259 passed
after:   fire/main_test.py  ->  1 failed, 260 passed

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).

… 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.
@google-cla

google-cla Bot commented Aug 18, 2026

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant