PyArg_ParseTupleAndKeywords 抛出警告:ISO C++ 禁止将字符串常量转换为‘char*’ [-Wwrite-strings]
Posted
技术标签:
【中文标题】PyArg_ParseTupleAndKeywords 抛出警告:ISO C++ 禁止将字符串常量转换为‘char*’ [-Wwrite-strings]【英文标题】:PyArg_ParseTupleAndKeywords throws warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings] 【发布时间】:2019-06-10 19:07:48 【问题描述】:搜索有关如何使用PyArg_ParseTupleAndKeywords
的示例我发现了这些问题:
-
How can one use PyArg_ParseTupleAndKeywords to parse a tuple with optional arguments as well as keywords?
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, ®ex ) )
return -1;
// other code ...
return 0;
在setup.py
上使用language = "c++"
编译它并使用-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
【问题讨论】:
err,你试过static const char* kwlist[] = "", "", NULL;
吗?
是的。它抛出error: invalid conversion from ‘const char**’ to ‘char**’
因为PyArg_ParseTupleAndKeywords
需要char **
好的,看我更新的答案。
【参考方案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 ) )
函数不会通过隐式“契约”来改变值(因为它是 python API,它不会骗你),所以在这种情况下,转换为 const 就可以了)
PS:this other answer 给出的替代方案也可以。请注意,使用空的可变字符串并不比强制转换为常量更安全,就好像被调用函数决定写入空字符串一样,它也会调用未定义的行为。
【讨论】:
【参考方案2】:使用可变空字符串:
char empty[] = "";
static char* kwlist[] = empty, empty, NULL;
【讨论】:
这会导致很难重现的非常奇怪的错误。下面试试。函数 foo 是 c++ 函数,它接受两个 python 关键字参数并且什么都不做。 import hello def call_foo(): hello.foo(a=1, b=2) hello.foo(a=1, b=2) # Success call_foo() # Fail以上是关于PyArg_ParseTupleAndKeywords 抛出警告:ISO C++ 禁止将字符串常量转换为‘char*’ [-Wwrite-strings]的主要内容,如果未能解决你的问题,请参考以下文章