gpt4 book ai didi

python - 接受争论

转载 作者:行者123 更新时间:2023-11-30 14:47:43 25 4
gpt4 key购买 nike

我正在尝试创建一个可以接受争论的 Python 扩展。这是我的脚本:

#include <Python.h>

static PyObject * _message(PyObject *self, PyObject *args) {
char *title, *message;

if (!PyArg_ParseTuple(args, &title, &message))
return NULL; // Throw an error

return Py_BuildValue("");
}


static PyMethodDef methods[] = {
{"Message", (PyCFunction) _message, METH_VARARGS, "Message(title, message) Take a message"},
{NULL, NULL, 0, NULL}
};


static struct PyModuleDef functions = {
PyModuleDef_HEAD_INIT,
"test",
"Take a message",
"-1",
methods

};

PyMODINIT_FUNC PyInit_test(void) {

PyObject *module = PyModule_Create(&functions);

return module;
}

安装.py:

from setuptools import setup, Extension

module = Extension (
"test",
sources = ['test.c'])


setup(
name = "test",
version = "1.0",
description = "Take message input",
author = "Simon",
ext_modules = [module])

但是,当我尝试编译它时,出现错误:

C:\Users\Simon\Desktop\PyTest>python setup.py build
running build
running build_ext
building 'test' extension
C:\Program Files (x86)\Microsoft Visual Studio\2017\WDExpress\VC\Tools\MSVC\14.14.26428\bin\HostX86\x86\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MT -IC:\Users\Simon\AppData\Local\Programs\Python\Python36-32\include -IC:\Users\Simon\AppData\Local\Programs\Python\Python36-32\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\WDExpress\VC\Tools\MSVC\14.14.26428\include" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\cppwinrt" /Tctest.c /Fobuild\temp.win32-3.6\Release\test.obj
test.c
test.c(2): warning C4067: unexpected tokens following preprocessor directive - expected a newline
test.c(10): warning C4047: 'function': 'const char *' differs in levels of indirection from 'char **'
test.c(10): warning C4024: 'PyArg_ParseTuple': different types for formal and actual parameter 2
test.c(23): warning C4047: 'initializing': 'Py_ssize_t' differs in levels of indirection from 'char [3]'
test.c(42): warning C4047: 'function': 'const char *' differs in levels of indirection from 'int'
test.c(42): warning C4024: 'PyModule_AddStringConstant': different types for formal and actual parameter 3
C:\Program Files (x86)\Microsoft Visual Studio\2017\WDExpress\VC\Tools\MSVC\14.14.26428\bin\HostX86\x86\link.exe /nologo /INCREMENTAL:NO /LTCG /nodefaultlib:libucrt.lib ucrt.lib /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:C:\Users\Simon\AppData\Local\Programs\Python\Python36-32\libs /LIBPATH:C:\Users\Simon\AppData\Local\Programs\Python\Python36-32\PCbuild\win32 "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2017\WDExpress\VC\Tools\MSVC\14.14.26428\lib\x86" "/LIBPATH:C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\lib\um\x86" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\ucrt\x86" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86" test.lib /EXPORT:PyInit_test build\temp.win32-3.6\Release\test.obj /OUT:build\lib.win32-3.6\test.cp36-win32.pyd /IMPLIB:build\temp.win32-3.6\Release\test.cp36-win32.lib
Creating library build\temp.win32-3.6\Release\test.cp36-win32.lib and object build\temp.win32-3.6\Release\iup.cp36-win32.exp
Generating code
Finished generating code

我很确定我的代码的其余部分是正确的,除了在以下位置获取参数:

static PyObject * _message(PyObject *self, PyObject *args) {
char *title, *message;

if (!PyArg_ParseTuple(args, &title, &message))

扩展已创建,但是当我尝试运行它时,它崩溃了。

如何获取位置参数?

最佳答案

您需要在第二个参数中指定参数类型。例如:

if (!PyArg_ParseTuple(args,"zz", &title, &message))

其中 z 表示 C 风格的零分隔字符串,"zz" 表示有两个字符串。这些被称为说明符,其中有很多,以下是一些最常见的:

i/I     Signed/unsigned int
d/D Signed/unsigned double
s C-style string (char *)
z C-style string or None
y bytes object
O Generic Python object (oh)
O! Typed Python object (oh!)

此外,函数中的“-1”应该是-1

关于python - 接受争论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50991334/

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