-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Exception-like Objects (TracebackException) For exc.__cause__ and exc.__context__ #111921
Comments
Can we just make |
I suppose we could. That would certainly be simpler. |
Having thought about it, the thing I don't like about subclassing Another option: support a new attr (something like
|
There might be a third option: Instead of making TracebackException extend Exception, define an exception type that wraps a TracebackException. Then if the wrapper gets raised, it doesn't trample on the data in the wrapped TracebackException. |
I'm glad you brought this up because I had the same idea. Last week I actually spent some time trying out a dedicated exception that wraps ...which is why I thought of |
Just to summarize, here are the options we've discussed so far:
Each of (1)-(3) has awkward potential corner cases and some awkward conceptual weirdness. I'd strongly favor (4) if it didn't feel so heavy-handed. |
Another idea: Ditch the generalized solution and deal with it via the specific exception (like we do with |
I think |
I'm closing this since I don't need it or any of its variants any more. |
Feature or enhancement
Currently
__cause__
and__context__
are expected to be exception objects (i.e. instances ofBaseException
). 1 We don't always enforce this expectation, but we definitely do in some places.Proposal
I'd like to allow them to be non-exception objects, as long as the objects look like exceptions. My own interest is that I be able to set either attribute to
traceback.TracebackException
objects specifically. However, I expect that supporting them would in practice mean supporting any object that matches the appropriate drop-in-replacement interface. [^2][^2] See gh-111917.
Note that I am not suggesting that we be able to use such drop-in-replacements anywhere the runtime expects exception objects. You couldn't start raising
TracebackException
objects nor catch them I don't have any motivating case for any of that. I'm only suggesting support in__cause__
and__context__
.drop-in-replacement for exception objects
The idea of drop-in-replacement (proxy or not) for an exception object isn't mine alone. The implication of the
traceback.TracebackException
docs is that it may be used as a representative proxy of an exception object. Furthermore, there has already been some consideration for treatingTracebackException
as a drop-in-replacement in actual technical situations, e.g. when pickling: #73652 (comment).What would need to happen?
How are
__cause__
and__context__
used (or might be used)?sys.excepthook()
What would have to change?
TracebackException
appropriately (it is used for the defaultsys.excepthook()
2); the diff is actually relatively smallOther Notes
Prior discussion about supporting
traceback.StackSummary
where we currently support__traceback__
: #74764 (comment)Footnotes
This is just like how
__traceback__
is currently expected to be aTracebackType
instance. ↩Since gh-110721: Use the traceback module for PyErr_Display() and fallback to the C implementation #110702 landed, we can accommodate
TracebackException
like this much more easily. ↩The text was updated successfully, but these errors were encountered: