Skip to content

Commit ba103d7

Browse files
committed
[ModelicaSystem] simplify subprocess.Popen() => use subprocess.run()
1 parent f6ac6cb commit ba103d7

1 file changed

Lines changed: 4 additions & 9 deletions

File tree

OMPython/ModelicaSystem.py

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

312312
try:
313-
p = subprocess.Popen(cmd, env=my_env, stdout=subprocess.PIPE,
314-
stderr=subprocess.PIPE, cwd=self.tempdir)
315-
stdout, stderr = p.communicate()
316-
317-
stdout = stdout.decode('ascii').strip()
318-
stderr = stderr.decode('ascii').strip()
319-
if stderr:
313+
cmdres = subprocess.run(cmd, capture_output=True, text=True, env=my_env, cwd=self.tempdir)
314+
stdout = cmdres.stdout.strip()
315+
stderr = cmdres.stderr.strip()
316+
if cmdres.returncode != 0 or stderr:
320317
raise ModelicaSystemError(f"Error running command {cmd}: {stderr}")
321318
if self._verbose and stdout:
322319
logger.info("OM output for command %s:\n%s", cmd, stdout)
323-
p.wait()
324-
p.terminate()
325320
except Exception as e:
326321
raise ModelicaSystemError(f"Exception {type(e)} running command {cmd}: {e}")
327322

0 commit comments

Comments
 (0)