
You cannot use a NamedTempFile with an external process because it may not be flushed to disk. The safest and most portable approach is to close the file, call the other process and then unlink the file manually. Presumably this works fine on Linux, but it fails on Darwin when targeting remote-linux. See https://bugs.python.org/issue29573
24 lines
566 B
Python
24 lines
566 B
Python
"""
|
|
Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
See https://llvm.org/LICENSE.txt for license information.
|
|
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
"""
|
|
|
|
import os
|
|
import tempfile
|
|
|
|
|
|
class OnDiskTempFile:
|
|
def __init__(self, delete=True):
|
|
self.path = None
|
|
|
|
def __enter__(self):
|
|
fd, path = tempfile.mkstemp()
|
|
os.close(fd)
|
|
self.path = path
|
|
return self
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
if os.path.exists(self.path):
|
|
os.remove(self.path)
|