diff options
author | Jeremy Hylton <jeremy@alum.mit.edu> | 2005-12-10 18:50:16 (GMT) |
---|---|---|
committer | Jeremy Hylton <jeremy@alum.mit.edu> | 2005-12-10 18:50:16 (GMT) |
commit | af68c874a6803b4e90b616077a602c0593719a1d (patch) | |
tree | c7361b29cf629171b4da8e51cfd1074f67d814a7 /Python/getargs.c | |
parent | aaa2f1dea706daf2a5f431d97a3e3120dba652d2 (diff) | |
download | cpython-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/getargs.c')
-rw-r--r-- | Python/getargs.c | 90 |
1 files changed, 44 insertions, 46 deletions
diff --git a/Python/getargs.c b/Python/getargs.c index df60a86..16f156f 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -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; } - - - |