an error in python lib?

Ulrich Eckhardt ulrich.eckhardt at dominolaser.com
Wed Oct 10 08:18:31 EDT 2012


Am 10.10.2012 03:16, schrieb MRAB:
> On 2012-10-10 01:32, Wenhua Zhao wrote:
>> 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?
>>
> The .acquire method will return True if the attempt to acquire has been
> successful. This can occur only if it is not currently owned.

The comment clearly states "owned by current thread", not "owned by any 
thread". The latter would also be useless, as that can change 
concurrently at any time when owned by a different thread, so making 
decisions on this state is futile. Also, acquire() can also return true 
when locking recursively, at least that's how I read the sources.

I think that this is really a bug, but it doesn't surface often because 
the built-in lock has its own _is_owned() function which is used instead 
of this flawed logic.

Uli




More information about the Python-list mailing list