git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] git-p4: close temporary file before removing

From
AAndrey <ahippo@yandex.ru>
Date
Jul 31, 2019, 01:48 UTC
Message-ID
<1955471564537683@vla1-53bffb0b04ed.qloud-c.yandex.net>
In-Reply-To
<BL0PR1901MB209738ADDF9D931253E8C317FFDC0@BL0PR1901MB2097.namprd19.prod.outlook.com>
30.07.2019, 13:37, "Philip McGraw" <philip.mcgraw@bentley.com>:
Show 26 quoted lines
> python os.remove() throws exceptions on Windows platform when attempting
> to remove file while it is still open. Need to grab filename while file open,
> close file handle, then remove by name. Apparently other platforms are more
> permissive of removing files while busy.
> reference: https://docs.python.org/3/library/os.html#os.remove
> ---
>  git-p4.py | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/git-p4.py b/git-p4.py
> index c71a6832e2..6b9d2a8317 100755
> --- a/git-p4.py
> +++ b/git-p4.py
> @@ -1161,12 +1161,14 @@ def exceedsLargeFileThreshold(self, relPath, contents):
>                  return False
>              contentTempFile = self.generateTempFile(contents)
>              compressedContentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=False)
> + compressedContentFileName = compressedContentFile.name
>              zf = zipfile.ZipFile(compressedContentFile.name, mode='w')
>              zf.write(contentTempFile, compress_type=zipfile.ZIP_DEFLATED)
>              zf.close()
>              compressedContentsSize = zf.infolist()[0].compress_size
>              os.remove(contentTempFile)
> - os.remove(compressedContentFile.name)
> + compressedContentFile.close()
> + os.remove(compressedContentFileName)

I'm not sure why NamedTemporaryFile() is called with delete=False above, but it appears to me that it can have delete=True instead, so that there is no need to call os.remove() explicitly and thus worry about remove vs close ordering at all.

Show 5 quoted lines
>              if compressedContentsSize > gitConfigInt('git-p4.largeFileCompressedThreshold'):
>                  return True
>          return False
> --
> 2.21.0.windows.1

Thank you, Andrey.

Previous: Philip McGrawNext: Philip McGraw
Message 2 of 8 in “git-p4: close temporary file before removing”
  1. git-p4: close temporary file before removingPhilip McGraw, Jul 30, 2019
  2. AndreyJul 31, 2019
  3. Philip McGrawJul 31, 2019
  4. AndreyJul 31, 2019
  5. Philip McGrawJul 31, 2019
  6. AndreyAug 1, 2019
  7. Philip McGrawAug 1, 2019
  8. AndreyAug 2, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.