Python/capi结果不显示

2024-10-04 13:29:18 发布

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

我想在Python中集成C模块,因此我选择了接口Python.h。一切编译没有错误和警告,所以我不明白是什么问题。你知道吗

C侧:

#include <python3.5m/Python.h>
...
#define PyInt_AsLong(x) (PyLong_AsLong((x)))
typedef PyObject* Py;

static Py getSumma(Py self, Py args){
  Py nums;
  if (!PyArg_ParseTuple(args, "O", &nums)){
    return NULL;
  }
  size_t numsAmount = PyList_Size(args);
  int32_t summa = 0;

  for (size_t i = 0; i < numsAmount; i++){
    Py temp = PyList_GetItem(nums, i);
    int32_t num = PyInt_AsLong(temp);
    summa += num;
  }
  return Py_BuildValue("l", summa);
}

static PyMethodDef moduleMethods[] = {
  {"getSumma", (PyCFunction)getSumma, METH_VARARGS, NULL},
  {NULL, NULL, 0, NULL}
};

static PyModuleDef SummaLogic = {
  PyModuleDef_HEAD_INIT,
  "SummaLogic",
  "",
  -1,
  moduleMethods
};

PyMODINIT_FUNC PyInit_SummaLogic(void){
  return PyModule_Create(&SummaLogic);
}

你知道吗设置.py地址:

from distutils.core import setup, Extension

SummaLogic = Extension("SummaLogic", sources=['SummaLogic.c'])
setup(ext_modules=[SummaLogic])

Python方面:

from SummaLogic import getSumma

if __name__ == "__main__":
    a = [1, 2, 3]
    b = getSumma(a)
    print(b)

这似乎是对的,但当我在终端启动它-什么都没有发生,只是挂着没有任何活动。我会错过什么?你知道吗


Tags: pysizereturnifargsstaticnullpylist
1条回答
网友
1楼 · 发布于 2024-10-04 13:29:18

它归结为^{},您不需要在那里检查错误。你知道吗

您可能想在nums上使用它,而不是将args用作参数。但是你用在args上,发生了一件非常有趣的事情:

  • argstuple
  • 因此PyList_Size失败并返回^{}
  • -1转换成一个无符号size_t-1可能会产生一个非常大的数字,可能2**64-1
  • 因此,您的迭代运行“非常长的时间”,因为迭代2**64-1项需要相当长的时间(除了所有的越界内存访问)。你知道吗

快速解决方法是使用:

Py_ssize_t listlength = PyList_Size(nums);  /* nums instead of args */
if (listlength == -1) {  /* check for errors */
    return NULL;
}
size_t numsAmount = (size_t)listlength /* cast to size_t only after you checked for errors */

但是,您应该检查错误条件是什么,并在每次pythoncapi函数调用之后测试它们,否则您将得到许多未定义的行为。另外,我可能会坚持使用已定义的返回类型,而不是int32_tPyInt_AsLong返回long,因此您可能会在那里得到奇怪的转换错误!),size_t。。。对于经常编写C扩展的人来说,typedef PyObject* Py;使事情变得非常棘手。你知道吗

相关问题 更多 >