Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix usages of NamedTemporaryFiles on Windows #486

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

alchzh
Copy link
Contributor

@alchzh alchzh commented Jul 22, 2024

Fixed usages of NamedTemporaryFiles on Windows to avoid permissions errors.

Please describe the changes in your request.

A common pattern in the OFRAK codebase is opening a tempfile.NamedTemporaryFile, writing some data to it, calling flush(), then calling an external tool on the temporary file. Unfortunately, this always causes a permissions error on Windows because two processes can't have the same file open at the same time. The solution is to close the temporary file, without deleting it, before handing it off to the external tool. This behavior wasn't supported by tempfile.NamedTemporaryFile before Python 3.12 introduced the delete_on_close parameter (python/cpython#58451).

This PR

  • copies tempfile.py from Python 3.12 to ofrak/tempfile.py and sets delete_on_close to default to False in NamedTemporaryFile.
  • changes imports of standard library tempfile to ofrak.tempfile
  • changes .flush() to .close() inside with NamedTemporaryFile() blocks
  • safely reopens files for reading when required
  • Changes ZstdPacker and ZstdUnpacker to use stdin/stdout directly instead of writing to temporary files

Anyone you think should look at this, specifically?
@whyitfor

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