搜索有关如何使用PyArg_ParseTupleAndKeywords
的示例,我发现了以下问题:
他们俩都使用static char* kwlist[] = {"a", "b", NULL}
static int PyClass_init(PyClass* self, PyObject* args, PyObject* kwargs) {
char* path;
char* regex;
static char* kwlist[] = {"", "", NULL};
if( !PyArg_ParseTupleAndKeywords( args, kwargs, "s|s", kwlist, &path, ®ex ) ) {
return -1;
}
// other code ...
return 0;
}
在language = "c++"
上用setup.py
进行编译,然后在-std=c++11
上进行编译会引发以下警告:
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O0 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -Isource -I/usr/include/python3.6m -c source/test.cpp -o build/temp.linux-x86_64-3.6/source/test.o -O0 -g -ggdb -std=c++11 -fstack-protector-all
source/test.cpp: In function ‘int PyClass_init(PyClass*, PyObject*, PyObject*)’:
source/test.cpp:41:42: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
static char* kwlist[] = {"a", "b", NULL};
^
在搜索此错误时,我发现此问题Why is conversion from string constant to 'char*' valid in C but invalid in C++即将解决,但将其应用为static char* const kwlist[] = {"a", "b", NULL};
可以保持/升级引入错误的警告:
source/test.cpp:41:50: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
static char* const kwlist[] = {"a", "b", NULL};
^
source/test.cpp:41:50: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
source/test.cpp:43:89: error: invalid conversion from ‘char* const*’ to ‘char**’ [-fpermissive]
if( !PyArg_ParseTupleAndKeywords( args, kwargs, "s|s", kwlist, &path, ®ex ) ) {
^
In file included from /usr/include/python3.6m/Python.h:117:0,
from source/test.cpp:3:
/usr/include/python3.6m/modsupport.h:17:41: note: initializing argument 4 of ‘int _PyArg_ParseTupleAndKeywords_SizeT(PyObject*, PyObject*, const char*, char**, ...)’
#define PyArg_ParseTupleAndKeywords _PyArg_ParseTupleAndKeywords_SizeT
^
/usr/include/python3.6m/modsupport.h:17:41: note: in definition of macro ‘PyArg_ParseTupleAndKeywords’
#define PyArg_ParseTupleAndKeywords _PyArg_ParseTupleAndKeywords_SizeT
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
在与Python C API要求兼容的同时,如何使用与static char* kwlist[] = {"a", "b", NULL}
和C++ 11
等效的结构摆脱警告?
在提出建议后,我尝试了static const char* kwlist[] = {"a", "b", NULL}
,但PyArg_ParseTupleAndKeywords
不接受它:
source/test.cpp:43:89: error: invalid conversion from ‘const char**’ to ‘char**’ [-fpermissive]
if( !PyArg_ParseTupleAndKeywords( args, kwargs, "s|s", kwlist, &filepath, &rawregex ) ) {
^
In file included from /usr/include/python3.6m/Python.h:117:0,
from source/test.cpp:3:
/usr/include/python3.6m/modsupport.h:17:41: note: initializing argument 4 of ‘int _PyArg_ParseTupleAndKeywords_SizeT(PyObject*, PyObject*, const char*, char**, ...)’
#define PyArg_ParseTupleAndKeywords _PyArg_ParseTupleAndKeywords_SizeT
^
/usr/include/python3.6m/modsupport.h:17:41: note: in definition of macro ‘PyArg_ParseTupleAndKeywords’
#define PyArg_ParseTupleAndKeywords _PyArg_ParseTupleAndKeywords_SizeT
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
答案 0 :(得分:1)
您从中复制的示例代码可能是C,警告没有被认真考虑(gcc
在这样的代码上未报告任何错误,g++
在没有警告选项的情况下也会发出警告)。
此处的解决方法是添加const
关键字,如下所示:
static const char* kwlist[] = {"a", "b", NULL};
您尝试过的const
关键字不是应用于值,而是应用于指针。
然后使用char **
使函数接受const_cast
:
const_cast<char **>(kwlist)
像这样:
PyArg_ParseTupleAndKeywords( args, kwargs, "s|s", const_cast<char **>(kwlist), &path, ®ex ) )
该函数不会通过隐式“ contract”来更改值(因为它是python API,所以不会骗你),因此在这种情况下,强制转换为const就可以了)
PS:this other answer给出的替代方法也有效。请注意,使用空的可变字符串并不比强制转换为常量更安全,就好像被调用函数决定写入空字符串一样,它也会调用未定义的行为。
答案 1 :(得分:1)
使用可变的空字符串:
char empty[] = "";
static char* kwlist[] = {empty, empty, NULL};