Tag: ctypes

使用Python的CFFI并排除系统头

我正在尝试使用Python的CFFI开发Python绑定到用C编写的科学模型.CFFI文档有点稀疏,我陷入了cdef阶段。 到目前为止,我的流程遵循以下步骤: 预处理头文件: gcc -E -gcc -std=c99 -E -P src/my_c_interface.c -I./include/ -I../shared/include/ > header.txt 这将生成一个文本文件,其中包含include/目录中头文件中包含的所有C声明。 它还包括标准库的声明(我很确定这是我的问题所在)。 header.txt看起来像这样(完整的header.txt在这里 ): 从系统标题开始: typedef float float_t; typedef double double_t; extern int __math_errhandling(void); extern int __fpclassifyf(float); extern int __fpclassifyd(double); extern int __fpclassifyl(long double); extern __inline __attribute__((__gnu_inline__)) __attribute__ ((__always_inline__)) int __inline_isfinitef(float); extern __inline __attribute__((__gnu_inline__)) __attribute__ 并以我的标题中定义的片段结束: FILE *LOG_DEST; void finalize_logging(void); void […]

CTRL + C不会在Python中使用CTYPES中断对共享库的调用

当调用在C共享库(动态库)中执行的循环时,Python将不会收到KeyboardInterrupt,并且没有任何内容会响应(或处理)CTRL + C. 我该怎么办?

创建inheritance外部模块类的“generics”类的最佳方法

在ctypes模块中,底层c_*类型执行C样式截断而不引发任何exception。 例如, c_uint8(0x120)与c_uint8(0x20)相同,与c_uint8(32)相同。 我希望创建inheritancectypes类型的类,如果类型超出其范围,将引发自定义exception。 这是我到目前为止所提出的: from ctypes import * class C_U8(c_uint8): def __init__(self, value): if value 0xFF: raise Exception(“Nice try! Out of range”) 虽然这可以按预期工作,但我很快意识到,对于每个ctypes类型来说,这将成为一个冗余的类。 我想知道是否有更好的方法来执行此操作,例如创建一个从ctypes模块inheritance所有类型的generics类,并相应地检查每个类型?

在python中检测C类型限制(“limits.h”)?

我在python中做了一个小测试程序,在很多情况下测试一些C函数。 这个python程序使用ctypes导入和使用我的C函数。 但我也想测试最小值和最大值的情况(例如limits.h的ULLONG_MAX )。 但由于其中一些限制可能取决于系统,我宁愿避免在我的程序中对其进行硬编码; 我宁愿动态地得到它。 是否有可能在python中获得这些限制值?

python与c ++使用ctypes

由于我还是新手,我遇到了一些问题,这是我的C ++代码: #include #define DLLEXPORT extern “C” __declspec(dllexport) DLLEXPORT PyObject *Add(PyObject *pSelf, PyObject *pArgs) { int s,d; if(!PyArg_ParseTuple(pArgs, “ii” , &s, &d)) { PyErr_SetString(PyExc_TypeError, “Add() invalid parameter”); return NULL; } return Py_BuildValue(“i”, s + d); } 和Python代码: import ctypes MyDll = ctypes.cdll.LoadLibrary(r”PyToCppTest.dll”) jj = MyDll.Add(1,2) 运行上面的Python代码时出错: OSError:exception:访问冲突读取0x000000000000000A 我希望将数据从Python转换为C ++,然后将其转换为C ++。

(ctypes)msvcrt.printf并在python中打印

我正在学习使用msvcrt.dll,python 2.7中的ctypes使用printf函数。 我观察到当使用msvcrt.printf(“x”)和通常的python print(“y”)函数时,“y”在“x”之前打印,而在“msvcret.printf”之后放置print(“y”)语句。 “X”) 。 任何人都可以解释为什么会这样吗? 这是我的代码: from ctypes import * msvcrt = cdll.msvcrt msvcrt.printf(“hello world!”) print(“abcd”) 输出: abcd hello world

使用ctypes从Python中的C函数返回的结构中访问数据

我知道这个问题已经得到了处理,但我找不到任何对我有用的东西,所以我猜我的问题与其他问题略有不同。 基本上,我所做的是使用ctypes将C函数包装到python代码中。 我的目标是在C函数中计算一些数量,将它们存储到一个结构中,然后在Python中返回结构,我可以在其中读取结构中的所有不同数据。 所以: 要在python中定义我的结构,我使用: class BITE(ctypes.Structure): _fields_ = [ (“lum”, ctypes.c_int), (“suce”, ctypes.c_int) ] 我这样编译: sub.call([“gcc”,”-shared”,”-Wl,-install_name,ex.so”,”-o”,”ex.so”,”-fPIC”,”ex.c”]) lum = ctypes.CDLL(‘ex.so’) lum = lum.lum 我声明参数和结果类型如下: lum.restype = ctypes.POINTER(BITE) lum.argtypes = [ctypes.POINTER(ctypes.c_float),ctypes.c_int,ctypes.POINTER(ctypes.c_float),ctypes.c_int,ctypes.POINTER(ctypes.c_float),ctypes.POINTER(ctypes.c_float),ctypes.c_int,ctypes.POINTER(ctypes.c_float),ctypes.c_int,ctypes.POINTER(ctypes.c_float),ctypes.POINTER(ctypes.c_float),ctypes.POINTER(ctypes.c_float),ctypes.POINTER(ctypes.c_float),ctypes.c_float,ctypes.c_float,ctypes.c_float,ctypes.c_float,ctypes.c_float] 然后,我在python代码中调用C函数“lum” out = lum(all the different variables needed) 问题从这里开始。 我有我的结构,但无法读取存储在每个字段中的数据。 我找到的部分解决方案是: out = out[0] 但是,在做的时候 print(out.suce) 我有一个分段错误11.不知道为什么。 我试图理解如何使用create_string_buffer,但我真的不明白它是如何工作的以及它应该做什么。 而且,我试图用作黑盒子,但仍然无效。 我还尝试了其他线程中提到的一些其他解决方案,例如使用out.contents或类似的东西,但它没有.contents属性。 也不是out.suce.value,这也是我在绝望的研究中看到的东西。 当然,我在C代码中检查了结构是否存在以及结构的每个字段是否存在,并且其中包含正确的数据。 谢谢。

ctypes – 将带有指针的结构传递给另一个结构

在我的C代码中我有: typedef struct{ int info1; int info2; MoreData* md; } BasicData; typedef struct{ int extinfo[100]; char stuff[100]; } MoreData; 现在我有一个C库函数,它将BasicData作为参数,我想从Python调用它。 为此,我构建了一个ctypes类: class BasicData(Structure): _fields_ = [(“info1”, c_int), (“info2”, c_int), (“md”, ??????)] 我的问题是我要填写什么? 空间能够将此结构传递给C函数(或者我需要一些完全不同的东西?)。 MoreData只是在计算过程中使用,我不需要在我的Python代码中读取它,因此为它分配内存将从C库级别处理。 我只需要传递一个具有正确指针类型的BasicData结构。

将FILE *传递给Python / ctypes中的函数

我有一个库函数(用C编写),它通过将输出写入FILE *来生成文本。 我想在Python(2.7.x)中使用创建临时文件或管道的代码将其包装,将其传递给函数,从文件中读取结果,并将其作为Python字符串返回。 这是一个简单的例子来说明我的目标: /* Library function */ void write_numbers(FILE * f, int arg1, int arg2) { fprintf(f, “%d %d\n”, arg1, arg2); } Python包装器: from ctypes import * mylib = CDLL(‘mylib.so’) def write_numbers( a, b ): rd, wr = os.pipe() write_fp = MAGIC_HERE(wr) mylib.write_numbers(write_fp, a, b) os.close(wr) read_file = os.fdopen(rd) res = read_file.read() read_file.close() return […]

使用ctypes传递数组

我有一个C函数 void read_FIFO_AI0(int16_t** input, size_t size, NiFpga_Session* session, NiFpga_Status* status) { *input = (int16_t*) malloc (size*sizeof(int16_t)); // function that populates the array *input } 填充数组“* input”。 现在我想将该数组中的数据传递给python进行进一步处理。 我尝试使用ctypes来做到这一点: def read_FIFO_AI0(size,session,status): _libfpga.read_FIFO_AI0.argtypes = [POINTER(ARRAY(c_int16, size)), c_int, POINTER(c_uint32), POINTER(c_int32)] _libfpga.read_FIFO_AI0.restype = None values = (c_int16*size)() _libfpga.read_FIFO_AI0(byref(values),size,byref(session),byref(status)) return values 代码执行但我在数组中得到错误的结果。 当我尝试在CI中使用C函数时获得正确的结果: size_t size=20; int16_t* input; read_FIFO_AI0(&input, size, &session, […]