mirror of
https://github.com/python/cpython.git
synced 2025-08-30 21:48:47 +00:00
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 *.
This commit is contained in:
parent
aaa2f1dea7
commit
af68c874a6
52 changed files with 272 additions and 255 deletions
|
@ -1907,7 +1907,7 @@ builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
|
|||
{
|
||||
PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
|
||||
PyObject *callable;
|
||||
static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
|
||||
static const char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
|
||||
long reverse;
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
|
||||
|
|
|
@ -3434,7 +3434,7 @@ PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
|
|||
return result;
|
||||
}
|
||||
|
||||
char *
|
||||
const char *
|
||||
PyEval_GetFuncName(PyObject *func)
|
||||
{
|
||||
if (PyMethod_Check(func))
|
||||
|
@ -3453,7 +3453,7 @@ PyEval_GetFuncName(PyObject *func)
|
|||
}
|
||||
}
|
||||
|
||||
char *
|
||||
const char *
|
||||
PyEval_GetFuncDesc(PyObject *func)
|
||||
{
|
||||
if (PyMethod_Check(func))
|
||||
|
|
|
@ -6,33 +6,33 @@
|
|||
#include <ctype.h>
|
||||
|
||||
|
||||
int PyArg_Parse(PyObject *, char *, ...);
|
||||
int PyArg_ParseTuple(PyObject *, char *, ...);
|
||||
int PyArg_VaParse(PyObject *, char *, va_list);
|
||||
int PyArg_Parse(PyObject *, const char *, ...);
|
||||
int PyArg_ParseTuple(PyObject *, const char *, ...);
|
||||
int PyArg_VaParse(PyObject *, const char *, va_list);
|
||||
|
||||
int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
|
||||
char *, char **, ...);
|
||||
const char *, const char **, ...);
|
||||
int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
|
||||
char *, char **, va_list);
|
||||
const char *, const char **, va_list);
|
||||
|
||||
|
||||
/* Forward */
|
||||
static int vgetargs1(PyObject *, char *, va_list *, int);
|
||||
static void seterror(int, char *, int *, char *, char *);
|
||||
static char *convertitem(PyObject *, char **, va_list *, int *, char *,
|
||||
static int vgetargs1(PyObject *, const char *, va_list *, int);
|
||||
static void seterror(int, const char *, int *, const char *, const char *);
|
||||
static char *convertitem(PyObject *, const char **, va_list *, int *, char *,
|
||||
size_t, PyObject **);
|
||||
static char *converttuple(PyObject *, char **, va_list *,
|
||||
static char *converttuple(PyObject *, const char **, va_list *,
|
||||
int *, char *, size_t, int, PyObject **);
|
||||
static char *convertsimple(PyObject *, char **, va_list *, char *,
|
||||
static char *convertsimple(PyObject *, const char **, va_list *, char *,
|
||||
size_t, PyObject **);
|
||||
static int convertbuffer(PyObject *, void **p, char **);
|
||||
|
||||
static int vgetargskeywords(PyObject *, PyObject *,
|
||||
char *, char **, va_list *);
|
||||
static char *skipitem(char **, va_list *);
|
||||
const char *, const char **, va_list *);
|
||||
static char *skipitem(const char **, va_list *);
|
||||
|
||||
int
|
||||
PyArg_Parse(PyObject *args, char *format, ...)
|
||||
PyArg_Parse(PyObject *args, const char *format, ...)
|
||||
{
|
||||
int retval;
|
||||
va_list va;
|
||||
|
@ -45,7 +45,7 @@ PyArg_Parse(PyObject *args, char *format, ...)
|
|||
|
||||
|
||||
int
|
||||
PyArg_ParseTuple(PyObject *args, char *format, ...)
|
||||
PyArg_ParseTuple(PyObject *args, const char *format, ...)
|
||||
{
|
||||
int retval;
|
||||
va_list va;
|
||||
|
@ -58,7 +58,7 @@ PyArg_ParseTuple(PyObject *args, char *format, ...)
|
|||
|
||||
|
||||
int
|
||||
PyArg_VaParse(PyObject *args, char *format, va_list va)
|
||||
PyArg_VaParse(PyObject *args, const char *format, va_list va)
|
||||
{
|
||||
va_list lva;
|
||||
|
||||
|
@ -120,17 +120,17 @@ cleanreturn(int retval, PyObject *freelist)
|
|||
|
||||
|
||||
static int
|
||||
vgetargs1(PyObject *args, char *format, va_list *p_va, int compat)
|
||||
vgetargs1(PyObject *args, const char *format, va_list *p_va, int compat)
|
||||
{
|
||||
char msgbuf[256];
|
||||
int levels[32];
|
||||
char *fname = NULL;
|
||||
char *message = NULL;
|
||||
const char *fname = NULL;
|
||||
const char *message = NULL;
|
||||
int min = -1;
|
||||
int max = 0;
|
||||
int level = 0;
|
||||
int endfmt = 0;
|
||||
char *formatsave = format;
|
||||
const char *formatsave = format;
|
||||
int i, len;
|
||||
char *msg;
|
||||
PyObject *freelist = NULL;
|
||||
|
@ -269,7 +269,8 @@ vgetargs1(PyObject *args, char *format, va_list *p_va, int compat)
|
|||
|
||||
|
||||
static void
|
||||
seterror(int iarg, char *msg, int *levels, char *fname, char *message)
|
||||
seterror(int iarg, const char *msg, int *levels, const char *fname,
|
||||
const char *message)
|
||||
{
|
||||
char buf[512];
|
||||
int i;
|
||||
|
@ -324,12 +325,12 @@ seterror(int iarg, char *msg, int *levels, char *fname, char *message)
|
|||
*/
|
||||
|
||||
static char *
|
||||
converttuple(PyObject *arg, char **p_format, va_list *p_va, int *levels,
|
||||
converttuple(PyObject *arg, const char **p_format, va_list *p_va, int *levels,
|
||||
char *msgbuf, size_t bufsize, int toplevel, PyObject **freelist)
|
||||
{
|
||||
int level = 0;
|
||||
int n = 0;
|
||||
char *format = *p_format;
|
||||
const char *format = *p_format;
|
||||
int i;
|
||||
|
||||
for (;;) {
|
||||
|
@ -392,11 +393,11 @@ converttuple(PyObject *arg, char **p_format, va_list *p_va, int *levels,
|
|||
/* Convert a single item. */
|
||||
|
||||
static char *
|
||||
convertitem(PyObject *arg, char **p_format, va_list *p_va, int *levels,
|
||||
convertitem(PyObject *arg, const char **p_format, va_list *p_va, int *levels,
|
||||
char *msgbuf, size_t bufsize, PyObject **freelist)
|
||||
{
|
||||
char *msg;
|
||||
char *format = *p_format;
|
||||
const char *format = *p_format;
|
||||
|
||||
if (*format == '(' /* ')' */) {
|
||||
format++;
|
||||
|
@ -424,7 +425,7 @@ convertitem(PyObject *arg, char **p_format, va_list *p_va, int *levels,
|
|||
/* Format an error message generated by convertsimple(). */
|
||||
|
||||
static char *
|
||||
converterr(char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
|
||||
converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
|
||||
{
|
||||
assert(expected != NULL);
|
||||
assert(arg != NULL);
|
||||
|
@ -459,10 +460,10 @@ float_argument_error(PyObject *arg)
|
|||
*/
|
||||
|
||||
static char *
|
||||
convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf,
|
||||
size_t bufsize, PyObject **freelist)
|
||||
convertsimple(PyObject *arg, const char **p_format, va_list *p_va,
|
||||
char *msgbuf, size_t bufsize, PyObject **freelist)
|
||||
{
|
||||
char *format = *p_format;
|
||||
const char *format = *p_format;
|
||||
char c = *format++;
|
||||
#ifdef Py_USING_UNICODE
|
||||
PyObject *uarg;
|
||||
|
@ -1134,8 +1135,8 @@ convertbuffer(PyObject *arg, void **p, char **errmsg)
|
|||
int
|
||||
PyArg_ParseTupleAndKeywords(PyObject *args,
|
||||
PyObject *keywords,
|
||||
char *format,
|
||||
char **kwlist, ...)
|
||||
const char *format,
|
||||
const char **kwlist, ...)
|
||||
{
|
||||
int retval;
|
||||
va_list va;
|
||||
|
@ -1158,9 +1159,9 @@ PyArg_ParseTupleAndKeywords(PyObject *args,
|
|||
|
||||
int
|
||||
PyArg_VaParseTupleAndKeywords(PyObject *args,
|
||||
PyObject *keywords,
|
||||
char *format,
|
||||
char **kwlist, va_list va)
|
||||
PyObject *keywords,
|
||||
const char *format,
|
||||
const char **kwlist, va_list va)
|
||||
{
|
||||
int retval;
|
||||
va_list lva;
|
||||
|
@ -1190,16 +1191,16 @@ PyArg_VaParseTupleAndKeywords(PyObject *args,
|
|||
|
||||
|
||||
static int
|
||||
vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
|
||||
char **kwlist, va_list *p_va)
|
||||
vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
|
||||
const char **kwlist, va_list *p_va)
|
||||
{
|
||||
char msgbuf[512];
|
||||
int levels[32];
|
||||
char *fname, *message;
|
||||
const char *fname, *message;
|
||||
int min, max;
|
||||
char *formatsave;
|
||||
const char *formatsave;
|
||||
int i, len, nargs, nkeywords;
|
||||
char *msg, **p;
|
||||
const char *msg, **p;
|
||||
PyObject *freelist = NULL;
|
||||
|
||||
assert(args != NULL && PyTuple_Check(args));
|
||||
|
@ -1269,7 +1270,7 @@ vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
|
|||
keyword parameter in messages */
|
||||
if (nkeywords > 0) {
|
||||
for (i = 0; i < nargs; i++) {
|
||||
char *thiskw = kwlist[i];
|
||||
const char *thiskw = kwlist[i];
|
||||
if (thiskw == NULL)
|
||||
break;
|
||||
if (PyDict_GetItemString(keywords, thiskw)) {
|
||||
|
@ -1402,9 +1403,9 @@ vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
|
|||
|
||||
|
||||
static char *
|
||||
skipitem(char **p_format, va_list *p_va)
|
||||
skipitem(const char **p_format, va_list *p_va)
|
||||
{
|
||||
char *format = *p_format;
|
||||
const char *format = *p_format;
|
||||
char c = *format++;
|
||||
|
||||
switch (c) {
|
||||
|
@ -1518,7 +1519,7 @@ err:
|
|||
|
||||
|
||||
int
|
||||
PyArg_UnpackTuple(PyObject *args, char *name, int min, int max, ...)
|
||||
PyArg_UnpackTuple(PyObject *args, const char *name, int min, int max, ...)
|
||||
{
|
||||
int i, l;
|
||||
PyObject **o;
|
||||
|
@ -1583,7 +1584,7 @@ PyArg_UnpackTuple(PyObject *args, char *name, int min, int max, ...)
|
|||
* not emtpy, returns 1 otherwise
|
||||
*/
|
||||
int
|
||||
_PyArg_NoKeywords(char *funcname, PyObject *kw)
|
||||
_PyArg_NoKeywords(const char *funcname, PyObject *kw)
|
||||
{
|
||||
if (kw == NULL)
|
||||
return 1;
|
||||
|
@ -1598,6 +1599,3 @@ _PyArg_NoKeywords(char *funcname, PyObject *kw)
|
|||
funcname);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -555,7 +555,7 @@ _PyImport_FindExtension(char *name, char *filename)
|
|||
'NEW' REFERENCE! */
|
||||
|
||||
PyObject *
|
||||
PyImport_AddModule(char *name)
|
||||
PyImport_AddModule(const char *name)
|
||||
{
|
||||
PyObject *modules = PyImport_GetModuleDict();
|
||||
PyObject *m;
|
||||
|
@ -1875,7 +1875,7 @@ PyImport_ImportFrozenModule(char *name)
|
|||
its module object WITH INCREMENTED REFERENCE COUNT */
|
||||
|
||||
PyObject *
|
||||
PyImport_ImportModule(char *name)
|
||||
PyImport_ImportModule(const char *name)
|
||||
{
|
||||
PyObject *pname;
|
||||
PyObject *result;
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue