【发布时间】:2019-08-30 22:12:46
【问题描述】:
我最近开始使用 Python/C API 来使用 C 代码为 Python 构建模块。我一直在尝试将 Python 数字列表传递给 C 函数,但没有成功:
asdf_module.c
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <Python.h>
int _asdf(int low, int high, double *pr)
// ...
for (i = 0; i < range; i++)
{
printf("pr[%d] = %f\n", i, pr[i]);
}
// ...
return 0;
}
static PyObject*
asdf(PyObject* self, PyObject* args)
{
int low, high;
double *pr;
// maybe something about PyObject *pr ?
if (!PyArg_ParseTuple(args, "iiO", &low, &high, &pr))
return NULL;
// how to pass list pr to _asdf?
return Py_BuildValue("i", _asdf(low, high, pr));
}
static PyMethodDef AsdfMethods[] =
{
{"asdf", asdf, METH_VARARGS, "..."},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC
initasdf(void)
{
(void) Py_InitModule("asdf", AsdfMethods);
}
使用 setup.py 构建模块:
from distutils.core import setup, Extension
module1 = Extension('asdf', sources = ['asdf_module.c'])
setup (name = 'asdf',
version = '1.0',
description = 'This is a demo package',
ext_modules = [module1])
使用 test_module.py 中的模块:
import asdf
print asdf.asdf(-2, 3, [0.7, 0.0, 0.1, 0.0, 0.0, 0.2])
但是,我得到的输出是:
pr[0] = 0.000000
pr[1] = 0.000000
pr[2] = 0.000000
pr[3] = 0.000000
pr[4] = 0.000000
pr[5] = -nan
另外,不是 _asdf 返回 0,而是如何返回 n 值的数组(其中 n 是一个固定数字)?
【问题讨论】:
-
一方面,
o参数用于获取PyObject *。所以你需要修复它,并继续解析。 -
如果您可以发布单独的问题而不是将您的问题合并为一个问题,则最好。这样,它可以帮助人们回答您的问题,也可以帮助其他人至少寻找您的一个问题。如果您还有其他问题,我建议您前往右上角的小
按钮。谢谢!