From 4d75997912d77497fd395fde222513436a7df046 Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Wed, 5 Apr 2017 19:21:35 +0100 Subject: [PATCH] -Wwrite-strings: More fix-old-Python-API wrappers When building against Python 2.7, -Wwrite-strings flags several cases of passing a string literal to Python functions that expect a "char *". This commit addresses the issue like we already handle several other similar cases -- wrap the Python API with our own fixed version that adds the necessary constification. gdb/ChangeLog: 2017-04-05 Pedro Alves * python/python-internal.h (gdb_PyObject_CallMethod) (gdb_PyErr_NewException, gdb_PySys_GetObject, gdb_PySys_SetPath): New functions. (GDB_PYSYS_SETPATH_CHAR, PyObject_CallMethod, PyErr_NewException) (PySys_GetObject, PySys_SetPath): New macros. --- gdb/ChangeLog | 8 +++++ gdb/python/python-internal.h | 63 ++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index fe1243be8cd..9430d3a273a 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,11 @@ +2017-04-05 Pedro Alves + + * python/python-internal.h (gdb_PyObject_CallMethod) + (gdb_PyErr_NewException, gdb_PySys_GetObject, gdb_PySys_SetPath): + New functions. + (GDB_PYSYS_SETPATH_CHAR, PyObject_CallMethod, PyErr_NewException) + (PySys_GetObject, PySys_SetPath): New macros. + 2017-04-05 Pedro Alves * mi/mi-cmd-info.c (mi_cmd_info_os): Call info_osdata instead of diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 4dd413dc312..55efd75a8b6 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -223,6 +223,69 @@ gdb_PyObject_HasAttrString (PyObject *obj, #define PyObject_HasAttrString(obj, attr) gdb_PyObject_HasAttrString (obj, attr) +/* PyObject_CallMethod's 'method' and 'format' parameters were missing + the 'const' qualifier before Python 3.4. Hence, we wrap the + function in our own version to avoid errors with string literals. + Note, this is a variadic template because PyObject_CallMethod is a + varargs function and Python doesn't have a "PyObject_VaCallMethod" + variant taking a va_list that we could defer to instead. */ + +template +static inline PyObject * +gdb_PyObject_CallMethod (PyObject *o, const char *method, const char *format, + Args... args) /* ARI: editCase function */ +{ + return PyObject_CallMethod (o, + const_cast (method), + const_cast (format), + args...); +} + +#undef PyObject_CallMethod +#define PyObject_CallMethod gdb_PyObject_CallMethod + +/* The 'name' parameter of PyErr_NewException was missing the 'const' + qualifier in Python <= 3.4. Hence, we wrap it in a function to + avoid errors when compiled with -Werror. */ + +static inline PyObject* +gdb_PyErr_NewException (const char *name, PyObject *base, PyObject *dict) +{ + return PyErr_NewException (const_cast (name), base, dict); +} + +#define PyErr_NewException gdb_PyErr_NewException + +/* PySys_GetObject's 'name' parameter was missing the 'const' + qualifier before Python 3.4. Hence, we wrap it in a function to + avoid errors when compiled with -Werror. */ + +static inline PyObject * +gdb_PySys_GetObject (const char *name) +{ + return PySys_GetObject (const_cast (name)); +} + +#define PySys_GetObject gdb_PySys_GetObject + +/* PySys_SetPath's 'path' parameter was missing the 'const' qualifier + before Python 3.6. Hence, we wrap it in a function to avoid errors + when compiled with -Werror. */ + +#ifdef IS_PY3K +# define GDB_PYSYS_SETPATH_CHAR wchar_t +#else +# define GDB_PYSYS_SETPATH_CHAR char +#endif + +static inline void +gdb_PySys_SetPath (const GDB_PYSYS_SETPATH_CHAR *path) +{ + PySys_SetPath (const_cast (path)); +} + +#define PySys_SetPath gdb_PySys_SetPath + /* In order to be able to parse symtab_and_line_to_sal_object function a real symtab_and_line structure is needed. */ #include "symtab.h" -- 2.30.2