an error in python lib?

Discussion in 'Python' started by Wenhua Zhao, Oct 10, 2012.

  1. Wenhua Zhao

    Wenhua Zhao Guest

    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
     
    Wenhua Zhao, Oct 10, 2012
    #1
    1. Advertising

  2. 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
     
    Ulrich Eckhardt, Oct 10, 2012
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Yong
    Replies:
    0
    Views:
    593
    James Yong
    Sep 12, 2005
  2. Praetorian

    Need odbc32.lib odbccp32.lib

    Praetorian, Apr 20, 2006, in forum: C++
    Replies:
    1
    Views:
    1,455
    mlimber
    Apr 20, 2006
  3. Durduran
    Replies:
    10
    Views:
    582
    Durduran
    Jul 30, 2007
  4. Replies:
    3
    Views:
    2,836
  5. est
    Replies:
    1
    Views:
    663
    Diez B. Roggisch
    Feb 16, 2008
Loading...

Share This Page