Python 到 C/C++常量字符问题
我正在用一些 C++ 代码扩展 Python。
我正在使用的函数之一具有以下签名:(
int PyArg_ParseTupleAndKeywords(PyObject *arg, PyObject *kwdict,
char *format, char **kwlist, ...);
链接:http ://docs.python.org/release/1.5.2p2/ext/parseTupleAndKeywords.html)
感兴趣的参数是kwlist。在上面的链接中,给出了如何使用此功能的示例。在示例中,kwlist 如下所示:
static char *kwlist[] = {"voltage", "state", "action", "type", NULL};
当我使用 g++ 编译它时,我收到警告:
warning: deprecated conversion from string constant to ‘char*’
因此,我可以将 static char* 更改为 static const char*。不幸的是,我无法更改Python 代码。因此,通过此更改,我得到了不同的编译错误(无法将 char** 转换为 const char**)。根据我在这里读到的内容,我可以打开编译器标志来忽略警告,或者可以将 kwlist 定义中的每个常量字符串转换为 char *。目前,我正在做后者。还有哪些其他解决方案?
抱歉,如果这个问题之前已被问过。我是新来的。
I am extending Python with some C++ code.
One of the functions I'm using has the following signature:
int PyArg_ParseTupleAndKeywords(PyObject *arg, PyObject *kwdict,
char *format, char **kwlist, ...);
(link: http://docs.python.org/release/1.5.2p2/ext/parseTupleAndKeywords.html)
The parameter of interest is kwlist. In the link above, examples on how to use this function are given. In the examples, kwlist looks like:
static char *kwlist[] = {"voltage", "state", "action", "type", NULL};
When I compile this using g++, I get the warning:
warning: deprecated conversion from string constant to ‘char*’
So, I can change the static char* to a static const char*. Unfortunately, I can't change the Python code. So with this change, I get a different compilation error (can't convert char** to const char**). Based on what I've read here, I can turn on compiler flags to ignore the warning or I can cast each of the constant strings in the definition of kwlist to char *. Currently, I'm doing the latter. What are other solutions?
Sorry if this question has been asked before. I'm new.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PyArg_ParseTupleAndKeywords()
是否期望修改您传入的数据?通常,在惯用的 C++ 中,const表示*
指向被调用者只能读取的对象,而则指向被调用者只能读取的对象。 *
指向被调用者可以写入的对象。如果
PyArg_ParseTupleAndKeywords()
期望能够写入您传入的char *
,那么除了您在问题中提到的问题之外,您还会遇到一个完全不同的问题。假设 PyArg_ParseTupleAndKeywords 不想修改其参数,处理此问题的惯用正确方法是将 kwlist 声明为 const char *kwlist[] 并使用>const_cast 在调用
PyArg_ParseTupleAndKeywords()
时删除其常量性,这将使其看起来像这样:Does
PyArg_ParseTupleAndKeywords()
expect to modify the data you are passing in? Normally, in idiomatic C++, aconst <something> *
points to an object that the callee will only read from, whereas<something> *
points to an object that the callee can write to.If
PyArg_ParseTupleAndKeywords()
expects to be able to write to thechar *
you are passing in, you've got an entirely different problem over and above what you mention in your question.Assuming that
PyArg_ParseTupleAndKeywords
does not want to modify its parameters, the idiomatically correct way of dealing with this problem would be to declare kwlist asconst char *kwlist[]
and useconst_cast
to remove its const-ness when callingPyArg_ParseTupleAndKeywords()
which would make it look like this:七年前有一个被接受的答案,但我想添加一个替代解决方案,因为这个主题似乎仍然相关。
如果您不喜欢 const_cast 解决方案,您还可以创建字符串数组的可写版本。
char name[] = ".."
将字符串复制到可写位置。There is an accepted answer from seven years ago, but I'd like to add an alternative solution, since this topic seems to be still relevant.
If you don't like the
const_cast
solution, you can also create a write-able version of the string array.The
char name[] = ".."
copies the your string to a writable location.