[英]Self-referencing class: concrete python class from C interface
I am trying to design a C interface which could easily be extended in Python (using ctypes ).我正在尝试设计一个可以在 Python 中轻松扩展的 C 接口(使用ctypes )。 I've used the natural idiom in C:
我在 C 中使用了自然习语:
struct format {
int (*can_open)(const char *filename);
struct format * (*open)(const char *filename);
void (*delete)(struct format *self);
int (*read)(struct format *self, char *buf, size_t len);
};
It works nicely if I want to extend this interface from C directly:如果我想直接从 C 扩展这个接口,它工作得很好:
struct derived /* concrete implementation */
{
struct format base;
};
But what I would really like to do, is implement this interface from Python using ctypes.但我真正想做的是使用 ctypes 从 Python 实现这个接口。 Here is what I have so far:
这是我到目前为止所拥有的:
CANOPENFUNC = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_char_p)
#OPENFUNC = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_char_p)
#OPENFUNC = ctypes.CFUNCTYPE(ctypes.POINTER( python_format ), ctypes.c_char_p)
#DELETEFUNC = ctypes.CFUNCTYPE(None, ctypes.c_void_p)
#READFUNC = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p)
def py_canopen_func( string ):
print "py_canopen_func", string
return 1
canopen_func = CANOPENFUNC(py_canopen_func)
#open_func = OPENFUNC( py_open_func)
#delete_func = DELETEFUNC(py_canopen_func)
#read_func = READFUNC(py_canopen_func)
class python_format(ctypes.Structure):
_fields_ = (
('can_open', CANOPENFUNC),
('open', OPENFUNC),
('delete', DELETEFUNC),
('read', READFUNC),
)
def __init__(self):
self.can_open = canopen_func
OPENFUNC = ctypes.CFUNCTYPE(ctypes.POINTER(python_format), ctypes.c_char_p)
def py_open_func2( string ):
print "py_open_func2", string
return ctypes.byref(self)
self.open = OPENFUNC( py_open_func2 )
#self.delete = delete_func
#self.read = read_func
Really I am struggling to define the prototype for OPENFUNC
here.我真的很努力在这里定义
OPENFUNC
的原型。 Technically it should be:从技术上讲,它应该是:
OPENFUNC = ctypes.CFUNCTYPE(ctypes.POINTER(python_format), ctypes.c_char_p)
However I need to define python_format
first, which in turns requires a definition for OPENFUNC
.但是我需要先定义
python_format
,而这又需要定义OPENFUNC
。
Bonus point: what would be an actual function implementation ?加分点:什么是实际的功能实现? For instance:
例如:
def func( str ): return None
or或者
def func( str ): i = python_format(); return ctypes.pointer(i)
both gives me:两者都给了我:
class python_format(ctypes.Structure):
pass
OPENFUNC = ctypes.CFUNCTYPE(ctypes.POINTER( python_format ), ctypes.c_char_p)
OPENFUNC( func )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: invalid result type for callback function
Is this related to this other issue ?这与其他问题有关吗? If so should I change my initial C design, since I will not be able to return a pointer to a
python_format
instance from a callback ?如果是这样,我是否应该更改我的初始 C 设计,因为我将无法从回调返回指向
python_format
实例的指针?
In the documentation for ctypes.Structure._fields_
it explains how to do this:在
ctypes.Structure._fields_
的文档中,它解释了如何执行此操作:
It is possible to define the
_fields_
class variable after the class statement that defines the Structure subclass, this allows to create data types that directly or indirectly reference themselves可以在定义 Structure 子类的 class 语句之后定义
_fields_
类变量,这允许创建直接或间接引用自身的数据类型
This means you can add a:这意味着您可以添加:
class python_format(ctypes.Structure): # forward declaration
pass
and then after defining OPENFUNC
(and the other function types):然后在定义
OPENFUNC
(和其他函数类型)之后:
OPENFUNC = ctypes.CFUNCTYPE(ctypes.POINTER( python_format ), ctypes.c_char_p)
DELETEFUNC = etc...
be able to then define python_format._fields_
thusly:然后能够这样定义
python_format._fields_
:
python_format._fields_ = (
('can_open', CANOPENFUNC),
('open', OPENFUNC),
('delete', DELETEFUNC),
('read', READFUNC),
)
Here's a more complete example based on your code:这是基于您的代码的更完整示例:
import ctypes
class python_format(ctypes.Structure): # forward declaration
pass
CANOPENFUNC = ctypes.PYFUNCTYPE(ctypes.c_int, ctypes.c_char_p)
OPENFUNC = ctypes.PYFUNCTYPE(ctypes.c_int,
ctypes.POINTER(python_format),
ctypes.c_char_p)
DELETEFUNC = ctypes.PYFUNCTYPE(None, ctypes.c_void_p)
READFUNC = ctypes.PYFUNCTYPE(ctypes.c_int, ctypes.c_void_p)
def py_canopen_func(self, string):
print "py_canopen_func", string
return 1
def py_open_func(self, string):
print "py_open_func2", string
# Return types from callbacks cannot be anything other than simple
# datatypes (c_int, c_float, ..., c_void_p). For other datatypes
# (STRUCTURE, POINTER, ...), ctypes returns the following error
# "Invalid result type for callback function"
# see http://bugs.python.org/issue5710
return 1 # can't return ctypes.byref(self)
canopen_func = CANOPENFUNC(py_canopen_func)
open_func = OPENFUNC(py_open_func)
#delete_func = DELETEFUNC(py_canopen_func)
#read_func = READFUNC(py_canopen_func)
class python_format(ctypes.Structure):
python_format._fields_ = (
('can_open', CANOPENFUNC),
('open', OPENFUNC),
('delete', DELETEFUNC),
('read', READFUNC),
)
def __init__(self):
self.can_open = canopen_func
self.open = open_func
#self.delete = delete_func
#self.read = read_func
pf = python_format()
In order to have a canonical answer, I'll answer my own question, thanks to @eryksun guidance.为了得到规范的答案,感谢@eryksun 的指导,我将回答我自己的问题。
So first thing first, while this is not clear from the documentation , one cannot return complex type from a callback function.所以首先,虽然文档中并不清楚,但不能从回调函数返回复杂类型。 Therefore, one cannot map a C function pointer:
因此,不能映射 C 函数指针:
struct format {
struct format * (*open)(const char *filename);
};
to到
class python_format:
pass
OPENFUNC = ctypes.CFUNCTYPE(ctypes.POINTER(python_format), ctypes.c_char_p)
def py_canopen_func( string ):
return None
open_func = OPENFUNC(py_open_func)
The above code will gracefully compiles, but at runtime, one gets:上面的代码可以优雅地编译,但在运行时,会得到:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: invalid result type for callback function
The long answer is that:长答案是:
The TypeError message you get when trying to use a non-simple type as the result of a callback is less than helpful.
尝试使用非简单类型作为回调的结果时,您收到的 TypeError 消息不太有用。 A callback's result type has to have a setfunc in its StgDictObject (a ctypes extension of the regular PyDictObject).
回调的结果类型必须在其 StgDictObject(常规 PyDictObject 的 ctypes 扩展)中有一个 setfunc。 This requirement restricts you to using a simple type such as c_void_p[...]
此要求限制您使用简单类型,例如 c_void_p[...]
Therefore the only solution here, as of today, until issue 5710 is fixed is the following:因此,截至今天,在问题 5710得到修复之前,这里唯一的解决方案如下:
class python_format(ctypes.Structure):
__self_ref = []
def __init__(self):
self.open = self.get_open_func()
# technically should be a @classmethod but since we are self-referencing
# ourself, this is just a normal method:
def get_open_func(self):
def py_open_func( string ):
python_format.__self_ref.append( self )
return ctypes.addressof(self)
return OPENFUNC( py_open_func )
OPENFUNC = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_char_p)
# delay init required because `read_info` requires a forward declaration:
python_format._fields_ = (
('open', OPENFUNC),
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.