classification
Title: Cannot copy a class with a metaclass other than type
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: alexandre.vassalotti Nosy List: alexandre.vassalotti, daniel.urban, eric.araujo, nailor, ncoghlan, pitrou, python-dev, stutzbach
Priority: normal Keywords: needs review, patch

Created on 2011-03-13 09:04 by daniel.urban, last changed 2013-12-01 21:27 by alexandre.vassalotti. This issue is now closed.

Files
File name Uploaded Description Edit
copy_metaclass.patch daniel.urban, 2011-03-13 09:04 review
copy_metaclass_2.patch daniel.urban, 2012-06-03 13:49 Updated patch review
Messages (8)
msg130731 - (view) Author: Daniel Urban (daniel.urban) * Date: 2011-03-13 09:04
copy.copy cannot copy a class which have a metaclass other than type:

>>> import abc
>>> import copy
>>> 
>>> class C(metaclass=abc.ABCMeta):
...     pass
... 
>>> copy.copy(C)
Traceback (most recent call last):
    ...
TypeError: can't pickle int objects


The reason seems to be, as described in msg8329 (issue494904) that the __reduce_ex__ function inherited from object will be called instead of the method bound to the class object (that's the reason of the strange error message). (See also issue7689.)

The interesting thing is, that copy.deepcopy was already fixed in 4680ef4fe90a.  I'm attaching a patch, that does basically the same for copy that was done with deepcopy (it also includes a test).
msg131343 - (view) Author: √Čric Araujo (eric.araujo) * (Python committer) Date: 2011-03-18 17:57
Looks good to me.
msg162211 - (view) Author: Daniel Urban (daniel.urban) * Date: 2012-06-03 13:49
I've updated the patch to the current version. I've also checked, that the tests still pass.
msg182891 - (view) Author: Jyrki Pulliainen (nailor) * Date: 2013-02-24 20:26
Hi, to be able to include your patch, could you sign the contributor form, please: http://www.python.org/psf/contrib/ ?
msg182893 - (view) Author: Daniel Urban (daniel.urban) * Date: 2013-02-24 20:53
I did, and I've been told that it has been added to the bug tracker: http://mail.python.org/pipermail/python-committers/2012-May/001987.html
msg182894 - (view) Author: Jyrki Pulliainen (nailor) * Date: 2013-02-24 21:03
Oh, my bad. The * was not just showing next to your name. Maybe someone with more access rights can help?
msg204965 - (view) Author: Roundup Robot (python-dev) Date: 2013-12-01 21:26
New changeset c6bb6f304f75 by Alexandre Vassalotti in branch '3.3':
Issue #11480: Fixed copy.copy to work with classes with custom metaclasses.
http://hg.python.org/cpython/rev/c6bb6f304f75

New changeset c4715c9f442f by Alexandre Vassalotti in branch 'default':
Issue #11480: Merge with 3.3.
http://hg.python.org/cpython/rev/c4715c9f442f
msg204966 - (view) Author: Alexandre Vassalotti (alexandre.vassalotti) * (Python committer) Date: 2013-12-01 21:27
Thank you for the patch!
History
Date User Action Args
2013-12-01 21:27:54alexandre.vassalottisetstatus: open -> closed
versions: + Python 3.4, - Python 3.1, Python 3.2
messages: + msg204966

assignee: alexandre.vassalotti
resolution: fixed
stage: patch review -> resolved
2013-12-01 21:26:58python-devsetnosy: + python-dev
messages: + msg204965
2013-02-24 21:03:08nailorsetmessages: + msg182894
2013-02-24 20:53:23daniel.urbansetmessages: + msg182893
2013-02-24 20:26:17nailorsetnosy: + nailor
messages: + msg182891
2012-06-03 13:49:15daniel.urbansetfiles: + copy_metaclass_2.patch
nosy: + ncoghlan
messages: + msg162211

2011-03-18 17:57:22eric.araujosetnosy: + eric.araujo, pitrou
messages: + msg131343
2011-03-14 16:48:18stutzbachsetkeywords: + needs review
nosy: + alexandre.vassalotti, stutzbach

stage: patch review
2011-03-13 09:04:19daniel.urbancreate