Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/symident/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
5 changes: 4 additions & 1 deletion src/symident/detection/ioproj.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(":"))
Expand Down
40 changes: 22 additions & 18 deletions src/symident/detection/work.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
19 changes: 19 additions & 0 deletions tests/test_discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading