提交 5d31582f authored 作者: Frédéric Bastien's avatar Frédéric Bastien

Merge pull request #2315 from abergeron/fix_cdata_debug

Fix problems with CDataType in DebugMode
......@@ -874,8 +874,12 @@ def _lessbroken_deepcopy(a):
"""
# this exists because copy.deepcopy on numpy arrays is broken
# This logic is also in link.py
from theano.gof.type import CDataType
if type(a) in (numpy.ndarray, numpy.memmap):
rval = a.copy()
elif type(a) is CDataType._cdata_type:
# This is not copyable (and should be used for constant data).
rval = a
else:
rval = copy.deepcopy(a)
......
......@@ -479,6 +479,15 @@ class CDataType(Type):
Represents opaque C data to be passed around. The intent is to
ease passing arbitrary data between ops C code.
"""
import ctypes
if PY3:
_cdata_type = ctypes.py_object.from_address(
ctypes.addressof(ctypes.pythonapi.PyCapsule_Type)).value
else:
_cdata_type = ctypes.py_object.from_address(
ctypes.addressof(ctypes.pythonapi.PyCObject_Type)).value
del ctypes
def __init__(self, ctype, freefunc=None):
"""
Build a type made to represent a C pointer in theano.
......@@ -504,11 +513,9 @@ class CDataType(Type):
return hash((type(self), self.ctype, self.freefunc))
def filter(self, data, strict=False, allow_downcast=None):
if data is not None:
raise TypeError("only None is valid")
def is_valid_value(self, a):
return a is None
if data is not None and not isinstance(data, self._cdata_type):
raise TypeError("expected None or PyCObject/PyCapsule")
return data
def c_declare(self, name, sub, check_input=True):
return """
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论