我试图将列表作为输入。我也用Google搜索了。 我在命令提示符下构建并安装了我的C-extension模块。我能够导入模块,但它确实显示了很少的警告。它告诉我的是......
D:\Python\c\summa>py setup.py build
running build
running build_ext
building 'saiyanism' extension
C:\Program Files\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.2612
8\bin\HostX86\x86\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IE:\python\include
-IE:\python\include "-IC:\Program Files\Microsoft Visual Studio\2017\Community\
VC\Tools\MSVC\14.13.26128\include" "-IC:\Program Files\Windows Kits\NETFXSDK\4.6
.1\include\um" "-IC:\Program Files\Windows Kits\10\include\10.0.16299.0\ucrt" "-
IC:\Program Files\Windows Kits\10\include\10.0.16299.0\shared" "-IC:\Program Fil
es\Windows Kits\10\include\10.0.16299.0\um" "-IC:\Program Files\Windows Kits\10\
include\10.0.16299.0\winrt" "-IC:\Program Files\Windows Kits\10\include\10.0.162
99.0\cppwinrt" /TcH.c /Fobuild\temp.win32-3.6\Release\H.obj
H.c
H.c(63): warning C4133: 'function': incompatible types - from 'PyObject *' to 'c
onst char *const '
H.c(117): warning C4113: 'PyObject *(__cdecl *)(PyObject *)' differs in paramete
r lists from 'PyCFunction'
C:\Program Files\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.2612
8\bin\HostX86\x86\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID
=2 /MANIFESTUAC:NO /LIBPATH:E:\python\libs /LIBPATH:E:\python\PCbuild\win32 "/LI
BPATH:C:\Program Files\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.1
3.26128\lib\x86" "/LIBPATH:C:\Program Files\Windows Kits\NETFXSDK\4.6.1\lib\um\x
86" "/LIBPATH:C:\Program Files\Windows Kits\10\lib\10.0.16299.0\ucrt\x86" "/LIBP
ATH:C:\Program Files\Windows Kits\10\lib\10.0.16299.0\um\x86" /EXPORT:PyInit_sai
yanism build\temp.win32-3.6\Release\H.obj /OUT:build\lib.win32-3.6\saiyanism.cp3
6-win32.pyd /IMPLIB:build\temp.win32-3.6\Release\saiyanism.cp36-win32.lib
Creating library build\temp.win32-3.6\Release\saiyanism.cp36-win32.lib and ob
ject build\temp.win32-3.6\Release\saiyanism.cp36-win32.exp
Generating code
d:\python\c\summa\h.c(112) : warning C4715: 'sumlist': not all control paths ret
urn a value
Finished generating code
D:\Python\c\summa>
#include "Python.h"
static PyObject* saiyanism(PyObject* self,PyObject* args)
{
const char* saiyajin;
if(!(PyArg_ParseTuple(args,"s",&saiyajin))) return NULL;
printf("Hello,%s.Glad to see a saiyan.\n",saiyajin);
Py_RETURN_NONE;
}
static PyObject* UI(PyObject* self) //,PyObject* args)
{/*code here works properly*/
Py_RETURN_NONE;
}
static PyObject* set_all(PyObject* self,PyObject *target)//PyLong_FromLong(PyObject *item))
{
/*some code and this too works*/
Py_RETURN_NONE;
}
static PyObject* sumlist(PyObject* self,PyObject *args)
{
PyObject *pList;
PyObject *pItem;
Py_ssize_t n;
int i;
if (!PyArg_ParseTuple(args, "O!", &PyList_Type, &pList)) {
PyErr_SetString(PyExc_TypeError, "parameter must be a list.");
return NULL;
}
n = PyList_Size(pList);
for (i=0; i<n; i++) {
pItem = PyList_GetItem(pList, i);
if(!PyLong_Check(pItem)) {
PyErr_SetString(PyExc_TypeError, "list items must be integers.");
printf("%ld",PyLong_AsLong(pItem));
Py_RETURN_NONE;
}
}
}
static PyMethodDef sups[]=
{
{"saiyanism",saiyanism,METH_VARARGS,""},
{"UI",UI,METH_NOARGS,""},
{"set_all",set_all,METH_VARARGS,""},
{"sumlist",sumlist,METH_VARARGS,""},
{NULL,NULL,0,NULL}
};
static struct PyModuleDef saiyanismdef={
PyModuleDef_HEAD_INIT,
"saiyanism",
"sad",
-1,
sups
};
PyMODINIT_FUNC PyInit_saiyanism(void){
Py_Initialize();
return PyModule_Create(&saiyanismdef);
}
当我尝试导入并使用python列表运行我的sumlist函数作为参数Python立即停止。
以下是image
我刚开始学习这个,所以如果我的课程有错误,请指出。
我应该以不同的方式传递争论吗?我哪里错了?
答案 0 :(得分:0)
我可以在sumlist
中看到至少两个错误:
在代码中
if(!PyLong_Check(pItem)) {
PyErr_SetString(PyExc_TypeError, "list items must be integers.");
printf("%ld",PyLong_AsLong(pItem));
Py_RETURN_NONE;
}
返回None
(Python对象)而不是C NULL
指针,用于指示错误。您还尝试使用pItem
打印PyLong_AsLong
- 您对[{1}}了解的一件事就是它不是pItem
!
(导致您出现问题的主要错误)当long
结束而没有任何错误时会发生什么?你没有返回任何东西,Python希望你返回sumlist
。这就是警告告诉你的事情:
警告C4715:'sumlist':并非所有控制路径都返回值
这里的快速修复是将PyObject*
添加到函数的末尾,以将Py_RETURN_NONE
对象返回给Python。但是,您可能想要做一些更有用的事情....