C中的Python扩展 – 来回传递列表
我有一个C库,用于包含以下功能的设备:
int GetDevInfo(int *devices); 在这种情况下,devices是一个整数数组,可能已经定义如下: int devices[10] 该函数将循环通过硬件总线以查找活动设备.在找到它们时,它会将设备编号放入设备[]中的下一个可用位置.所以例如在扫描之前: devices = {0,0},全局变量DeviceCount知道有0个活动变量.该功能发挥其神奇作用,并决定设备5和8是否有效.那么: devices = {5,8,DeviceCount知道有2个. 我希望Python函数类似于: devices = list(range(64)) for i in range(0,len(devices)): # Set them all to 0 devices[i] = 0 DeviceCount = 0 # Now we'll update it DeviceCount = myModule.GetDevInfo(DeviceCount,devices) 返回时,DeviceCount可能设置为2,设备如下所示: [5,0] 我已经编写了其他包装器的代码,这些包装器传入整数并传回字符串,但我没有找到任何网络智慧来帮助我.我知道如果我做这样的事情: def f(a,data): a.append(data) l = [1,2,3] f(l,4) 我得到l = [1,3,4]但是如何使用Python包装器将效果变为C并不明显. 提前做好准备,对于任何可以提供帮助的人来说都有披萨钱! 解决方法
好的,稍后阅读网页和大量的反复试验……
static PyObject* LMS_module_timesTwo(PyObject *self,PyObject *args) { int i,numLines; PyObject *listObj,*numObj; unsigned int n; n = PyTuple_Size(args); if (1 == n) { /* argument 1 should be an list of integers */ listObj = PyTuple_GetItem(args,0); /* get the number of elements in the list */ numLines = PyList_Size(listObj); /* should raise an error here. */ if (numLines < 0) return NULL; /* Not a list */ for (i=0; i<numLines; i++) { numObj = PyList_GetItem(listObj,i); n = PyLong_AsUnsignedLong(PyNumber_Long(numObj)); // This is the action - multiply by 2 for the test case n = n * 2; PyList_SetItem(listObj,i,Py_BuildValue("i",n)); } } else { PyErr_SetString(PyExc_ValueError,"Function expects one argument"); return NULL; } return PyLong_FromLong(1); } 使用该代码作为扩展的一部分(我遗漏了其他结构位,但可以发布整个事情,如果有人需要它们),这作为Python 3测试程序,它工作膨胀! import my_module testlist = [1,4,5] print("List before multiplying = ",testlist) my_module.timesTwo(testlist) print("List after multiplying = ",testlist) 感谢所有阅读此内容并思考答案的人.我的实际代码也有错误检查和内容,我不关心如果程序传入垃圾会发生什么,因为这是一个非常具体的功能. 最初提出的问题是将列表作为数组传递给C函数,因此这更接近最终: static PyObject* LMS_module_timesTwo(PyObject *self,*numObj; unsigned int n; unsigned int carray[64]; // this has to be big enough! n = PyTuple_Size(args); if (1 == n) { /* argument 1 should be an list of integers */ listObj = PyTuple_GetItem(args,i); carray[i] = PyLong_AsUnsignedLong(PyNumber_Long(numObj)); } /* Do the actual work on the array */ someCfunction(carry); // this modifies the array somehow for (i=0; i<numLines; i++) { PyList_SetItem(listObj,carray[i])); } } else { PyErr_SetString(PyExc_ValueError,"Function expects one argument"); return NULL; } return PyLong_FromLong(1); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |