Repository navigation
Make IO and RawIO classes usable as context managers - #1975
Open
ptlnextdoor wants to merge 2 commits into
Open
ptlnextdoor wants to merge 2 commits into
ptlnextdoor wants to merge 2 commits into
Conversation
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().
ptlnextdoor
force-pushed
the
fix/oss-neo-175
branch
from
November 30, 2026 07:45
277d07d to
af5980d
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #175
What was broken
IO classes could not be used in a
withstatement. OnlyNixIO,NixIOFrandEDFRawIOhad__enter__and__exit__.What changed
BaseIOandBaseRawIOget__enter__,__exit__and a defaultclose().__exit__callsclose().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 awithblock. Callingclose()twice is safe.close()keep it. Readers that free handles only in their own__del__(for exampleBlackrockRawIOandAlphaOmegaRawIO) are not changed, sowithdoes not release their handles yet.open()method and lazy opening, which the issue also discusses, are out of scope.How it was tested
neo/test/iotest/test_context_manager.py. Thewithtests fail onmaster(does not support the context manager protocol) and pass now. They check thatclose()runs on normal exit and on an exception, and that a buffer-API reader closes its files once, even after a secondclose().black -l 120is clean. I ran the new test andtest_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).