【发布时间】:2015-09-29 01:24:30
【问题描述】:
如果我编写一个接受单个无符号整数 (0 - 0xFFFFFFFF) 的函数,我可以使用:
uint32_t myInt;
if(!PyArg_ParseTuple(args, "I", &myInt))
return NULL;
然后从 python,我可以传递 int 或 long。
但是如果我得到一个整数列表呢?
uint32_t* myInts;
PyObject* pyMyInts;
PyArg_ParseTuple(args, "O", &pyMyInts);
if (PyList_Check(intsObj)) {
size_t n = PyList_Size(v);
myInts = calloc(n, sizeof(*myInts));
for(size_t i = 0; i < n; i++) {
PyObject* item = PyList_GetItem(pyMyInts, i);
// What function do I want here?
if(!GetAUInt(item, &myInts[i]))
return NULL;
}
}
// cleanup calloc'd array on exit, etc
具体来说,我的问题是处理:
- 包含
ints 和longs 混合的列表 - 分配给 uint32 时检测溢出
【问题讨论】:
标签: python c python-2.x cpython