-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
deepcopy doesn't copy instance methods #45856
Comments
Currently, using deepcopy on instance methods causes an exception to be d[types.MethodType] = _deepcopy_atomic This will not make duplicate copies of mutable values referenced within |
I disagree that this would be harmless; this was excluded intentionally. This is different from classes (which could contain state) or modules I suppose you have a specific use case in mind. Can't you solve that by |
I am implementing a library that makes extensive use of delayed I don't mind whether instance methods have shallow or deep copies as TypeError: instancemethod expected at least 2 arguments, got 0 Ideally, deepcopy() and pickle() would copy the internals of all No matter the implementation, I think that there should be some way of |
I'll take this off line. |
Guido pointed out a common use case where people use bound methods in a def _deepcopy_method(x, memo):
return type(x)(x.im_func, deepcopy(x.im_self, memo), x.im_class)
d[types.MethodType] = _deepcopy_method The function and class are still shallow-copied but the bound object is |
Hallo, I' developer of the OpenOpt, free Python-based numerical Please don't you mind me to increase Severity of the bug to major. This http://openopt.blogspot.com/2008/01/about-prob-structure-redefinition.html |
I've read your blog. You don't have to modify the Python core: import copy
import types
def _deepcopy_method(x, memo):
return type(x)(x.im_func, deepcopy(x.im_self, memo), x.im_class)
copy._deepcopy_dispatch[types.MethodType] = _deepcopy_method I still wonder why you have to use deepcopy in your app. |
I'm bumping up the version number to 2.6. Python 2.5 is in maintenance |
I'm fine with applying Michael's suggestion to 2.6. I're reverting the priority change though; using deepcopy is not OO and |
Since Guido accepted the proposal can you please provide a patch with at |
I don't know did you mean it to me (as I've noticed from address) or no. import copy
import types
def _deepcopy_method(x, memo):
return type(x)(x.im_func, deepcopy(x.im_self, memo), x.im_class)
copy._deepcopy_dispatch[types.MethodType] = _deepcopy_method
from scikits.openopt import NLP
p = NLP(lambda x: x**2, 1)
p3 = _deepcopy_method(p, None) Traceback (most recent call last):
File "test_deepcopy.py", line 10, in <module>
p3 = _deepcopy_method(p, None)
File "test_deepcopy.py", line 5, in _deepcopy_method
return type(x)(x.im_func, deepcopy(x.im_self, memo), x.im_class)
AttributeError: NLP instance has no attribute 'im_func' as for copy.py from Python core, it seems like the file has no Users want to run r = p.solve(solverName) for several different solvers and same p instance. So it needs each time using (assign prob only once) Christian Heimes wrote:
|
Dmitrey: You can't call _deepcopy_method() on anything other than Christian Heimes, this is my unit test: # This test would fail due to an exception during deepcopy in version 2.5.1 class C(object):
def __init__(self, n):
self.n = n
def GetN(self):
return self.n
orig_obj = C(42)
copy_list = copy.deepcopy([orig_obj, orig_obj.GetN])
if copy_list[1]() != 42:
print 'Error: Instance method lost object?'
orig_obj.n = 43
if copy_list[1]() != 42:
print 'Error: Instance method assoc with orig object'
copy_list[0].n = 44
if copy_list[1]() != 44:
print 'Error: Instance method should assoc with new object copy'
if not type(copy_list[1]) is type(orig_obj.GetN):
print 'Error: Deepcopy changed type of instance method' Why do people want to use copy and deepcopy? I think that the issue is |
On Jan 31, 2008 2:54 PM, Michael Van Biesbrouck wrote:
I see the point for shallow copies. I just don't see the point for deep copies.
I don't understand the use case. I still think this comes primarily |
(I see some time has passed since the last message. I'm assuming the Here's a use case for using deepcopy: I'm developing a simulations This bug is currently preventing me from writing simpacks whose world I think this issue should be bumped in priority. Also, in the mean time, |
Ran into this trying to do some test isolation stuff. Notwithstanding the questions about 'why', this is a clear limitation I've attached a patch, with a test (in the style of the current tests) |
This affects 2.7 too. |
Robert's patch looks fine to me. My concern is changing this in a point Probably a question for python-dev. |
The test should be a real unittest in Lib/test/test_copy, not something To nitpick a bit, I also think Michael's test above was better, since it |
@antoine, I agree that the tests for copy should be a proper unit test; I don't have a checkout of 3 at the moment, but do you think the test |
Not really, since Lib/test/test_copy.py exists and contains tests for
What I mean is that Lib/copy.py fails without your changes under py3k. |
Oh man, I looked for a regular unit test - sorry that I missed it. Bah. I've added a call to the method and moved it into test_copy. |
Smells like a feature to me. "Bug" == "coding error" "Feature" == "change in (documented or intended) behavior" I'm fine with this going into 2.7 / 3.2. |
Guido - agreed! Versions updated. |
The patch has been committed in r76571 (trunk) and r76572 (py3k). Thank you! |
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: