-
Notifications
You must be signed in to change notification settings - Fork 645
Feature: Context manager #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Hm, very promising. :) Maybe #235 can help you with the states. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't worry about interface specific behaviour, since that is to be expected on all operations.
can/bus.py
Outdated
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
for t in self._tx_threads: | ||
t.stop() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. It could even be moved to the .shutdown()
method in my opinion.
@@ -169,5 +169,103 @@ def test_basics(self): | |||
notifier.stop() | |||
|
|||
|
|||
class Back2BackTestCaseWithContext(unittest.TestCase): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think this is needed. The other test should suffice.
@edobez Any updates on this? |
Uhm, I guess this method should not assume that that attribute exists. It is not documented anywhere! And shouldn't it call |
I removed the |
I will merge it and then combine |
Hello everybody,
I am a new user of
python-can
but 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
virtual
class and that you can see in the PR.Let me know if this can be a good idea.