Skip to content
2 changes: 2 additions & 0 deletions package/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ Chronological list of authors
- Sumit Saraswat
- Carlos Vega
- Erik Fransson
- Srikar025


External code
-------------
Expand Down
2 changes: 2 additions & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ The rules for this file:
* 2.12.0

Fixes
* Fixed `ProgressBar` closing the underlying trajectory reader when a loop
is interrupted with progress display disabled (Issue #5540)
* reduced resident memory (RSS) of the NCDF (Amber) file reader when used with
`mmap=True` (default for files on disk): on Linux, it no longer grows the RSS
towards the size of the whole trajectory file. Not changed on macOS (where
Expand Down
9 changes: 9 additions & 0 deletions package/MDAnalysis/lib/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,12 @@ def __init__(self, *args, **kwargs):
# disable should take precedence over verbose if both are set
kwargs["disable"] = kwargs.pop("disable", disable)
super(ProgressBar, self).__init__(*args, **kwargs)

def __iter__(self):
"""Iterate without closing the underlying iterable when disabled."""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a comment referencing issue and PR and state briefly that we're working around a change in tqdm (and mention the version of the change). When looking at this code, readers should understand why it's necessary.

if self.disable:
for obj in self.iterable:
yield obj
return

yield from super().__iter__()
22 changes: 22 additions & 0 deletions testsuite/MDAnalysisTests/lib/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,25 @@ def test_verbose_disable(self, capsys):
expected = ""
actual = err.strip().split("\r")[-1]
assert actual == expected

def test_trajectory_remains_open_when_disabled(self, monkeypatch):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is fine but it is a bit synthetic. In MAICoS we also tested an Analysis workflow that breaks. https://github.com/atomistic-lab/maicos/pull/614/changes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, something like your test_aborted_run_keeps_trajectory_open would be good.

"""Ensure early iterator closure leaves the trajectory accessible."""
import MDAnalysis as mda
from MDAnalysis.tests.datafiles import PSF, DCD
Comment on lines +57 to +58

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imports at top

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is pretty common for LLMs to scope imports in (test) functions when that isn't necessary. Do we really need to scope these imports locally like this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just made a similar comment — we collect imports at the top unless there's a very good reason to do something else.


def iter_with_yield_from(self):
yield from self.iterable

tqdm_class = ProgressBar.__mro__[1]
monkeypatch.setattr(tqdm_class, "__iter__", iter_with_yield_from)
Comment on lines +63 to +64

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work?

Suggested change
tqdm_class = ProgressBar.__mro__[1]
monkeypatch.setattr(tqdm_class, "__iter__", iter_with_yield_from)
import tqdm.std
monkeypatch.setattr(tqdm.std.tqdm, "__iter__", iter_with_yield_from)


u = mda.Universe(PSF, DCD)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make the universe a fixture so that you can re-use it for a test_aborted_run_keeps_trajectory_open, too.


progress = ProgressBar(u.trajectory, verbose=False)
iterator = iter(progress)

next(iterator)
iterator.close()

assert u.trajectory[0].frame == 0
u.trajectory.close()
Loading