<div dir="ltr">There are certainly still questions about appropriate behavior. I think the least-surprising result of a failure in e.__raised__ is to raise with context, but I realize I'm naive about the implementation-level implications of doing so.<div><br></div><div>I thought twice about using that word, but by calling __raised__ straightforward, I meant that a failure in the __raise__ version creates more ambiguity in regard to what those properties will contain. Does it keep only the properties successfully set before failure and attempt no recovery? Does it try to make lemonade by keeping the successful properties, but set default values for the others? Does it overwrite all values with defaults as if the magic method wasn't defined? I'd say the first is least surprising because it lacks magic, but it means a failure can break the cause/context chain; these all seem quite surprising in their own ways.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Apr 11, 2015 at 8:15 PM, Andrew Barnert <span dir="ltr"><<a href="mailto:abarnert@yahoo.com" target="_blank">abarnert@yahoo.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div><span></span></div><div><span class=""><div>On Apr 11, 2015, at 08:40, Travis Everett <<a href="mailto:travis.a.everett@gmail.com" target="_blank">travis.a.everett@gmail.com</a>> wrote:</div><div><br></div><blockquote type="cite"><div><div dir="ltr">Thanks for the addendum--I suspected a mix of performance and the difficulty of handling potential failures at that point would be the ultimate answer to why the exception is already cut out of the loop there. Not that the dumber __raised__() version wouldn't have the same problem, but its behavior in case of failure could be straightforward.</div></div></blockquote><div><br></div></span><div>Is it straightforward? When e.__raised__ raises, do you want to just swallow it and raise e anyway, or raise the exception raised by e.__raised__ with e as a context, or lose e and just raise the exception raised by e.__raised__? (Or you could just punt like C++ and say that if an exception type itself raises an exception in a place that's hard to deal with, the program terminates. :) Remember the possibility that what e.__raised__ raised may be a MemoryError, which always makes things extra fun.</div><div><br></div><div>You have pretty much the same choices in setting the __cause__ or __context__; some of the answers may turn out to be harder to implement there than with __raised__, but instead of just assuming that means the ideal version of your proposal won't work, it's probably better to decide what behavior is actually right, and then see if it turns out to be impossible.</div><div><br></div><div>So... Which one do you think is right in each case?</div><div><div class="h5"><br><blockquote type="cite"><div><div dir="ltr"><div>An idea I had last night led to some progress this morning. BaseException's with_traceback(tb) method already sets some thematic precedent for users being able to cause new exceptions while setting values within the raise process, (though unfortunately it looks like the traceback it sets gets overwritten by the new one set in C during the raise.) I realize that this is just a thematic precedent, since they'd be caused at very different places. </div><div><br></div><div>For now I've replaced my previous workaround with a with_cause(c) method which is at least a step in the right direction for handling my specific case in a way that feels roughly congruent with existing idioms, makes sense in that context, and cleans up code using the exceptions; __cause__ still gets overwritten by the C code, but this doesn't cause trouble since we know what this object will be at raise time. This wouldn't suffice if I needed access to __traceback__ at this point.</div><div><br></div><div>RE the question in your first response on patch benchmarking: I'm new to the list and have no previous contributions, but there's a first time for everything; if the information on doing so is readily available I should be fine, but otherwise I'd need some direction.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Apr 10, 2015 at 5:07 PM, Andrew Barnert <span dir="ltr"><<a href="mailto:abarnert@yahoo.com" target="_blank">abarnert@yahoo.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><span><div>On Apr 10, 2015, at 14:39, Andrew Barnert <<a href="mailto:abarnert@yahoo.com.dmarc.invalid" target="_blank">abarnert@yahoo.com.dmarc.invalid</a>> wrote:</div><div><br></div><blockquote type="cite"><div><div>On Apr 10, 2015, at 12:54, Travis Everett <<a href="mailto:travis.a.everett@gmail.com" target="_blank">travis.a.everett@gmail.com</a>> wrote:</div><div><br></div><blockquote type="cite"><div><div dir="ltr"><div>Hi all,</div><div><br></div><div>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.<br></div><div><br></div><div>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.</div><div><br></div><div>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.</div></div></div></blockquote><div><br></div>I'd assume the only reasons are to make the code a bit simpler and a bit more efficient.</div></blockquote><div><br></div></span>Actually, on second though, it's not _quite_ that simple.<div><br></div><div>First, settings args, __traceback__ etc. are no longer hookable; it seems like you'd want those to be as well, for consistency? So that's a few more functions to change.</div><div><br></div><div>Second, the API functions for setting cause and context (and the private API function for automatically setting context) can't possibly fail anywhere, so they have no return value. Changing them to go through SetAttr means they now can (your __setattr__ could raise anything it wants--or even creating the 1 object to store in __suppress_context__ could fail). Should we just swallow and ignore any such exceptions when called from the C error API? (C code that uses the exception type API or the object API will of course continue to get exceptions, as will Python code, it's just the error machinery that would ignore errors from munging exceptions to be raised.)</div><span><div><div><br></div><div><blockquote type="cite"><div><div>Adding some new __raise__ or __raised__ special-purpose protocol doesn't seem like it would be any simpler or more efficient than just changing the existing APIs to access the attributes via SetAttr, which would give you what you want without being a special case.</div><div><br></div><div><span style="background-color:rgba(255,255,255,0)">Also notice that there's an open PEP (<a href="https://www.python.org/dev/peps/pep-0490/" target="_blank">https://www.python.org/dev/peps/pep-0490/</a>) to make the C API for raising exceptions automatically set the context instead of forcing it to be done manually (with a private API function), as a few builtin and stdlib functions do. So it seems safer to change things in a way that would work equally well with the current APIs, with that change, and with all reasonable alternatives to that change--and again, using SetAttr seems like the simplest way to make sure of that.</span></div><div><br></div><div>But either way, it's a relatively local and simple change in Objects/exceptions.c.</div><div><br></div><div>I suspect the big thing you'd need to get this accepted is to show that the performance cost is negligible. Do you know how to run appropriate benchmarks and present the results if someone else writes the patch?</div><div><div><div><br></div><blockquote type="cite"><div><div dir="ltr">I was encouraged to bring discussion here (to see if there's any traction) after opening an enhancement request (<a href="http://bugs.python.org/issue23902" target="_blank">http://bugs.python.org/issue23902</a>).<br><div><br></div></div></div></blockquote><div><br></div><br><blockquote type="cite"><div><div dir="ltr"><div>Cheers,</div><div>Travis</div></div>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>Python-ideas mailing list</span><br><span><a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a></span><br><span><a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a></span><br><span>Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a></span></div></blockquote></div></div></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>Python-ideas mailing list</span><br><span><a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a></span><br><span><a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a></span><br><span>Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a></span></div></blockquote></div></div></span></div></blockquote></div><br></div>
</div></blockquote></div></div></div></div></blockquote></div><br></div>