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__() Enter the runtime context and return either this object or another object related to the runtime context. The value returned by this method is bound to the identifier in the as clause of with statements using this context manager. ... 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.
> 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? 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.