From 3af6625242766c7aa6e01100988dd4247baad470 Mon Sep 17 00:00:00 2001 From: Kazuya O'moto Date: Fri, 6 Sep 2024 00:42:10 +0900 Subject: [PATCH] Don't warn unclosed file with subprocess.Popen https://docs.python.org/3/library/subprocess.html#popen-constructor > Changed in version 3.2: Added context manager support. > Changed in version 3.6: Popen destructor now emits a ResourceWarning warning if the child process is still running. --- buildtools/config.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/buildtools/config.py b/buildtools/config.py index d84544ea..2c1fed32 100644 --- a/buildtools/config.py +++ b/buildtools/config.py @@ -907,29 +907,29 @@ def runcmd(cmd, getOutput=False, echoCmd=True, fatal=True, onError=None): otherKwArgs = dict(stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - sp = subprocess.Popen(cmd, shell=True, env=os.environ, **otherKwArgs) + with subprocess.Popen(cmd, shell=True, env=os.environ, **otherKwArgs) as sp: - output = None - if getOutput: - outputEncoding = 'cp1252' if sys.platform == 'win32' else 'utf-8' - output = sp.stdout.read() - if sys.version_info > (3,): - output = output.decode(outputEncoding) - output = output.rstrip() - - rval = sp.wait() - if rval: - # Failed! - #raise subprocess.CalledProcessError(rval, cmd) - print("Command '%s' failed with exit code %d." % (cmd, rval)) + output = None if getOutput: - print(output) - if onError is not None: - onError() - if fatal: - sys.exit(rval) + outputEncoding = 'cp1252' if sys.platform == 'win32' else 'utf-8' + output = sp.stdout.read() + if sys.version_info > (3,): + output = output.decode(outputEncoding) + output = output.rstrip() - return output + rval = sp.wait() + if rval: + # Failed! + #raise subprocess.CalledProcessError(rval, cmd) + print("Command '%s' failed with exit code %d." % (cmd, rval)) + if getOutput: + print(output) + if onError is not None: + onError() + if fatal: + sys.exit(rval) + + return output def myExecfile(filename, ns):