PyArg_ParseTupleAndKeywords引发警告:ISO C ++禁止将字符串常量转换为“ char *” [-Wwrite-strings]

时间:2019-06-10 19:07:48

标签: python c++ c c++11 python-c-api

搜索有关如何使用PyArg_ParseTupleAndKeywords的示例,我发现了以下问题:

  1. How can one use PyArg_ParseTupleAndKeywords to parse a tuple with optional arguments as well as keywords?
  2. How does PyArg_ParseTupleAndKeywords work?

他们俩都使用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, &regex ) ) {
        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, &regex ) ) {
                                                                                         ^
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

2 个答案:

答案 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, &regex ) ) 

该函数不会通过隐式“ contract”来更改值(因为它是python API,所以不会骗你),因此在这种情况下,强制转换为const就可以了)

PS:this other answer给出的替代方法也有效。请注意,使用空的可变字符串并不比强制转换为常量更安全,就好像被调用函数决定写入空字符串一样,它也会调用未定义的行为。

答案 1 :(得分:1)

使用可变的空字符串:

char empty[] = "";
static char* kwlist[] = {empty, empty, NULL};