Skip to content

Fix copying generic instances in Python 3#502

Merged
ilevkivskyi merged 2 commits into
python:masterfrom
ilevkivskyi:fix-copy
Nov 25, 2017
Merged

Fix copying generic instances in Python 3#502
ilevkivskyi merged 2 commits into
python:masterfrom
ilevkivskyi:fix-copy

Conversation

@ilevkivskyi
Copy link
Copy Markdown
Member

Fixes #498 on Python 3

Unfortunately, it looks like this can't be (reasonably) fixed on Python 2, unless https://bugs.python.org/issue11480 is first backported to Python 2

@ilevkivskyi
Copy link
Copy Markdown
Member Author

Just to clarify, on Python 2 the problem with copying classes is not specific to generics, any class with a metaclass other than type can't be copied:

>>> class M(type): pass
>>> class C(object):
...     __metaclass__ = M
>>> from copy import copy
>>> copy(C)  # obscure TypeError

But the ad-hoc __copy__ on metaclass fix that is currently used clashes with copying instances of generic classes.

@gvanrossum
Copy link
Copy Markdown
Member

gvanrossum commented Nov 25, 2017 via email

@ilevkivskyi
Copy link
Copy Markdown
Member Author

If you mean copy_reg.dispatch_table, then no, __copy__ is searched before. But there is an internal API copy._copy_dispatch (see new commit) that works. If you are fine with this, then I am also fine.

@ilevkivskyi
Copy link
Copy Markdown
Member Author

Oh, wait, but we don't need __copy__. OK, then I will make a cleaner fix now.

@ilevkivskyi
Copy link
Copy Markdown
Member Author

Actually, copy_reg explicitly says that copy_reg is not intended for use with classes, therefore I think the _copy_dispatch fix for Python 2 is OK.

@ilevkivskyi ilevkivskyi merged commit cca58ee into python:master Nov 25, 2017
@ilevkivskyi ilevkivskyi deleted the fix-copy branch November 25, 2017 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error when copying instances of Generic subclasses

3 participants