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
threading classes' __enter__ should return self #58324
Comments
The __enter__() methods of Lock, RLock, Semaphore and Condition in threading (and multiprocessing) all return True. This seems to contradict the documentation for the context protocol which says contextmanager.__enter__()
I don't think True qualifies as "another object related to the runtime context". It looks like an oversight caused by making __enter__() an alias for acquire(). Is it reasonable to change this for 3.3? I tripped over the issue when I tried writing with Condition() as c:
... |
IIUC returning True is not incorrect, only useless. In the stdlib I usually see “with lock:”. Can you tell what is the use case for accessing the condition object inside the context block? Does it apply only to Condition or also to *Lock and Semaphore? |
"with Lock() as lock:" doesn't make any sense - you need to share the lock with other threads or code for it be useful, which means you can't create it inline in the with statement header. Instead, you have to store it somewhere else (usually as a closure reference or a module, class or instance attribute) and then merely use it in the with statement to acquire and release it appropriately. Absent a compelling use case, I'm inclined to reject this one - when there's no specifically useful value to return from __enter__, None, True or False are all reasonable alternatives. |
I was going to do something like with Condition() as c:
Thread(target=foo, args=(c,...)).start()
c.wait_for(...) But I will agree that I don't have a compelling use case. |
Closing then. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: