从 C 返回 CTypes 指针
我正在编写一个 Python C 扩展,它需要返回一个指向内存中 char 数组的 CTypes 指针(我需要与另一个需要 CTypes 指针的 Python 库进行交互)。
我找不到有关 C 的任何类型的 CTypes 接口的任何文档。是否有任何解决方法,例如调用 Python 指针构造函数?
static PyObject *get_pointer(myObject *self)
{
char *my_pointer = self->internal_pointer;
return PyCTypes_Pointer(my_pointer); /* how do I turn 'my_pointer' into
a Python object representing a CTypes pointer? */
}
提前致谢。
I'm writing a Python C Extension that needs to return a CTypes pointer to a char
array in memory (I need to interface with another Python library that expects a CTypes pointer).
I cannot find any documentation on any kind of CTypes interface for C. Are there any workarounds, like calling the Python pointer constructor?
static PyObject *get_pointer(myObject *self)
{
char *my_pointer = self->internal_pointer;
return PyCTypes_Pointer(my_pointer); /* how do I turn 'my_pointer' into
a Python object representing a CTypes pointer? */
}
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使您使用 C 语言编程,也请尝试使用 Python 本身。遗憾的是我不知道 C 的任何类型的 CTypes 接口,并且在源代码中找不到任何提示。也许您可以使用以下代码编写自己的
PyCTypes_Pointer
方法。我没有在这里进行任何清理、错误检查或处理,但它对我来说适用于 Python 2.7 和 3.4。
只需构建
并获得类似的输出
如果您仅处理以零结尾的字符串,您可能可以直接使用
ctypes.c_char_p
,也许......Even though you're programming in C, try using Python itself. Sadly I don't know of any kind of CTypes interface for C and couldn't find any hint in the sources. Maybe you'll be able to write your own
PyCTypes_Pointer
method with the following.I didn't do any cleanup, error checking or handling here, but it's working with Python 2.7 and 3.4 for me.
Just build with
and get an output like
If you're handling zero-terminated strings only, you might be able to use
ctypes.c_char_p
directly, maybe ...