ofrak icon indicating copy to clipboard operation
ofrak copied to clipboard

Fix usages of NamedTemporaryFiles on Windows

Open alchzh opened this issue 1 year ago • 0 comments

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

alchzh avatar Jul 22 '24 14:07 alchzh