JDevlieghere wrote:

> The issue is _compiler_supports is also using a `with` statement, so 
> `__exit__` is called before the function returns. Using another `with` 
> statement would still clean up the file unless we refcounted the number of 
> `__enter__` and `__exit__` calls to only remove the file once they're 
> balanced.

Ack, I missed that we're passing the file to `_compiler_supports`, but I think 
the way that works currently is kind of bogus. If we pass in an output file, 
`_compiler_supports` shouldn't muck with it, and trust that the caller is in 
charge. 

What about:

```
from contextlib import nullcontext

def _compiler_supports(compiler, flag, source="int main() {}", 
output_file=None):
    """Test whether the compiler supports the given flag."""
    with with temp_file.OnDiskTempFile() if not output_file else nullcontext() 
as ctx:
        if platform.system() == "Darwin":
            compiler = "xcrun " + compiler
        try:
            cmd = "echo '%s' | %s %s -x c -o %s -" % (
                source,
                compiler,
                flag,
                output_file.path if output_file else ctx.path,
            )
            subprocess.check_call(cmd, shell=True)
        except subprocess.CalledProcessError:
            return False
        return True

```



https://github.com/llvm/llvm-project/pull/176463
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to