gpt4 book ai didi

python - Cython 和 deepcopy() 与引用的方法/函数有关。任何替代想法?

转载 作者:太空狗 更新时间:2023-10-29 17:30:55 25 4
gpt4 key购买 nike

我最近一直在使用 Cython 来提高速度,但我的项目继承了一个具有 copy() 的模块使用 deepcopy() 的方法.我尝试实现 deepcopy()copy() 的覆盖版本中,我以为我已经开始工作了,但它似乎不再工作了。

TypeError: object.__new__(cython_binding_builtin_function_or_method) is not safe,
use cython_binding_builtin_function_or_method.__new__()

这发生在 python/lib/copy_reg.py 中:

return cls.__new__(cls, *args)

我这里使用的是 Python 2.7。是否有可能从 deepcopy() 返回更新版本的 Python以“安全”的方式?我也在使用最新版本的 Cython,0.15.1。

更新3

请注意,我删除了之前的更新,以尽可能简单。

好的!我想我发现了不兼容性,但我真的不知道该怎么办。

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}

然后在我的主应用中:

from cython1 import CythonClass
from copy import deepcopy

if __name__ == "__main__":
gc1 = CythonClass()
gc1.call_handle()
gc2 = deepcopy(gc1)

我得到:

I'm a little handle!

Traceback (most recent call last):
File "cythontest.py", line 8, in <module>
gc2 = deepcopy(gc1)
File "C:\python26\lib\copy.py", line 162, in deepcopy
y = copier(x, memo)
File "C:\python26\lib\copy.py", line 292, in _deepcopy_inst
state = deepcopy(state, memo)
File "C:\python26\lib\copy.py", line 162, in deepcopy
y = copier(x, memo)
File "C:\python26\lib\copy.py", line 255, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\python26\lib\copy.py", line 189, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\python26\lib\copy.py", line 323, in _reconstruct
y = callable(*args)
File "C:\python26\lib\copy_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__()

关键是函数/句柄引用:

handles = {"handle_method", handle_method}

如果我不包含方法/函数引用,Cython 将不会在 deepcopy 期间崩溃。如果我包含一个,它不喜欢 deepcopy/copy_reg 复制引用的方式。

除了不使用方法/函数引用之外还有什么想法吗?如果这是简单的答案,我还有一些事情要做。 (当我完成输入时,我已经在做这件事)

谢谢!

最佳答案

发现这个:

“深度复制是否与 Cython 一起正常工作?”

没有。在这种情况下(您正在使用扩展类型,即 cdef 类)您必须为你的类(class)实现 pickle 协议(protocol) http://docs.python.org/library/pickle.html#pickling-and-unpickling-extension-types

来自这里:https://groups.google.com/forum/#!topic/cython-users/p2mzJrnOH4Q

链接文章中的“实现 pickle 协议(protocol)”实际上很简单,简单地解决了我的问题(尽管我做的事情略有不同——我的类是一个 cdef 类,我有一个指针到存储在那里的 CPP 对象,它不能简单地复制——我不知道这是否会解决上面的 python 继承问题,但它肯定值得一试。)

无论如何,实现 pickle 协议(protocol)是微不足道的(下面的示例使用“C++ cython”,它对 del 关键字具有双重含义,among other things。):

cdef class PyObject(object):
cdef CppObject* cpp
cdef object arg1
cdef object arg2

def __cinit__(self, arg1=[], arg2=False):
# C++ constructor using python values, store result in self.cpp.

# new code: cache the python arguments that were used.
self.arg1 = arg1
self.arg2 = arg2

def __init__(self, arg1=[], arg2=False):
# logic for validating arguments.
pass

def __dealloc__(self):
if not self.cpp == NULL:
del self.cpp

def __reduce__(self):
# a tuple as specified in the pickle docs - (class_or_constructor,
# (tuple, of, args, to, constructor))
return (self.__class__, (self.arg1, self.arg2))

当我尝试这个时,我可以在包含我的 Cython 扩展类型实例的字典上调用 copy.deepcopy(),并获得一个包含新实例(具有不同内存地址)的新字典当打印到终端时。)以前相同的代码导致了段错误。

关于python - Cython 和 deepcopy() 与引用的方法/函数有关。任何替代想法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7666873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com