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().

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.

3 participants