Add const to several API functions that take char *.
In C++, it's an error to pass a string literal to a char* function
without a const_cast(). Rather than require every C++ extension
module to put a cast around string literals, fix the API to state the
const-ness.
I focused on parts of the API where people usually pass literals:
PyArg_ParseTuple() and friends, Py_BuildValue(), PyMethodDef, the type
slots, etc. Predictably, there were a large set of functions that
needed to be fixed as a result of these changes. The most pervasive
change was to make the keyword args list passed to
PyArg_ParseTupleAndKewords() to be a const char *kwlist[].
One cast was required as a result of the changes: A type object
mallocs the memory for its tp_doc slot and later frees it.
PyTypeObject says that tp_doc is const char *; but if the type was
created by type_new(), we know it is safe to cast to char *.
diff --git a/Python/modsupport.c b/Python/modsupport.c
index 3b4c517..e77cb36 100644
--- a/Python/modsupport.c
+++ b/Python/modsupport.c
@@ -26,7 +26,7 @@
This Python has API version %d, module %.100s has version %d.";
PyObject *
-Py_InitModule4(char *name, PyMethodDef *methods, char *doc,
+Py_InitModule4(const char *name, PyMethodDef *methods, const char *doc,
PyObject *passthrough, int module_api_version)
{
PyObject *m, *d, *v, *n;
@@ -99,7 +99,7 @@
/* Helper for mkvalue() to scan the length of a format */
static int
-countformat(char *format, int endchar)
+countformat(const char *format, int endchar)
{
int count = 0;
int level = 0;
@@ -142,14 +142,14 @@
/* Generic function to create a value -- the inverse of getargs() */
/* After an original idea and first implementation by Steven Miale */
-static PyObject *do_mktuple(char**, va_list *, int, int);
-static PyObject *do_mklist(char**, va_list *, int, int);
-static PyObject *do_mkdict(char**, va_list *, int, int);
-static PyObject *do_mkvalue(char**, va_list *);
+static PyObject *do_mktuple(const char**, va_list *, int, int);
+static PyObject *do_mklist(const char**, va_list *, int, int);
+static PyObject *do_mkdict(const char**, va_list *, int, int);
+static PyObject *do_mkvalue(const char**, va_list *);
static PyObject *
-do_mkdict(char **p_format, va_list *p_va, int endchar, int n)
+do_mkdict(const char **p_format, va_list *p_va, int endchar, int n)
{
PyObject *d;
int i;
@@ -195,7 +195,7 @@
}
static PyObject *
-do_mklist(char **p_format, va_list *p_va, int endchar, int n)
+do_mklist(const char **p_format, va_list *p_va, int endchar, int n)
{
PyObject *v;
int i;
@@ -242,7 +242,7 @@
#endif
static PyObject *
-do_mktuple(char **p_format, va_list *p_va, int endchar, int n)
+do_mktuple(const char **p_format, va_list *p_va, int endchar, int n)
{
PyObject *v;
int i;
@@ -278,7 +278,7 @@
}
static PyObject *
-do_mkvalue(char **p_format, va_list *p_va)
+do_mkvalue(const char **p_format, va_list *p_va)
{
for (;;) {
switch (*(*p_format)++) {
@@ -454,7 +454,7 @@
PyObject *
-Py_BuildValue(char *format, ...)
+Py_BuildValue(const char *format, ...)
{
va_list va;
PyObject* retval;
@@ -465,9 +465,9 @@
}
PyObject *
-Py_VaBuildValue(char *format, va_list va)
+Py_VaBuildValue(const char *format, va_list va)
{
- char *f = format;
+ const char *f = format;
int n = countformat(f, '\0');
va_list lva;
@@ -494,7 +494,7 @@
PyObject *
-PyEval_CallFunction(PyObject *obj, char *format, ...)
+PyEval_CallFunction(PyObject *obj, const char *format, ...)
{
va_list vargs;
PyObject *args;
@@ -516,7 +516,7 @@
PyObject *
-PyEval_CallMethod(PyObject *obj, char *methodname, char *format, ...)
+PyEval_CallMethod(PyObject *obj, const char *methodname, const char *format, ...)
{
va_list vargs;
PyObject *meth;
@@ -545,7 +545,7 @@
}
int
-PyModule_AddObject(PyObject *m, char *name, PyObject *o)
+PyModule_AddObject(PyObject *m, const char *name, PyObject *o)
{
PyObject *dict;
if (!PyModule_Check(m)) {
@@ -574,13 +574,13 @@
}
int
-PyModule_AddIntConstant(PyObject *m, char *name, long value)
+PyModule_AddIntConstant(PyObject *m, const char *name, long value)
{
return PyModule_AddObject(m, name, PyInt_FromLong(value));
}
int
-PyModule_AddStringConstant(PyObject *m, char *name, char *value)
+PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
{
return PyModule_AddObject(m, name, PyString_FromString(value));
}