an error in python lib?

W

Wenhua Zhao

Hi list,

I just noticed that in /usr/lib/python2.7/threading.py

class _Condition(_Verbose):
...
def _is_owned(self):
# Return True if lock is owned by current_thread.
# This method is called only if __lock doesn't have
_is_owned().
if self.__lock.acquire(0):
self.__lock.release()
return False
else:
return True

The return values seem to be wrong. They should be swapped:

def _is_owned(self):
if self.__lock.acquire(0):
self.__lock.release()
return True
else:
return False

Or I understood it wrong here?

Thanks,
Wenhua
 
U

Ulrich Eckhardt

Am 10.10.2012 02:32, schrieb Wenhua Zhao:
I just noticed that in /usr/lib/python2.7/threading.py

class _Condition(_Verbose):
...
def _is_owned(self):
# Return True if lock is owned by current_thread.
# This method is called only if __lock doesn't have
# _is_owned().
if self.__lock.acquire(0):
self.__lock.release()
return False
else:
return True

The return values seem to be wrong. They should be swapped:

def _is_owned(self):
if self.__lock.acquire(0):
self.__lock.release()
return True
else:
return False

Or I understood it wrong here?

I think you are correct, but there is one thing that I would audit
first: The whole code there seems to use integers in places where a
boolean would be appropriate, like e.g. the 'blocking' parameter to
acquire(). I wouldn't be surprised to find the interpretation of "0
means no error" in some places there, so that a False translates to 0
and then to "OK, I have the lock".

Also, assuming an underlying implementation where a nonblocking
acquire() could still newly acquire an uncontended lock, that
implementation would release the acquired lock and still return "yes I'm
holding the lock", which would be dead wrong. It must verify if the lock
count is at least 2 after acquiring the lock.

Uli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top