[Python-ideas] notify exception during raise (once magic properties are set)

Travis Everett travis.a.everett at gmail.com
Fri Apr 10 21:54:32 CEST 2015


Hi all,

I have a specialized use case for exceptions which need to analyze their
__cause__ property before they're fully initialized. The property isn't set
until after init, and (forgive my ignorance of Python's source!) appears to
be done at the C level in a way that it can't be observed with a property
setter or __setattr__. I currently create a method to do this post-init
work as a workaround, and push the burden of making sure it gets called on
the code using the exceptions--but this makes the exceptions idiomatic and
error-prone.

A magic method called once all of the special exception properties are
already set would suffice. The exception could define something like
__raised__(self) to react to the new values of those properties.

A more comprehensive approach might be a __raise__(self, cause, context,
traceback...) method which has full control over setting the properties,
but I assume there are good reasons why the usual channels (setters and
__setattr__) are cut out of the loop.

I was encouraged to bring discussion here (to see if there's any
traction) after opening an enhancement request (
http://bugs.python.org/issue23902).

Cheers,
Travis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150410/e424023a/attachment.html>


More information about the Python-ideas mailing list