Skip to content
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

Closed
sbt mannequin opened this issue Feb 24, 2012 · 5 comments
Closed

threading classes' __enter__ should return self #58324

sbt mannequin opened this issue Feb 24, 2012 · 5 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@sbt
Copy link
Mannequin

sbt mannequin commented Feb 24, 2012

BPO 14116
Nosy @ncoghlan, @pitrou, @merwok

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:

assignee = None
closed_at = <Date 2012-03-31.23:33:18.027>
created_at = <Date 2012-02-24.21:03:00.517>
labels = ['type-feature', 'library']
title = "threading classes' __enter__ should return self"
updated_at = <Date 2012-03-31.23:33:18.026>
user = 'https://bugs.python.org/sbt'

bugs.python.org fields:

activity = <Date 2012-03-31.23:33:18.026>
actor = 'pitrou'
assignee = 'none'
closed = True
closed_date = <Date 2012-03-31.23:33:18.027>
closer = 'pitrou'
components = ['Library (Lib)']
creation = <Date 2012-02-24.21:03:00.517>
creator = 'sbt'
dependencies = []
files = []
hgrepos = []
issue_num = 14116
keywords = []
message_count = 5.0
messages = ['154161', '154196', '154225', '154361', '157242']
nosy_count = 4.0
nosy_names = ['ncoghlan', 'pitrou', 'eric.araujo', 'sbt']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue14116'
versions = ['Python 3.3']

@sbt
Copy link
Mannequin Author

sbt mannequin commented Feb 24, 2012

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:
    ...

@sbt sbt mannequin added type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir labels Feb 24, 2012
@merwok
Copy link
Member

merwok commented Feb 25, 2012

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?

@ncoghlan
Copy link
Contributor

"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.

@sbt
Copy link
Mannequin Author

sbt mannequin commented Feb 26, 2012

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.

@birkenfeld birkenfeld added type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels Mar 9, 2012
@merwok merwok changed the title Lock.__enter__() method returns True instead of self Condition.__enter__ should return self Mar 11, 2012
@birkenfeld birkenfeld changed the title Condition.__enter__ should return self threading classes' __enter__ should return self Mar 11, 2012
@pitrou
Copy link
Member

pitrou commented Mar 31, 2012

Closing then.

@pitrou pitrou closed this as completed Mar 31, 2012
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants