Douglas Raillard <douglas.raill...@arm.com> added the comment:
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 ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue43460> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com