Repository navigation
Make IO and RawIO classes usable as context managers - #1975
Open
ptlnextdoor wants to merge 1 commit into
Open
ptlnextdoor wants to merge 1 commit into
ptlnextdoor wants to merge 1 commit into
Conversation
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
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.
Fixes #175
What was broken
IO classes could not be used in a
withstatement. OnlyNixIO,NixIOFrandEDFRawIOhad__enter__and__exit__.What changed
BaseIOandBaseRawIOnow have__enter__,__exit__and a defaultclose()that does nothing.__exit__callsclose(). IO classes that hold file handles already overrideclose()(or can do so), sowith SomeIO(...) as io:now closes them on exit. Files are still opened in the constructor. No per-IO changes. Classes with their own__enter__/__exit__keep them.How it was tested
neo/test/iotest/test_context_manager.py. It fails onmaster(does not support the context manager protocol) and passes now. It checks thatclose()runs on normal exit and when an exception is raised.ruffandblack -l 120are clean on the changed files.test_exampleio.pyandtest_examplerawio.pycould not run here, because theephy_testing_datafiles are missing. I did not run the full suite.AI disclosure: this PR was prepared with AI assistance (Claude, Anthropic).