[PyKDE] QMutexLocker doesn't unlock the mutex if an exception is raised

Lukáš Lalinský lalinsky at gmail.com
Sat Sep 16 09:07:38 BST 2006


Hi,

the Qt4/PyQt4 manuals specifically talk about using QMutexLocker to simplify
exception handling in functions. However, in PyQt the mutex doesn't get unlocked
if an exception is raised in the function code. I was wondering whether this is
a bug or a technical limitation? If the latter, then perhaps it could be
mentioned in the PyQt manual?

Regards,
   Lukáš
-------------- next part --------------
from PyQt4 import QtCore

mutex = QtCore.QMutex()

def a1():
    locker = QtCore.QMutexLocker(mutex)
#    raise Exception

def a2():
    mutex.lock()
    try:
        raise Exception
    finally:
        mutex.unlock()

print "QMutexLocker"
try:
    a1()
except:
    if mutex.tryLock():
        print " - OK"
    else:
        print " - Locked!"
    mutex.unlock()

print "Manual locking"
try:
    a2()
except:
    if mutex.tryLock():
        print " - OK"
    else:
        print " - Locked!"
    mutex.unlock()


More information about the PyQt mailing list