Repository navigation
Expose PyErr_ChainExceptions in the stable API #89101
Description
Activity
Currently, exception chaining in the C-API must be done by hand. This is a painstaking process that involves several steps and is very easy to do incorrectly.
We currently have a private function: _PyErr_ChainExceptions that does this job. Given that exception chaining is a very fundamental operation, this functionality must be exposed in the stable C-API
As the original author of _PyErr_ChainExceptions() (bpo-21715) I am not sure that its name and signature are optimal. It did not matter while it was internal function because we could change them at any moment, but for public API the design should be more thoughtful.
Is passing three arguments convenient enough? Would not be better to pass them as a single argument like in _PyErr_ChainStackItem()? Is the function name good? Should we provide several similar functions for setting __context__, __cause__ and both?
There are other, more specialized, helpers: _PyErr_FormatFromCause() and _PyErr_TrySetFromCause().
There are also some flaws in setting __context__ and __cause__ (see bpo-39725). Perhaps we will design better concept of chaining exceptions in future.
- added3.11only security fixesonly security fixestype-featureA feature request or enhancementA feature request or enhancement
on Aug 18, 2021 Serhiy, what do you suggest we should do then?
I agree with Serhiy that this API isn't necessarily the right one. It's just what happens to be there now. Also, we're still not clear on our stance towards cycles (see the bpo-25782 discussion). (Maybe the exposed version should result in an error if it would cause a cycle to be created.) Someone would need to think through what the API should be, though.
As long as the cycles don't actually end in a crash or an infinite cycle, I think is fine to not include them in the contact.
Even if that were included I don't think is actually a problem. Many APIs in C or C++ require the user to not form cycles, for instance, shared pointers in the c++ stdlib.
related: https://www.python.org/dev/peps/pep-0490/ (rejected pep to auto-chain from C API calls)
I don't come across it often, but I did just review some code at work today where the chain might have been nice (though not a big deal).
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: