Gra*_*row 5 python django ctypes
在我的django应用程序(在Red Hat上运行的Django 1.6.0,python 2.7.5)中,我的数据库驱动程序从ctypes看到错误:
TypeError:类型不兼容,使用LP_c_int实例而不是LP_c_int实例
代码如下:
is_null = value is None
param.value.is_null = pointer(c_int(is_null)) <-- error occurs on this line
Run Code Online (Sandbox Code Playgroud)
我对ctypes并不是很熟悉(我继承了这段代码),但从表面上看,此错误消息没有任何意义。该代码已经运行了多年,但现在到处都可以看到此错误。我做错了什么吗?
由于IljaEverilä的评论,这是一个最小的复制:
from ctypes import *
from ctypes import _reset_cache
class DataValue(Structure):
"""Must match a_sqlany_data_value."""
_fields_ = [("buffer", POINTER(c_char)),
("buffer_size", c_size_t),
("length", POINTER(c_size_t)),
("type", c_int),
("is_null", POINTER(c_int))]
d = DataValue()
_reset_cache()
is_null = False
d.is_null = pointer( c_int( is_null ) )
Run Code Online (Sandbox Code Playgroud)
我遇到了类似的问题,就我而言,它与预定义空有关,Structure以避免循环引用。我正在做这样的事情:
from ctypes import Structure, POINTER, pointer, c_int
class B(Structure):
pass
class A(Structure):
_fields_ = (('b_ref', POINTER(B)),)
class B(Structure):
_fields_ = (('a_ref', POINTER(A)),)
b = B()
a = A()
a.b_ref = pointer(b)
b.a_ref = pointer(a)
Run Code Online (Sandbox Code Playgroud)
结果如下:
TypeError:类型不兼容,LP_B 实例而不是 LP_B 实例
多么无益的消息...
这些评论让我知道了 ctypes 缓存的存在,这让我意识到重新定义是问题所在。解决方案是在设置之前简单地预定义所有类_fields_:
from ctypes import Structure, POINTER, pointer, c_int
class A(Structure): pass
class B(Structure): pass
A._fields_ = (('b_ref', POINTER(B)),)
B._fields_ = (('a_ref', POINTER(A)),)
b = B()
a = A()
a.b_ref = pointer(b)
b.a_ref = pointer(a)
Run Code Online (Sandbox Code Playgroud)
然而,关于您与本机的确切问题c_int,我相信这可能与 Django 的开发服务器重新加载任何更改的模块的功能有关。然而,我从未使用过 django,这对我来说很难测试(特别是考虑到帖子的年龄)。ctypes导入时自动调用_reset_cache()。通常重复调用import ...只是为了获取库的缓存版本,所以我认为它一定与某种importlib.reload(...)类似的调用有关。
| 归档时间: |
|
| 查看次数: |
224 次 |
| 最近记录: |