summaryrefslogtreecommitdiffstats
path: root/Objects
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 /Objects
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 'Objects')
-rw-r--r--Objects/boolobject.c2
-rw-r--r--Objects/classobject.c2
-rw-r--r--Objects/complexobject.c2
-rw-r--r--Objects/descrobject.c4
-rw-r--r--Objects/enumobject.c2
-rw-r--r--Objects/fileobject.c7
-rw-r--r--Objects/floatobject.c2
-rw-r--r--Objects/funcobject.c2
-rw-r--r--Objects/intobject.c2
-rw-r--r--Objects/listobject.c4
-rw-r--r--Objects/longobject.c3
-rw-r--r--Objects/methodobject.c8
-rw-r--r--Objects/moduleobject.c8
-rw-r--r--Objects/object.c10
-rw-r--r--Objects/stringobject.c2
-rw-r--r--Objects/structseq.c2
-rw-r--r--Objects/tupleobject.c2
-rw-r--r--Objects/typeobject.c16
-rw-r--r--Objects/unicodeobject.c2
-rw-r--r--Objects/weakrefobject.c2
20 files changed, 45 insertions, 39 deletions
diff --git a/Objects/boolobject.c b/Objects/boolobject.c
index f2429fe..677a98b 100644
--- a/Objects/boolobject.c
+++ b/Objects/boolobject.c
@@ -50,7 +50,7 @@ PyObject *PyBool_FromLong(long ok)
static PyObject *
bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- static char *kwlist[] = {"x", 0};
+ static const char *kwlist[] = {"x", 0};
PyObject *x = Py_False;
long ok;
diff --git a/Objects/classobject.c b/Objects/classobject.c
index ddec3e4..c8057e2 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -159,7 +159,7 @@ static PyObject *
class_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *name, *bases, *dict;
- static char *kwlist[] = {"name", "bases", "dict", 0};
+ static const char *kwlist[] = {"name", "bases", "dict", 0};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "SOO", kwlist,
&name, &bases, &dict))
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 138ba80..08c8c89 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -829,7 +829,7 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Py_complex cr, ci;
int own_r = 0;
static PyObject *complexstr;
- static char *kwlist[] = {"real", "imag", 0};
+ static const char *kwlist[] = {"real", "imag", 0};
r = Py_False;
i = NULL;
diff --git a/Objects/descrobject.c b/Objects/descrobject.c
index df7435a..f763832 100644
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -579,7 +579,7 @@ PyTypeObject PyWrapperDescr_Type = {
};
static PyDescrObject *
-descr_new(PyTypeObject *descrtype, PyTypeObject *type, char *name)
+descr_new(PyTypeObject *descrtype, PyTypeObject *type, const char *name)
{
PyDescrObject *descr;
@@ -1182,7 +1182,7 @@ static int
property_init(PyObject *self, PyObject *args, PyObject *kwds)
{
PyObject *get = NULL, *set = NULL, *del = NULL, *doc = NULL;
- static char *kwlist[] = {"fget", "fset", "fdel", "doc", 0};
+ static const char *kwlist[] = {"fget", "fset", "fdel", "doc", 0};
propertyobject *gs = (propertyobject *)self;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO:property",
diff --git a/Objects/enumobject.c b/Objects/enumobject.c
index f74acdc..a9d4a5e 100644
--- a/Objects/enumobject.c
+++ b/Objects/enumobject.c
@@ -16,7 +16,7 @@ enum_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
enumobject *en;
PyObject *seq = NULL;
- static char *kwlist[] = {"sequence", 0};
+ static const char *kwlist[] = {"sequence", 0};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:enumerate", kwlist,
&seq))
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index 7e40547..259a423 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -1884,7 +1884,7 @@ file_init(PyObject *self, PyObject *args, PyObject *kwds)
{
PyFileObject *foself = (PyFileObject *)self;
int ret = 0;
- static char *kwlist[] = {"name", "mode", "buffering", 0};
+ static const char *kwlist[] = {"name", "mode", "buffering", 0};
char *name = NULL;
char *mode = "r";
int bufsize = -1;
@@ -1926,8 +1926,9 @@ file_init(PyObject *self, PyObject *args, PyObject *kwds)
return -1;
/* We parse again to get the name as a PyObject */
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:file", kwlist,
- &o_name, &mode, &bufsize))
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:file",
+ kwlist, &o_name, &mode,
+ &bufsize))
return -1;
if (fill_file_fields(foself, NULL, o_name, mode,
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index b3f861f..198beb1 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -941,7 +941,7 @@ static PyObject *
float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *x = Py_False; /* Integer zero */
- static char *kwlist[] = {"x", 0};
+ static const char *kwlist[] = {"x", 0};
if (type != &PyFloat_Type)
return float_subtype_new(type, args, kwds); /* Wimp out */
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index 85cef6f..d0e3a25 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -364,7 +364,7 @@ func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
PyObject *closure = Py_None;
PyFunctionObject *newfunc;
int nfree, nclosure;
- static char *kwlist[] = {"code", "globals", "name",
+ static const char *kwlist[] = {"code", "globals", "name",
"argdefs", "closure", 0};
if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
diff --git a/Objects/intobject.c b/Objects/intobject.c
index 0ead74b..5a0b259 100644
--- a/Objects/intobject.c
+++ b/Objects/intobject.c
@@ -879,7 +879,7 @@ int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *x = NULL;
int base = -909;
- static char *kwlist[] = {"x", "base", 0};
+ static const char *kwlist[] = {"x", "base", 0};
if (type != &PyInt_Type)
return int_subtype_new(type, args, kwds); /* Wimp out */
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 1a96361..04671f4 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -1983,7 +1983,7 @@ listsort(PyListObject *self, PyObject *args, PyObject *kwds)
PyObject *keyfunc = NULL;
int i;
PyObject *key, *value, *kvpair;
- static char *kwlist[] = {"cmp", "key", "reverse", 0};
+ static const char *kwlist[] = {"cmp", "key", "reverse", 0};
assert(self != NULL);
assert (PyList_Check(self));
@@ -2357,7 +2357,7 @@ static int
list_init(PyListObject *self, PyObject *args, PyObject *kw)
{
PyObject *arg = NULL;
- static char *kwlist[] = {"sequence", 0};
+ static const char *kwlist[] = {"sequence", 0};
if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
return -1;
diff --git a/Objects/longobject.c b/Objects/longobject.c
index ff5ba6f..6f98d44 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -1,4 +1,5 @@
+
/* Long (arbitrary precision) integer object implementation */
/* XXX The functional organization of this file is terrible */
@@ -2922,7 +2923,7 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *x = NULL;
int base = -909; /* unlikely! */
- static char *kwlist[] = {"x", "base", 0};
+ static const char *kwlist[] = {"x", "base", 0};
if (type != &PyLong_Type)
return long_subtype_new(type, args, kwds); /* Wimp out */
diff --git a/Objects/methodobject.c b/Objects/methodobject.c
index 5818616..3a205f5 100644
--- a/Objects/methodobject.c
+++ b/Objects/methodobject.c
@@ -132,7 +132,7 @@ meth_dealloc(PyCFunctionObject *m)
static PyObject *
meth_get__doc__(PyCFunctionObject *m, void *closure)
{
- char *doc = m->m_ml->ml_doc;
+ const char *doc = m->m_ml->ml_doc;
if (doc != NULL)
return PyString_FromString(doc);
@@ -311,13 +311,13 @@ listmethodchain(PyMethodChain *chain)
/* Find a method in a method chain */
PyObject *
-Py_FindMethodInChain(PyMethodChain *chain, PyObject *self, char *name)
+Py_FindMethodInChain(PyMethodChain *chain, PyObject *self, const char *name)
{
if (name[0] == '_' && name[1] == '_') {
if (strcmp(name, "__methods__") == 0)
return listmethodchain(chain);
if (strcmp(name, "__doc__") == 0) {
- char *doc = self->ob_type->tp_doc;
+ const char *doc = self->ob_type->tp_doc;
if (doc != NULL)
return PyString_FromString(doc);
}
@@ -339,7 +339,7 @@ Py_FindMethodInChain(PyMethodChain *chain, PyObject *self, char *name)
/* Find a method in a single method list */
PyObject *
-Py_FindMethod(PyMethodDef *methods, PyObject *self, char *name)
+Py_FindMethod(PyMethodDef *methods, PyObject *self, const char *name)
{
PyMethodChain chain;
chain.methods = methods;
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index cc75e45..76a4ab3 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -15,7 +15,7 @@ static PyMemberDef module_members[] = {
};
PyObject *
-PyModule_New(char *name)
+PyModule_New(const char *name)
{
PyModuleObject *m;
PyObject *nameobj;
@@ -149,10 +149,10 @@ _PyModule_Clear(PyObject *m)
static int
module_init(PyModuleObject *m, PyObject *args, PyObject *kwds)
{
- static char *kwlist[] = {"name", "doc", NULL};
+ static const char *kwlist[] = {"name", "doc", NULL};
PyObject *dict, *name = Py_None, *doc = Py_None;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "S|O:module.__init__", kwlist,
- &name, &doc))
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "S|O:module.__init__",
+ kwlist, &name, &doc))
return -1;
dict = m->md_dict;
if (dict == NULL) {
diff --git a/Objects/object.c b/Objects/object.c
index 1895697..6d6d078 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -663,7 +663,7 @@ static int
default_3way_compare(PyObject *v, PyObject *w)
{
int c;
- char *vname, *wname;
+ const char *vname, *wname;
if (v->ob_type == w->ob_type) {
/* When comparing these pointers, they must be cast to
@@ -1018,7 +1018,7 @@ PyObject_Hash(PyObject *v)
}
PyObject *
-PyObject_GetAttrString(PyObject *v, char *name)
+PyObject_GetAttrString(PyObject *v, const char *name)
{
PyObject *w, *res;
@@ -1033,7 +1033,7 @@ PyObject_GetAttrString(PyObject *v, char *name)
}
int
-PyObject_HasAttrString(PyObject *v, char *name)
+PyObject_HasAttrString(PyObject *v, const char *name)
{
PyObject *res = PyObject_GetAttrString(v, name);
if (res != NULL) {
@@ -1045,7 +1045,7 @@ PyObject_HasAttrString(PyObject *v, char *name)
}
int
-PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
+PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
{
PyObject *s;
int res;
@@ -1589,7 +1589,7 @@ merge_class_dict(PyObject* dict, PyObject* aclass)
*/
static int
-merge_list_attr(PyObject* dict, PyObject* obj, char *attrname)
+merge_list_attr(PyObject* dict, PyObject* obj, const char *attrname)
{
PyObject *list;
int result = 0;
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index 037fa6a..7c3ab09 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -3325,7 +3325,7 @@ static PyObject *
string_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *x = NULL;
- static char *kwlist[] = {"object", 0};
+ static const char *kwlist[] = {"object", 0};
if (type != &PyString_Type)
return str_subtype_new(type, args, kwds);
diff --git a/Objects/structseq.c b/Objects/structseq.c
index 603477f..ac3cf03 100644
--- a/Objects/structseq.c
+++ b/Objects/structseq.c
@@ -97,7 +97,7 @@ structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
PyObject *ob;
PyStructSequence *res = NULL;
int len, min_len, max_len, i, n_unnamed_fields;
- static char *kwlist[] = {"sequence", "dict", 0};
+ static const char *kwlist[] = {"sequence", "dict", 0};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:structseq",
kwlist, &arg, &dict))
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
index 76e5de3..3fd311a 100644
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -528,7 +528,7 @@ static PyObject *
tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *arg = NULL;
- static char *kwlist[] = {"sequence", 0};
+ static const char *kwlist[] = {"sequence", 0};
if (type != &PyTuple_Type)
return tuple_subtype_new(type, args, kwds);
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 591c62b..7c36ba4 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -21,7 +21,7 @@ static PyMemberDef type_members[] = {
static PyObject *
type_name(PyTypeObject *type, void *context)
{
- char *s;
+ const char *s;
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
PyHeapTypeObject* et = (PyHeapTypeObject*)type;
@@ -1556,7 +1556,7 @@ static PyObject *
type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
{
PyObject *name, *bases, *dict;
- static char *kwlist[] = {"name", "bases", "dict", 0};
+ static const char *kwlist[] = {"name", "bases", "dict", 0};
PyObject *slots, *tmp, *newslots;
PyTypeObject *type, *base, *tmptype, *winner;
PyHeapTypeObject *et;
@@ -1856,12 +1856,13 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
PyObject *doc = PyDict_GetItemString(dict, "__doc__");
if (doc != NULL && PyString_Check(doc)) {
const size_t n = (size_t)PyString_GET_SIZE(doc);
- type->tp_doc = (char *)PyObject_MALLOC(n+1);
- if (type->tp_doc == NULL) {
+ char *tp_doc = PyObject_MALLOC(n+1);
+ if (tp_doc == NULL) {
Py_DECREF(type);
return NULL;
}
- memcpy(type->tp_doc, PyString_AS_STRING(doc), n+1);
+ memcpy(tp_doc, PyString_AS_STRING(doc), n+1);
+ type->tp_doc = tp_doc;
}
}
@@ -2105,7 +2106,10 @@ type_dealloc(PyTypeObject *type)
Py_XDECREF(type->tp_mro);
Py_XDECREF(type->tp_cache);
Py_XDECREF(type->tp_subclasses);
- PyObject_Free(type->tp_doc);
+ /* A type's tp_doc is heap allocated, unlike the tp_doc slots
+ * of most other objects. It's okay to cast it to char *.
+ */
+ PyObject_Free((char *)type->tp_doc);
Py_XDECREF(et->name);
Py_XDECREF(et->slots);
type->ob_type->tp_free((PyObject *)type);
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 013fe4a..9e5e3b4 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -7238,7 +7238,7 @@ static PyObject *
unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *x = NULL;
- static char *kwlist[] = {"string", "encoding", "errors", 0};
+ static const char *kwlist[] = {"string", "encoding", "errors", 0};
char *encoding = NULL;
char *errors = NULL;
diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c
index 5412dd3..1de9434 100644
--- a/Objects/weakrefobject.c
+++ b/Objects/weakrefobject.c
@@ -126,7 +126,7 @@ gc_clear(PyWeakReference *self)
static PyObject *
weakref_call(PyWeakReference *self, PyObject *args, PyObject *kw)
{
- static char *argnames[] = {NULL};
+ static const char *argnames[] = {NULL};
if (PyArg_ParseTupleAndKeywords(args, kw, ":__call__", argnames)) {
PyObject *object = PyWeakref_GET_OBJECT(self);