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

Phil Thompson phil at riverbankcomputing.co.uk
Sun Sep 17 18:51:20 BST 2006


On Saturday 16 September 2006 8:03 pm, Giovanni Bajo wrote:
> Phil Thompson <phil at riverbankcomputing.co.uk> wrote:
> >> 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?
> >
> > It's a feature of the way Python exceptions work. The locals aren't
> > garbage collected (and so the mutex isn't unlocked) while the
> > exception is being handled. Doing something like...
> >
> >     try:
> >         raise Exception
> >     finally:
> >         del locker
> >
> > ...fixes it, but I agree that that rather misses the point of
> > QMutexLocker in the first place.
>
> I guess it would be a better compromise if QMutexLocker supported the new
> with statement in Python 2.5:
>
> with locker:
>     # code
>
> and the fact was then mentioned in the documentation of PyQt.

Well, that turned out to be trivial. The attached modified script will work 
with tonight's PyQt4 snapshot.

Phil
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mutexlocker.py
Type: application/x-python
Size: 562 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20060917/f212bc34/mutexlocker.bin


More information about the PyQt mailing list