自引用类:来自C的具体python类

2024-06-09 17:58:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试设计一个可以在Python中轻松扩展的C接口(使用ctypes)。我在C中使用了natural idiom

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);
};

如果我想直接从C扩展这个接口,它会很好地工作:

^{pr2}$

但我真正想做的是使用ctypes从Python实现这个接口。以下是我目前所掌握的情况:

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

实际上,我正在努力定义OPENFUNC的原型。技术上应该是:

OPENFUNC    = ctypes.CFUNCTYPE(ctypes.POINTER(python_format), ctypes.c_char_p)

不过,我需要先定义python_format,这又需要{}的定义。在

加分:什么是实际的函数实现?例如:

def func( str ): return None

或者

def func( str ): i = python_format(); return ctypes.pointer(i)

两者都给了我:

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

这和另一个issue有关吗?如果是这样的话,我应该改变我最初的C设计,因为我将无法从回调返回指向python_format实例的指针?在


Tags: pyselfformatreaddefopenctypesdelete
2条回答

ctypes.Structure的文档中,它解释了如何执行此操作:

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

这意味着您可以添加:

class python_format(ctypes.Structure):  # forward declaration
    pass

然后在定义OPENFUNC(以及其他函数类型)之后:

^{pr2}$

因此,能够定义python_format._fields_

python_format._fields_ = (
    ('can_open',  CANOPENFUNC),
    ('open',      OPENFUNC),
    ('delete',    DELETEFUNC),
    ('read',      READFUNC),
  )

下面是一个基于您的代码的更完整的示例:

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()

为了得到一个规范的答案,我将回答我自己的问题,这要感谢@eryksun的指导。在

因此,首先,虽然这在documentation中并不清楚,但不能从回调函数返回复杂类型。因此,不能映射C函数指针:

struct format {
    struct format * (*open)(const char *filename);
};

^{pr2}$

上面的代码可以很好地编译,但是在运行时,可以得到:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: invalid result type for callback function

长话短说的答案是:

The TypeError message you get when trying to use a non-simple type as the result of a callback is less than helpful. A callback's result type has to have a setfunc in its StgDictObject (a ctypes extension of the regular PyDictObject). This requirement restricts you to using a simple type such as c_void_p[...]

因此,到今天为止,在issue 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),
  )

相关问题 更多 >