Skip to content

Commit d8ff798

Browse files
committed
[ModelicaSystem] simplify subprocess.Popen() => use subprocess.run()
1 parent 0b63a2b commit d8ff798

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

OMPython/ModelicaSystem.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,13 @@ def _run_cmd(self, cmd: list):
273273
my_env = None
274274

275275
try:
276-
p = subprocess.Popen(cmd, env=my_env, stdout=subprocess.PIPE,
277-
stderr=subprocess.PIPE, cwd=self.tempdir)
278-
stdout, stderr = p.communicate()
279-
280-
stdout = stdout.decode('ascii').strip()
281-
stderr = stderr.decode('ascii').strip()
282-
if stderr:
276+
cmdres = subprocess.run(cmd, capture_output=True, text=True, env=my_env, cwd=self.tempdir)
277+
stdout = cmdres.stdout.strip()
278+
stderr = cmdres.stderr.strip()
279+
if cmdres.returncode != 0 or stderr:
283280
raise ModelicaSystemError(f"Error running command {cmd}: {stderr}")
284281
if self._verbose and stdout:
285282
logger.info("OM output for command %s:\n%s", cmd, stdout)
286-
p.wait()
287-
p.terminate()
288283
except Exception as e:
289284
raise ModelicaSystemError(f"Exception {type(e)} running command {cmd}: {e}")
290285

0 commit comments

Comments
 (0)