Message388428
The solution based on the signature is something along those lines:
class E(BaseException):
def __new__(cls, *args, **kwargs):
"""
Fix exception copying.
Turn all the keyword arguments into positional arguments, so that the
:exc:`BaseException` machinery has all the parameters for a valid call
to ``__new__``, instead of missing all the keyword arguments.
"""
sig = inspect.signature(cls.__init__)
bound_args = sig.bind_partial(*args, **kwargs)
bound_args.apply_defaults()
args = tuple(bound_args.arguments.values())
return super().__new__(cls, *args)
def __init__(self, x):
self.x=x
But there are a many shortcomings to that approach:
* What if super().__new__() consumes arguments before passing the rest to __init__() ? This fix is blind to that since it only cares about __init__ signature
* What if inspect.signature() does not provide a signature (extension modules) ?
* Defaults are "hardcoded" in the args, so the object will always be restored with the defaults of the time it was created. This is a breaking change, as currently the defaults used when restoring the instance are the current ones.
* Also uses more memory for args (and for pickle files), since it contains all the defaults |
|
Date |
User |
Action |
Args |
2021-03-10 11:47:51 | douglas-raillard-arm | set | recipients:
+ douglas-raillard-arm |
2021-03-10 11:47:51 | douglas-raillard-arm | set | messageid: <1615376871.09.0.380975849727.issue43460@roundup.psfhosted.org> |
2021-03-10 11:47:51 | douglas-raillard-arm | link | issue43460 messages |
2021-03-10 11:47:50 | douglas-raillard-arm | create | |
|