-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
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
Exception copy error #87626
Comments
Instances of subclasses of BaseException created with keyword argument fail to copy properly as demonstrated by: import copy
class E(BaseException):
def __init__(self, x):
self.x=x
# works fine
e = E(None)
copy.copy(e)
# raises
e = E(x=None)
copy.copy(e) This seems to affect all Python versions I've tested (3.6 <= Python <= 3.9). I've currently partially worked around the issue with a custom pickler that just restores __dict__, but:
One the root of the issue:
It seems that the current behavior is a consequence of the __dict__ being created lazily, I assume for speed and memory efficiency There seems to be a few approaches that would solve the issue:
[1] https://github.com/python/cpython/blob/master/Objects/exceptions.c#L134 |
The solution based on the signature is something along those lines: class E(BaseException):
def __new__(cls, *args, **kwargs):
"""
Fix exception copying.
def __init__(self, x):
self.x=x But there are a many shortcomings to that approach:
|
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:
The text was updated successfully, but these errors were encountered: