[Python-Dev] PEP 343 - Abstract Block Redux

Nick Coghlan ncoghlan at gmail.com
Sat May 14 16:20:37 CEST 2005


Paul Moore wrote:
> On 5/14/05, Fredrik Lundh <fredrik at pythonware.com> wrote:
> 
>>Nick Coghlan wrote:
>>
>>
>>>PEP 343 (like PEP 310 before it) makes it possible to define the correct
>>>resource management *once*, and then invoke it via a 'with' (or 'do')
>>>statement.
> 
> 
> This is probably the main point for me - encapsulate the try...finally
> dance in such a way that the two parts are not separated by an
> (arbitrarily long) chunk of code. I hated the equivalent dances in C
> (usually malloc/free stuff in that case) and it feels awkward that
> this is the one real ugliness of C that Python hasn't fixed for me :-)
> 
> 
>>sure, but even if you look at both the application code *and*
>>the resource management, there are no clues that the "with"
>>statement is really just a masked "try/finally" statement.  just
>>look at the generator example:
>>
>>    acquire
>>    yield
>>    release
>>
>>what in this snippet tells you that the "release" part will run even if
>>the external block raises an exception?
> 
> 
> I agree with this point, though. What I liked about the original PEP
> 340 was the fact that the generator was a template, with "yield"
> acting as a "put the block here" placeholder.

I also think the "generator as statement template" works much better if the 
__exit__ method is able to inject the exception into the generator frame, rather 
than always calling next().

Maybe PEP 343 should drop any suggestion of using generators to define these 
things, and focus on the PEP 310 style templates.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.blogspot.com


More information about the Python-Dev mailing list