[Python-Dev] PEP 310 and exceptions

Bernhard Herzog bh at intevation.de
Sat Apr 23 19:59:29 CEST 2005


Nick Coghlan <ncoghlan at gmail.com> writes:

> holger krekel wrote:
>> Moreover, i think that there are more than the "transactional"
>> use cases mentioned in the PEP.  For example, a handler may want to
>> log exceptions to some tracing utility or it may want to swallow
>> certain exceptions when
>> its block does IO operations that are ok to fail. 
>
> With the current PEP 310 definition, these can be manually handled using
> sys.exc_info() in the __exit__ method.

With the proposed implementation of PEP 310 rev. 1.5 it wouldn't work.
sys.exc_info returns a tuple of Nones unless an except: clause has been
entered.  Either sys.exc_info() would have to be changed to always
return exception information after an exception has been raised or the
implementation would have to be changed to do the equivalent of e.g.

    if hasattr(var, "__enter__"):
	var.__enter__()

    try:
        try:
            suite
        except:
            pass
    finally:
        var.__exit__()


An empty except: suite suffices.  In C that's equivalent to a call to
PyErr_NormalizeException AFAICT.



   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Skencil                                           http://skencil.org/
Thuban                                  http://thuban.intevation.org/


More information about the Python-Dev mailing list