如何使用 python 中的 ctypes 创建包含 mpfr.h 类型的相应结构?
How can I create a corresponding struct containing mpfr.h types using ctypes in python?
我有一个 C API,其函数适用于以下 C 结构:
typedef struct s_t{
mpq_ptr mpq;
mpfr_ptr mpfr;
} s_t;
我正在为这个 C API 构建一个 python 包装器,所以我需要将这个结构定义为一个 class 继承自 'Structure' class 在 ctypes 中,然后给 _field_ 赋值。但是 mpq_ptr、mpfr_ptr 是 mpfr.h 的类型,并且没有对应的 ctypes 类型。有没有办法解决这个问题?
我想在python中得到的是这样的:
class S_t(Structure):
_fields_ = [('mpq', CTYPEFOR(mpq_ptr)),('mpfr', CTYPEFOR(mpfr_ptr))]
这样我就可以在 python 中创建此类对象并将它们传递给以 s_t 作为输入参数的相应 C 函数。
我通过跟踪 mpfr.h 和 gmp.h 文件中的所有类型定义并在 python 中创建所有必要的结构来解决这个问题:
class Mpz(Structure):
# define struct manually
class Mpq(Structure):
# define struct manually
class Mpfr(Structure):
# define struct manually
我有一个 C API,其函数适用于以下 C 结构:
typedef struct s_t{
mpq_ptr mpq;
mpfr_ptr mpfr;
} s_t;
我正在为这个 C API 构建一个 python 包装器,所以我需要将这个结构定义为一个 class 继承自 'Structure' class 在 ctypes 中,然后给 _field_ 赋值。但是 mpq_ptr、mpfr_ptr 是 mpfr.h 的类型,并且没有对应的 ctypes 类型。有没有办法解决这个问题?
我想在python中得到的是这样的:
class S_t(Structure):
_fields_ = [('mpq', CTYPEFOR(mpq_ptr)),('mpfr', CTYPEFOR(mpfr_ptr))]
这样我就可以在 python 中创建此类对象并将它们传递给以 s_t 作为输入参数的相应 C 函数。
我通过跟踪 mpfr.h 和 gmp.h 文件中的所有类型定义并在 python 中创建所有必要的结构来解决这个问题:
class Mpz(Structure):
# define struct manually
class Mpq(Structure):
# define struct manually
class Mpfr(Structure):
# define struct manually