homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: threading classes' __enter__ should return self
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: eric.araujo, ncoghlan, pitrou, sbt
Priority: normal Keywords:

Created on 2012年02月24日 21:03 by sbt, last changed 2022年04月11日 14:57 by admin. This issue is now closed.

Messages (5)
msg154161 - (view) Author: Richard Oudkerk (sbt) * (Python committer) Date: 2012年02月24日 21:03
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:
 ...
msg154196 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012年02月25日 07:22
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?
msg154225 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2012年02月25日 09:49
"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.
msg154361 - (view) Author: Richard Oudkerk (sbt) * (Python committer) Date: 2012年02月26日 15:39
> 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.
msg157242 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012年03月31日 23:33
Closing then.
History
Date User Action Args
2022年04月11日 14:57:27adminsetgithub: 58324
2012年03月31日 23:33:18pitrousetstatus: open -> closed
resolution: rejected
messages: + msg157242

stage: resolved
2012年03月11日 13:50:52georg.brandlsettitle: Condition.__enter__ should return self -> threading classes' __enter__ should return self
2012年03月11日 10:13:45eric.araujosettitle: Lock.__enter__() method returns True instead of self -> Condition.__enter__ should return self
2012年03月09日 09:38:30georg.brandlsettype: behavior -> enhancement
2012年02月26日 15:39:56sbtsetmessages: + msg154361
2012年02月25日 09:49:54ncoghlansetmessages: + msg154225
2012年02月25日 07:22:57eric.araujosetnosy: + eric.araujo, ncoghlan, pitrou
messages: + msg154196
2012年02月24日 21:03:00sbtcreate

AltStyle によって変換されたページ (->オリジナル) /