[Python-Dev] Merging PEP 310 and PEP 340-redux?

Nick Coghlan ncoghlan at gmail.com
Thu May 12 12:31:44 CEST 2005


Steven Bethard wrote:
> On 5/11/05, Nick Coghlan <ncoghlan at gmail.com> wrote:
> 
>>The gist is that the alternative is to require an __exit__() method to raise
>>TerminateBlock in order to suppress an exception.
> 
> So I didn't see any examples that really needed TerminateBlock to
> suppress an exception.

Yeah, I figured out a tidier way to handle it after reading Phillip's message 
earlier today. My idea is similar to your second solution, but with an early 
exit via break, continue or return still indicated to the __exit__() method via 
TerminateBlock so that examples like transaction() continue to do the right thing:

     the_stmt = EXPR1
     stmt_enter = getattr(the_stmt, "__enter__", None)
     stmt_exit = getattr(the_stmt, "__exit__", None)
     if stmt_enter is None or stmt_exit is None:
         raise TypeError("User defined statement template required")

     terminate = True
     VAR1 = stmt_enter() # Omit 'VAR1 =' if no 'as' clause
     try:
         try:
             BLOCK1
         except TerminateBlock:
             raise # Disallow suppression of TerminateBlock
         except:
             terminate = False
             if not stmt_exit(*sys.exc_info()):
                 raise
         else:
             terminate = False
             stmt_exit()
     finally:
         if terminate:
             try:
                 stmt_exit(TerminateBlock, None, None)
             except TerminateBlock:
                 pass

Version 1.5 uses these updated semantics, and the suggested generator __exit__() 
method semantics are adjusted appropriately. I've also added a paragraph in Open 
Issues about removing the ability to suppress exceptions as Guido has suggested. 
However, I'm hoping his objections are based on the assorted horrible mechanisms 
I used in versions before this one - he is quite right that forcing every 
__exit__() method to reraise exceptions was a rather ugly wart.

The new version also fixes a typo in the auto_retry example that a couple of 
people pointed out, and adds a non-exception related example from Arnold deVos.

The URL is the same as before:
http://members.iinet.net.au/~ncoghlan/public/pep-3XX.html

Cheers,
Nick.

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


More information about the Python-Dev mailing list