使用 python scipy.weave 内联 ctype 变量?
我正在尝试使用 scipy.weave.inline 将 ctype 变量传递给内联 c 代码。 人们会认为这很简单。 当使用普通的 python 对象类型时,文档很好,但是,它们具有比我需要的更多的功能,并且在使用 C 时使用 ctypes 对我来说更有意义。但是,我不确定我的错误在哪里。
from scipy.weave import inline
from ctypes import *
def test():
y = c_float()*50
x = pointer(y)
code = """
#line 120 "laplace.py" (This is only useful for debugging)
int i;
for (i=0; i < 50; i++) {
x[i] = 1;
}
"""
inline(code, [x], compiler = 'gcc')
return y
output = test()
pi = pointer(output)
print pi[0]
I am trying to pass a ctype variable to inline c code using scipy.weave.inline. One would think this would be simple. Documentation is good when doing it with normal python object types, however, they have a lot more features than I need, and It makes more sense to me to use ctypes when working with C. I am unsure, however, where my error is.
from scipy.weave import inline
from ctypes import *
def test():
y = c_float()*50
x = pointer(y)
code = """
#line 120 "laplace.py" (This is only useful for debugging)
int i;
for (i=0; i < 50; i++) {
x[i] = 1;
}
"""
inline(code, [x], compiler = 'gcc')
return y
output = test()
pi = pointer(output)
print pi[0]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
scipy.weave 对 ctypes 一无所知。 输入仅限于大多数基本内置类型、numpy 数组、wxPython 对象、VTK 对象和 SWIG 包装对象。 不过,您可以添加自己的转换器代码。 目前这方面的文档不多,但您可以查看 SWIG 实现 作为一个指导性示例。
scipy.weave does not know anything about ctypes. Inputs are restricted to most of the basic builtin types, numpy arrays, wxPython objects, VTK objects, and SWIG wrapped objects. You can add your own converter code, though. There is currently not much documentation on this, but you can look at the SWIG implementation as an instructive example.