争论

时间:2018-06-22 15:35:04

标签: python c function arguments python-c-api

我正在尝试创建一个需要争论的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;
}

Setup.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)) 

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

如何接受位置参数?

2 个答案:

答案 0 :(得分:1)

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

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

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

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中的functions

答案 1 :(得分:0)

您是否正在寻找disutils?

该库可以运行C代码,也许可以为您提供帮助。您可以在Python官方文档的以下页面中找到有关的更多信息:https://docs.python.org/3/extending/building.html