[Python-Dev] Pre-PEP: with syntax (was: New syntax threads -- summary wanted) (original) (raw)

Bernhard Herzog bh@intevation.de
10 Feb 2003 13:53:04 +0100


"M.-A. Lemburg" <mal@lemburg.com> writes:

try: with mylock: ... finally: ... other cleanup code ...

The new syntax saves you one line and obscures the code rather than clarifying what is happening (who knows what mylock does in its enter/exit method ?).

If you come across an unfamiliar function or type you look it up in the documentation or the source. I don't see the problem here.

The original argument for this was not to save you typing, it was built on the rather unlikely case where the process receives a signal or MemoryError causing an exception while executing the byte codes after calling .acquire() and before entering the try-block.

However, that's not hard to deal with either because all you need to do is place the .acquire() call inside the try-block and then adding a check whether you have to release the lock in the finally-clause.

That just moves the race condition to a different place. If the acquire call is inside the try block the place of the race condition is between the acquire call and setting a flag or in the finally clause between checking a flag and actually releasing the lock.

Bernhard

-- Intevation GmbH http://intevation.de/ Sketch http://sketch.sourceforge.net/ MapIt! http://www.mapit.de/