gpt4 book ai didi

Python C Api 无提示地失败

转载 作者:行者123 更新时间:2023-11-30 19:50:33 25 4
gpt4 key购买 nike

我正在尝试为我的 C 应用程序创建一个 Python 接口(interface),但是当我运行它时,shell 只是重置并且没有打印任何错误。这是我的代码:

#include <Python.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

static PyObject *method_doubletuple(self, args)
PyObject *self;
PyObject *args;
{
PyObject *ret = PyTuple_New(16);
for (int c = 0; c < 16; c++) {
PyObject *newValue = PyNumber_Multiply(PyTuple_GetItem(args, c), Py_BuildValue("%u", (int)2));
PyTuple_SET_ITEM(ret, c, newValue);
}
return ret;
}

static PyMethodDef PyTestMethods[] = {
{"system", method_doubletuple, METH_VARARGS, "Doubles the value of a tuple with 16 elements"},
{NULL, NULL, 0, NULL}
};

static struct PyModuleDef pytestmodule = {
PyModuleDef_HEAD_INIT,
"pytest",
"pytest",
-1,
PyTestMethods
};

PyMODINIT_FUNC PyInit_pytest(void) {
return PyModule_Create(&pytestmodule);
}
C:\Users\jotje\Desktop\Chess\SampleCModule>python setup.py install
running install
running build
running build_ext
building 'pytest' extension
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Users\jotje\AppData\Local\Programs\Python\Python36\include -IC:\Users\jotje\AppData\Local\Programs\Python\Python36\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\cppwinrt" /Tcaddarray.c /Fobuild\temp.win-amd64-3.6\Release\addarray.obj
addarray.c
addarray.c(20): warning C4113: 'PyObject *(__cdecl *)()' differs in parameter lists from 'PyCFunction'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:C:\Users\jotje\AppData\Local\Programs\Python\Python36\libs /LIBPATH:C:\Users\jotje\AppData\Local\Programs\Python\Python36\PCbuild\amd64 "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.16299.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.16299.0\um\x64" /EXPORT:PyInit_pytest build\temp.win-amd64-3.6\Release\addarray.obj /OUT:build\lib.win-amd64-3.6\pytest.cp36-win_amd64.pyd /IMPLIB:build\temp.win-amd64-3.6\Release\pytest.cp36-win_amd64.lib
Creating library build\temp.win-amd64-3.6\Release\pytest.cp36-win_amd64.lib and object build\temp.win-amd64-3.6\Release\pytest.cp36-win_amd64.exp
Generating code
Finished generating code
running install_lib
copying build\lib.win-amd64-3.6\pytest.cp36-win_amd64.pyd -> C:\Users\jotje\AppData\Local\Programs\Python\Python36\Lib\site-packages
running install_egg_info
Removing C:\Users\jotje\AppData\Local\Programs\Python\Python36\Lib\site-packages\pytest-1.0.0-py3.6.egg-info
Writing C:\Users\jotje\AppData\Local\Programs\Python\Python36\Lib\site-packages\pytest-1.0.0-py3.6.egg-info

C:\Users\jotje\Desktop\Chess\SampleCModule>python
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pytest
>>> pytest.doubletuple((4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4))

C:\Users\jotje\Desktop\Chess\SampleCModule>

如果有人知道如何获取错误消息或日志或可以指出问题的内容,并且可能告诉我我做错了什么,那就太好了。

最佳答案

问题是这个循环:

for (int c = 0; c < 16; c++) {
PyObject *newValue = PyNumber_Multiply(PyTuple_GetItem(args, c), Py_BuildValue("%u", (int)2));
PyTuple_SET_ITEM(ret, c, newValue);
}

通过调用 PyTuple_GetItem(args, c),您将获取提供给函数的第 c 个参数,但您只提供一个参数。包含 float 的元组。您可能打算做这样的事情:

PyObject *input = PyTuple_GetItem(args, 0); 
for (int c = 0; c < 16; c++) {
PyObject *newValue = PyNumber_Multiply(PyTuple_GetItem(input, c), Py_BuildValue("%u", (int)2));
PyTuple_SET_ITEM(ret, c, newValue);
}

关于Python C Api 无提示地失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59547905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com