辩论

2024-05-23 09:39:00 发布

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

我正在尝试创建一个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)) 

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

如何接受位置参数?你知道吗


Tags: testbuildmessageincludewindowslibfilesprogram
2条回答

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

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"应该是-1in functions。你知道吗

你有没有在找不完整的东西?你知道吗

这个库可以运行C代码,也许可以帮助你。您可以在官方Python文档的这个页面中获得更多关于的信息:https://docs.python.org/3/extending/building.html

相关问题 更多 >

    热门问题