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: "lock.__exit__ == lock.release" should be False
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: dghjfrdj, r.david.murray
Priority: normal Keywords:

Created on 2011-02-11 17:40 by dghjfrdj, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (4)
msg128409 - (view) Author: O.C. (dghjfrdj) Date: 2011-02-11 17:40
Hello,

if 'lock' is a threading.Lock, the functions lock.__exit__() and lock.release() are different. The former takes 3 arguments, while the latter takes no argument. However, "lock.__exit__ == lock.release" returns True. Shouldn't it return False ?

Best regards,

O.C.

Details:
In [20]: lock=threading.Lock()
In [21]: lock.acquire()
In [22]: lock.__exit__(None,None,None)
In [23]: lock.locked()
Out[23]: False
In [24]: lock.acquire()
In [25]: lock.release(None,None,None)
TypeError: release() takes no arguments (3 given)
In [26]: lock.__exit__==lock.release
Out[26]: True
msg128414 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-02-11 18:15
Well, under the (C) hood, it is in fact the same method, it just takes a variable number of arguments (and ignores them, in the __exit__ case).  The fact that the arguments are rejected in the 'release' case is because of how the C function is defined as a Python method using the C API.  So the result of the boolean test is more accurate than a False report would be, although not precise.  

I don't *think* there is any bug to fix here.  Do you have a use case that this impacts?
msg128425 - (view) Author: O.C. (dghjfrdj) Date: 2011-02-11 19:51
> Do you have a use case that this impacts?

No, I can live with it. It was rather a point about clarity and consistency. For example, the difference between Lock and RLock:
lock.__exit__==lock.release    -> True
rlock.__exit__==rlock.release  -> False

We came on that topic when using 'with' statements with exceptions and 'returns'. I wanted to check and understand the details, and I was confused by the fact that:
- three arguments will be given to lock.__exit__()
- lock.__exit__ is "the same" as lock.release
- lock.release takes no argument

These propositions are all true, but seem contradictory. The meaning of "the same" is not obvious in this case.
msg128431 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-02-11 21:57
Yeah, sometimes you just have to read the source.

Previous to Python3.2, RLock was implemented in Python, and the two methods are actually different methods there.  In Python3.2, rlock.release == rlock.__exit__ is True.
History
Date User Action Args
2022-04-11 14:57:12adminsetgithub: 55403
2011-02-11 21:57:51r.david.murraysetstatus: open -> closed
nosy: r.david.murray, dghjfrdj
messages: + msg128431

resolution: not a bug
stage: resolved
2011-02-11 19:51:50dghjfrdjsetnosy: r.david.murray, dghjfrdj
messages: + msg128425
2011-02-11 18:15:31r.david.murraysetnosy: + r.david.murray

messages: + msg128414
versions: + Python 2.7, Python 3.2, Python 3.3
2011-02-11 17:40:57dghjfrdjcreate