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

Nick Coghlan ncoghlan at gmail.com
Fri May 13 19:08:39 CEST 2005


Phillip J. Eby wrote:
> At 08:41 AM 5/13/2005 -0700, Guido van Rossum wrote:
> 
>>The 'oke' argument is so that the author of transactional() can decide
>>what to do with a non-local goto: commit, rollback or hit the author
>>over the head with a big stick.
<snip>
> * Automatically roll back partially-done work in case of exception, and/or 
> "roll forward" completed work (try/except/else, used for "transaction" 
> scenarios)

Doing transactions with try/except/else is not quite correct, since using any of 
the three non-local goto's actually executes neither the commit nor the rollback 
(of course, this is where Guido's stick comment comes into play. . .).

However, I'm fine with declaring that, from the perspective of a statement 
template, 'return', 'break' and 'continue' are all 'non-exceptional exits', and 
so templates like transaction() are expected to treat them as such.

Picking one way and enforcing it by restricting the information seen by 
__exit__() also seems to be a much better option than allowing the possibility of:

   do bobs.transaction():
       break
       # Triggers a rollback!

   do alices.transaction():
       break
       # Triggers a commit!

Going the 'non-exceptional exits' route also saves inventing a pseudo-exception 
to stand in for the 3 non-local goto statements (such a pseudo-exception would 
recreate the above behavioural hole, anyway).

An exceptional exit can be forced if a non-local goto needs to be executed in 
response to a failure:

   class AbortDo(Exception): pass

   do alices.transaction():
       break
       # Triggers a commit (non-exceptional exit)

   try:
       do alices.transaction():
           raise AbortDo
           # Triggers a rollback (exceptional exit)
   except AbortDo:
       break

Cheers,
Nick.

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


More information about the Python-Dev mailing list