I've been playing with Cython recently for the speed ups, but my project inherits a module that has a copy()
method which uses deepcopy()
. I tried implementing the deepcopy()
within an overrided version of copy()
, and I thought I had it working, but it doesn't appear to be anymore.
TypeError: object.__new__(cython_binding_builtin_function_or_method) is not safe,
use cython_binding_builtin_function_or_method.__new__()
This is occuring in python/lib/copy_reg.py here:
return cls.__new__(cls, *args)
I'm on Python 2.7 here. Is it possible that a newer version of Python returns from deepcopy()
in a "safe" way? I'm also on the latest version of Cython, 0.15.1.
Update3
Note that I've removed the previous updates to keep this as simple as possible.
Ok! I think I found the incompatibility but I don't really know what to do about it.
class CythonClass:
def __init__(self):
self._handle = self._handles.get("handle_method")
def call_handle(self):
self._handle(self)
def handle_method(self):
print "I'm a little handle!"
handles = {"handle_method", handle_method}
Then in my main app:
from cython1 import CythonClass
from copy import deepcopy
if __name__ == "__main__":
gc1 = CythonClass()
gc1.call_handle()
gc2 = deepcopy(gc1)
I get:
I'm a little handle!
Traceback (most recent call last):
File "cythontest.py", line 8, in <module>
gc2 = deepcopy(gc1)
File "C:python26libcopy.py", line 162, in deepcopy
y = copier(x, memo)
File "C:python26libcopy.py", line 292, in _deepcopy_inst
state = deepcopy(state, memo)
File "C:python26libcopy.py", line 162, in deepcopy
y = copier(x, memo)
File "C:python26libcopy.py", line 255, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:python26libcopy.py", line 189, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:python26libcopy.py", line 323, in _reconstruct
y = callable(*args)
File "C:python26libcopy_reg.py", line 93, in __newobj__
return cls.__new__(cls, *args)
TypeError: object.__new__(cython_binding_builtin_function_or_method) is not safe, use cython_binding_builtin_function_or_method.__new__()
The key is the function/handle reference:
handles = {"handle_method", handle_method}
If I don't include the method/function reference, Cython will not blow up during deepcopy. If I include one, it doesn't like how deepcopy/copy_reg copies the reference over.
Any ideas besides not using method/function references? I have a bit of untangling to do if that the simple answer. (which I'm already working on as I finish typing this)
Thanks!
See Question&Answers more detail:
os