diff --git a/NEWS.md b/NEWS.md index 9378dfc..c8a3b9e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,8 @@ +# symident 0.18.1 + +* `discrete()` closes the output pipes of every msolve call and the output file of msolve in the + input-output projections; Python no longer warns about unclosed files. + # symident 0.18.0 ## Global analysis diff --git a/pyproject.toml b/pyproject.toml index 1f8158e..29d01d2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "scikit_build_core.build" [project] name = "symident" -version = "0.18.0" +version = "0.18.1" description = "Structural identifiability of ODE models by Lie symmetries over finite fields" readme = "README.md" requires-python = ">=3.10" diff --git a/src/symident/__init__.py b/src/symident/__init__.py index 4d4a081..aba44a1 100644 --- a/src/symident/__init__.py +++ b/src/symident/__init__.py @@ -8,4 +8,4 @@ __all__ = ["Model", "Symmetries", "Symmetry", "Reduction", "detect", "reduce", "read_petab", "PEtabError", "reconst_control", "install_msolve", "switches"] -__version__ = "0.18.0" +__version__ = "0.18.1" diff --git a/src/symident/detection/ioproj.py b/src/symident/detection/ioproj.py index 99a4be2..d56d045 100644 --- a/src/symident/detection/ioproj.py +++ b/src/symident/detection/ioproj.py @@ -479,7 +479,10 @@ def text(d): with work.local_caps(msolve_entries=work.cap("msolve_entries") / 10): r = work.run_msolve([binary, "-f", fi, "-o", fo, "-P", "1", "-t", str(_threads())]) work.tick(int(r.entries)) - txt = open(fo).read().strip() if os.path.exists(fo) else "" + txt = "" + if os.path.exists(fo): + with open(fo) as fh: + txt = fh.read().strip() if r.returncode != 0 or not txt: return None data = ast.literal_eval(txt.rstrip(":")) diff --git a/src/symident/detection/work.py b/src/symident/detection/work.py index 2330e3c..5765f89 100644 --- a/src/symident/detection/work.py +++ b/src/symident/detection/work.py @@ -203,27 +203,31 @@ def run_msolve(cmd, text=True): result has returncode 1 and `capped` the reason. The count of a finished call decides as well, so the outcome does not depend on when the output is read.""" most = cap("msolve_entries") - proc = subprocess.Popen(list(cmd) + ["-v", "2"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, - preexec_fn=_limiter(MSOLVE_MEMORY)) out = [] - reader = threading.Thread(target=lambda: out.append(proc.stdout.read())) - reader.start() total, buf, done = 0, b"", 0 - fd = proc.stderr.fileno() - while True: - chunk = os.read(fd, 1 << 16) - if not chunk: - break - buf += chunk - for m in _MATRIX.finditer(buf, done): - total += int(m.group(1)) * int(m.group(2)) * float(m.group(3)) / 100 - done = m.end() - if total > most: + # leaving the block closes both pipes and waits for the process + with subprocess.Popen(list(cmd) + ["-v", "2"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, + preexec_fn=_limiter(MSOLVE_MEMORY)) as proc: + reader = threading.Thread(target=lambda: out.append(proc.stdout.read())) + reader.start() + try: + fd = proc.stderr.fileno() + while True: + chunk = os.read(fd, 1 << 16) + if not chunk: + break + buf += chunk + for m in _MATRIX.finditer(buf, done): + total += int(m.group(1)) * int(m.group(2)) * float(m.group(3)) / 100 + done = m.end() + if total > most: + proc.kill() + break + except BaseException: proc.kill() - break - proc.stderr.close() - proc.wait() - reader.join() + raise + finally: + reader.join() stdout, stderr = out[0] if out else b"", buf if text: stdout, stderr = stdout.decode(errors="replace"), stderr.decode(errors="replace") diff --git a/tests/test_discrete.py b/tests/test_discrete.py index 5d81edb..bc81f52 100644 --- a/tests/test_discrete.py +++ b/tests/test_discrete.py @@ -712,6 +712,25 @@ def test_a_process_past_its_address_space_fails_and_not_the_one_that_waits(): assert r.returncode != 0 +def test_run_msolve_closes_the_pipes_of_the_process(monkeypatch): + import subprocess + import sys + + from symident.detection import work + procs = [] + + class Recorded(subprocess.Popen): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + procs.append(self) + + monkeypatch.setattr(work.subprocess, "Popen", Recorded) + code = "import sys; print('[0]'); sys.stderr.write('3 x 4 50.0%\\n')" + r = work.run_msolve([sys.executable, "-c", code]) + assert r.returncode == 0 and r.stdout.strip() == "[0]" and r.entries == 6 + assert procs and procs[0].stdout.closed and procs[0].stderr.closed + + def test_the_symbols_of_the_package_hold_no_clock(): import pathlib import re