summaryrefslogtreecommitdiffstats
path: root/Python/modsupport.c
diff options
context:
space:
mode:
authorJeremy Hylton <jeremy@alum.mit.edu>2005-12-10 18:50:16 (GMT)
committerJeremy Hylton <jeremy@alum.mit.edu>2005-12-10 18:50:16 (GMT)
commitaf68c874a6803b4e90b616077a602c0593719a1d (patch)
treec7361b29cf629171b4da8e51cfd1074f67d814a7 /Python/modsupport.c
parentaaa2f1dea706daf2a5f431d97a3e3120dba652d2 (diff)
downloadcpython-af68c874a6803b4e90b616077a602c0593719a1d.zip
cpython-af68c874a6803b4e90b616077a602c0593719a1d.tar.gz
cpython-af68c874a6803b4e90b616077a602c0593719a1d.tar.bz2
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 *.
Diffstat (limited to 'Python/modsupport.c')
-rw-r--r--Python/modsupport.c36
1 files changed, 18 insertions, 18 deletions
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 @@ static char api_version_warning[] =
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 @@ Py_InitModule4(char *name, PyMethodDef *methods, char *doc,
/* 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 @@ countformat(char *format, int endchar)
/* 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 @@ do_mkdict(char **p_format, va_list *p_va, int endchar, int n)
}
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 @@ _ustrlen(Py_UNICODE *u)
#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 @@ do_mktuple(char **p_format, va_list *p_va, int endchar, int n)
}
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 @@ do_mkvalue(char **p_format, va_list *p_va)
PyObject *
-Py_BuildValue(char *format, ...)
+Py_BuildValue(const char *format, ...)
{
va_list va;
PyObject* retval;
@@ -465,9 +465,9 @@ Py_BuildValue(char *format, ...)
}
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 @@ Py_VaBuildValue(char *format, va_list va)
PyObject *
-PyEval_CallFunction(PyObject *obj, char *format, ...)
+PyEval_CallFunction(PyObject *obj, const char *format, ...)
{
va_list vargs;
PyObject *args;
@@ -516,7 +516,7 @@ PyEval_CallFunction(PyObject *obj, char *format, ...)
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 @@ PyEval_CallMethod(PyObject *obj, char *methodname, char *format, ...)
}
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 @@ PyModule_AddObject(PyObject *m, char *name, PyObject *o)
}
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));
}