Skip to content

Make IO and RawIO classes usable as context managers - #1975

Open
ptlnextdoor wants to merge 2 commits into
NeuralEnsemble:masterfrom
ptlnextdoor:fix/oss-neo-175
Open

ptlnextdoor wants to merge 2 commits into
NeuralEnsemble:masterfrom
ptlnextdoor:fix/oss-neo-175

Conversation

@ptlnextdoor

@ptlnextdoor ptlnextdoor commented Nov 17, 2026 •

Copy link
Copy Markdown

Part of #175

What was broken

IO classes could not be used in a with statement. Only NixIO, NixIOFr and EDFRawIO had __enter__ and __exit__.

What changed

  • BaseIO and BaseRawIO get __enter__, __exit__ and a default close(). __exit__ calls close().
  • BaseRawWithBufferApiIO.close() now closes the memmap and HDF5 buffers. __del__ calls it, so the cleanup is the same as before and can also run at the end of a with block. Calling close() twice is safe.
  • Readers that already define close() keep it. Readers that free handles only in their own __del__ (for example BlackrockRawIO and AlphaOmegaRawIO) are not changed, so with does not release their handles yet.
  • Files are still opened in the constructor. An open() method and lazy opening, which the issue also discusses, are out of scope.

How it was tested

  • New neo/test/iotest/test_context_manager.py. The with tests fail on master (does not support the context manager protocol) and pass now. They check that close() runs on normal exit and on an exception, and that a buffer-API reader closes its files once, even after a second close().
  • black -l 120 is clean. I ran the new test and test_examplerawio.py. The ExampleIO tests need the ephy_testing_data files, which are not available here.

AI disclosure: this PR was prepared with AI assistance (Claude, Anthropic).

Copilot AI balanced review requested due to automatic review settings November 17, 2026 05:13

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@zm711

zm711 commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Hey @ptlnextdoor thanks for the contribution. I think this type of PR will likely have to be discussed at our next maintenance meeting as we haven't discussed this in a quite a while (as you can see from the age of the issue linked). We will provide feedback once we decide if we would want to go forward with this possibility.

Add __enter__, __exit__ and a default no-op close() to BaseIO and BaseRawIO,
so that 'with SomeIO(...) as io:' calls close() on exit.

Fixes NeuralEnsemble#175
BaseRawWithBufferApiIO.close() now closes the memmap and HDF5 buffers, so
leaving a with block frees them. __del__ calls close().
Comment thread neo/rawio/baserawio.py
def __exit__(self, exc_type, exc_value, traceback):
self.close()

def close(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why are you making this public?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

__exit__ has to call something, and the issue asks for with SomeIO(...) as io: to close the file. close is also the name existing IOs already use publicly (NixIO, NWBIO, MedIO, ElphyIO, BrainwareSrcIO), so the base class now declares it as a no-op that those classes override. The only other change is that BaseRawWithBufferApiIO.__del__ now calls the new public close(). If you prefer a private hook such as _close, say so and I will change it.

@ptlnextdoor

Copy link
Copy Markdown
Author

About the failing call-iotests (3.14, 2.5.4) job: it fails in setup with FileNotFoundError for ephy_testing_data/Generated0_NixIO.h5. The same error appears on master, for example in https://github.com/NeuralEnsemble/python-neo/actions/runs/47379783144 (job 3.14, 2.3.3), so this PR does not cause it. Thanks @zm711, I will wait for the maintenance meeting.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants