Feature: Context manager#283
Merged
Uh oh!
There was an error while loading. Please reload this page.
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.
Hello everybody,
I am a new user of
python-canbut I decided to contribute a little to it.I have started to work on a new feature: the context manager implementation for the Bus class, and I would like to do the same with other classes that require accessing and releasing resources, like Notifier.
In this way it's possible to open and close a bus instance automatically, i.e.:
The context manager implementation is quite straight forward. What I wonder about is how to implement the exception behaviour.
In order to get an exception when requesting an operation after the bus has been closed, the instance should know about its 'openness' (note that this applies also without the context manager).
At the moment if you try to call a function after the
shutdown()call, you'll get an exception specific to one interface implementation.Relying on this, in my opinion, it's not the best since different interfaces (thus different implementations) can behave in different ways._
For example, the virtual interface doesn't release any resource after its shutdown and continues to work even after the closing call.
My idea is to use a state variable inside the instance that indicates if the bus is open or not, and use this information before trying to do any operation on the bus.
This would require to edit all the current interface implementations by adding some helper functions, something like what I did on the
virtualclass and that you can see in the PR.Let me know if this can be a good idea.