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

M.-A. Lemburg mal@lemburg.com
Mon, 10 Feb 2003 12:34:48 +0100


Moore, Paul wrote:
> PEP: XXXX
> Title: Syntax for Reliable Acquisition/Release Pairs
> Version: $Revision: $
> Last-Modified: $Date: 2002/08/26 16:29:31 $
> Author: Michael Hudson <mwh@python.net>,
>         Paul Moore <gustav@morpheus.demon.co.uk>
> Status: Draft
> Type: Standards Track
> Content-Type: text/plain
> Created: 18-Dec-2002
> Python-Version: 2.4
> Post-History:
> 
> 
> Abstract
> 
>     It would be nice to have a less typing-intense way of writing:
> 
>         the_lock.acquire()
>         try:
>             ....
>         finally:
>             the_lock.release()
> 
>     This PEP proposes a piece of syntax (a 'with' block) and a
>     "small-i" interface that generalizes the above.

-1

I don't see the gain from adding new syntax for this. It is
rather likely that you want to do other cleanup stuff as well
in the finally clause. With your new syntax this would become:

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 ?).

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.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Software directly from the Source  (#1, Feb 10 2003)
 >>> Python/Zope Products & Consulting ...         http://www.egenix.com/
 >>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________
Python UK 2003, Oxford:                                     50 days left
EuroPython 2003, Charleroi, Belgium:                       134 days left