-Wwrite-strings: More fix-old-Python-API wrappers
authorPedro Alves <palves@redhat.com>
Wed, 5 Apr 2017 18:21:35 +0000 (19:21 +0100)
committerPedro Alves <palves@redhat.com>
Wed, 5 Apr 2017 18:21:35 +0000 (19:21 +0100)
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  <palves@redhat.com>

* 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
gdb/python/python-internal.h

index fe1243b..9430d3a 100644 (file)
@@ -1,5 +1,13 @@
 2017-04-05  Pedro Alves  <palves@redhat.com>
 
+       * 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  <palves@redhat.com>
+
        * mi/mi-cmd-info.c (mi_cmd_info_os): Call info_osdata instead of
        info_osdata_command.
        * osdata.c (info_osdata_command): Rename to ...
index 4dd413d..55efd75 100644 (file)
@@ -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<typename... Args>
+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<char *> (method),
+                             const_cast<char *> (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<char *> (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<char *> (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<GDB_PYSYS_SETPATH_CHAR *> (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"