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/Modules/cPickle.c b/Modules/cPickle.c
index 6af99ba..865541f 100644
--- a/Modules/cPickle.c
+++ b/Modules/cPickle.c
@@ -339,7 +339,7 @@
int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
int nesting;
- int (*write_func)(struct Picklerobject *, char *, int);
+ int (*write_func)(struct Picklerobject *, const char *, int);
char *write_buf;
int buf_size;
PyObject *dispatch_table;
@@ -417,7 +417,7 @@
}
static int
-write_file(Picklerobject *self, char *s, int n)
+write_file(Picklerobject *self, const char *s, int n)
{
size_t nbyteswritten;
@@ -437,7 +437,7 @@
}
static int
-write_cStringIO(Picklerobject *self, char *s, int n)
+write_cStringIO(Picklerobject *self, const char *s, int n)
{
if (s == NULL) {
return 0;
@@ -451,14 +451,14 @@
}
static int
-write_none(Picklerobject *self, char *s, int n)
+write_none(Picklerobject *self, const char *s, int n)
{
if (s == NULL) return 0;
return n;
}
static int
-write_other(Picklerobject *self, char *s, int n)
+write_other(Picklerobject *self, const char *s, int n)
{
PyObject *py_str = 0, *junk = 0;
@@ -669,7 +669,7 @@
* The caller is responsible for free()'ing the return value.
*/
static char *
-pystrndup(char *s, int n)
+pystrndup(const char *s, int n)
{
char *r = (char *)malloc(n+1);
if (r == NULL)
@@ -945,7 +945,7 @@
static int
save_bool(Picklerobject *self, PyObject *args)
{
- static char *buf[2] = {FALSE, TRUE};
+ static const char *buf[2] = {FALSE, TRUE};
static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
long l = PyInt_AS_LONG((PyIntObject *)args);
@@ -2858,7 +2858,7 @@
static PyObject *
get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
{
- static char *kwlist[] = {"file", "protocol", NULL};
+ static const char *kwlist[] = {"file", "protocol", NULL};
PyObject *file = NULL;
int proto = 0;
@@ -5378,7 +5378,7 @@
static PyObject *
cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
{
- static char *kwlist[] = {"obj", "file", "protocol", NULL};
+ static const char *kwlist[] = {"obj", "file", "protocol", NULL};
PyObject *ob, *file, *res = NULL;
Picklerobject *pickler = 0;
int proto = 0;
@@ -5407,7 +5407,7 @@
static PyObject *
cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
{
- static char *kwlist[] = {"obj", "protocol", NULL};
+ static const char *kwlist[] = {"obj", "protocol", NULL};
PyObject *ob, *file = 0, *res = NULL;
Picklerobject *pickler = 0;
int proto = 0;