Skip to content

Commit af68c87

Browse files
committed
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 *.
1 parent aaa2f1d commit af68c87

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+272
-255
lines changed

Include/cStringIO.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static struct PycStringIO_CAPI {
3838
int(*creadline)(PyObject *, char **);
3939

4040
/* Write a string to an output object*/
41-
int(*cwrite)(PyObject *, char *, int);
41+
int(*cwrite)(PyObject *, const char *, int);
4242

4343
/* Get the output object as a Python string (returns new reference). */
4444
PyObject *(*cgetvalue)(PyObject *);

Include/ceval.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ PyAPI_FUNC(PyObject *) PyEval_CallObject(PyObject *, PyObject *);
1818
#define PyEval_CallObject(func,arg) \
1919
PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
2020

21-
PyAPI_FUNC(PyObject *) PyEval_CallFunction(PyObject *obj, char *format, ...);
21+
PyAPI_FUNC(PyObject *) PyEval_CallFunction(PyObject *obj,
22+
const char *format, ...);
2223
PyAPI_FUNC(PyObject *) PyEval_CallMethod(PyObject *obj,
23-
char *methodname, char *format, ...);
24+
const char *methodname,
25+
const char *format, ...);
2426

2527
PyAPI_FUNC(void) PyEval_SetProfile(Py_tracefunc, PyObject *);
2628
PyAPI_FUNC(void) PyEval_SetTrace(Py_tracefunc, PyObject *);
@@ -60,8 +62,8 @@ PyAPI_DATA(int) _Py_CheckRecursionLimit;
6062
# define _Py_MakeRecCheck(x) (++(x) > _Py_CheckRecursionLimit)
6163
#endif
6264

63-
PyAPI_FUNC(char *) PyEval_GetFuncName(PyObject *);
64-
PyAPI_FUNC(char *) PyEval_GetFuncDesc(PyObject *);
65+
PyAPI_FUNC(const char *) PyEval_GetFuncName(PyObject *);
66+
PyAPI_FUNC(const char *) PyEval_GetFuncDesc(PyObject *);
6567

6668
PyAPI_FUNC(PyObject *) PyEval_GetCallStats(PyObject *);
6769
PyAPI_FUNC(PyObject *) PyEval_EvalFrame(struct _frame *);

Include/import.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ PyAPI_FUNC(PyObject *) PyImport_ExecCodeModule(char *name, PyObject *co);
1212
PyAPI_FUNC(PyObject *) PyImport_ExecCodeModuleEx(
1313
char *name, PyObject *co, char *pathname);
1414
PyAPI_FUNC(PyObject *) PyImport_GetModuleDict(void);
15-
PyAPI_FUNC(PyObject *) PyImport_AddModule(char *name);
16-
PyAPI_FUNC(PyObject *) PyImport_ImportModule(char *name);
15+
PyAPI_FUNC(PyObject *) PyImport_AddModule(const char *name);
16+
PyAPI_FUNC(PyObject *) PyImport_ImportModule(const char *name);
1717
PyAPI_FUNC(PyObject *) PyImport_ImportModuleEx(
1818
char *name, PyObject *globals, PyObject *locals, PyObject *fromlist);
1919
PyAPI_FUNC(PyObject *) PyImport_Import(PyObject *name);

Include/methodobject.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ PyAPI_FUNC(int) PyCFunction_GetFlags(PyObject *);
3535
PyAPI_FUNC(PyObject *) PyCFunction_Call(PyObject *, PyObject *, PyObject *);
3636

3737
struct PyMethodDef {
38-
char *ml_name; /* The name of the built-in function/method */
38+
const char *ml_name; /* The name of the built-in function/method */
3939
PyCFunction ml_meth; /* The C function that implements it */
4040
int ml_flags; /* Combination of METH_xxx flags, which mostly
4141
describe the args expected by the C func */
42-
char *ml_doc; /* The __doc__ attribute, or NULL */
42+
const char *ml_doc; /* The __doc__ attribute, or NULL */
4343
};
4444
typedef struct PyMethodDef PyMethodDef;
4545

46-
PyAPI_FUNC(PyObject *) Py_FindMethod(PyMethodDef[], PyObject *, char *);
46+
PyAPI_FUNC(PyObject *) Py_FindMethod(PyMethodDef[], PyObject *, const char *);
4747

4848
#define PyCFunction_New(ML, SELF) PyCFunction_NewEx((ML), (SELF), NULL)
4949
PyAPI_FUNC(PyObject *) PyCFunction_NewEx(PyMethodDef *, PyObject *,
@@ -76,7 +76,7 @@ typedef struct PyMethodChain {
7676
} PyMethodChain;
7777

7878
PyAPI_FUNC(PyObject *) Py_FindMethodInChain(PyMethodChain *, PyObject *,
79-
char *);
79+
const char *);
8080

8181
typedef struct {
8282
PyObject_HEAD

Include/modsupport.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ extern "C" {
99

1010
#include <stdarg.h>
1111

12-
PyAPI_FUNC(int) PyArg_Parse(PyObject *, char *, ...);
13-
PyAPI_FUNC(int) PyArg_ParseTuple(PyObject *, char *, ...);
12+
PyAPI_FUNC(int) PyArg_Parse(PyObject *, const char *, ...);
13+
PyAPI_FUNC(int) PyArg_ParseTuple(PyObject *, const char *, ...);
1414
PyAPI_FUNC(int) PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
15-
char *, char **, ...);
16-
PyAPI_FUNC(int) PyArg_UnpackTuple(PyObject *, char *, int, int, ...);
17-
PyAPI_FUNC(PyObject *) Py_BuildValue(char *, ...);
18-
PyAPI_FUNC(int) _PyArg_NoKeywords(char *funcname, PyObject *kw);
15+
const char *, const char **, ...);
16+
PyAPI_FUNC(int) PyArg_UnpackTuple(PyObject *, const char *, int, int, ...);
17+
PyAPI_FUNC(PyObject *) Py_BuildValue(const char *, ...);
18+
PyAPI_FUNC(int) _PyArg_NoKeywords(const char *funcname, PyObject *kw);
1919

20-
PyAPI_FUNC(int) PyArg_VaParse(PyObject *, char *, va_list);
20+
PyAPI_FUNC(int) PyArg_VaParse(PyObject *, const char *, va_list);
2121
PyAPI_FUNC(int) PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
22-
char *, char **, va_list);
23-
PyAPI_FUNC(PyObject *) Py_VaBuildValue(char *, va_list);
22+
const char *, const char **, va_list);
23+
PyAPI_FUNC(PyObject *) Py_VaBuildValue(const char *, va_list);
2424

25-
PyAPI_FUNC(int) PyModule_AddObject(PyObject *, char *, PyObject *);
26-
PyAPI_FUNC(int) PyModule_AddIntConstant(PyObject *, char *, long);
27-
PyAPI_FUNC(int) PyModule_AddStringConstant(PyObject *, char *, char *);
25+
PyAPI_FUNC(int) PyModule_AddObject(PyObject *, const char *, PyObject *);
26+
PyAPI_FUNC(int) PyModule_AddIntConstant(PyObject *, const char *, long);
27+
PyAPI_FUNC(int) PyModule_AddStringConstant(PyObject *, const char *, const char *);
2828

2929
#define PYTHON_API_VERSION 1012
3030
#define PYTHON_API_STRING "1012"
@@ -84,9 +84,9 @@ PyAPI_FUNC(int) PyModule_AddStringConstant(PyObject *, char *, char *);
8484
#define Py_InitModule4 Py_InitModule4TraceRefs
8585
#endif
8686

87-
PyAPI_FUNC(PyObject *) Py_InitModule4(char *name, PyMethodDef *methods,
88-
char *doc, PyObject *self,
89-
int apiver);
87+
PyAPI_FUNC(PyObject *) Py_InitModule4(const char *name, PyMethodDef *methods,
88+
const char *doc, PyObject *self,
89+
int apiver);
9090

9191
#define Py_InitModule(name, methods) \
9292
Py_InitModule4(name, methods, (char *)NULL, (PyObject *)NULL, \

Include/moduleobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ PyAPI_DATA(PyTypeObject) PyModule_Type;
1212
#define PyModule_Check(op) PyObject_TypeCheck(op, &PyModule_Type)
1313
#define PyModule_CheckExact(op) ((op)->ob_type == &PyModule_Type)
1414

15-
PyAPI_FUNC(PyObject *) PyModule_New(char *);
15+
PyAPI_FUNC(PyObject *) PyModule_New(const char *);
1616
PyAPI_FUNC(PyObject *) PyModule_GetDict(PyObject *);
1717
PyAPI_FUNC(char *) PyModule_GetName(PyObject *);
1818
PyAPI_FUNC(char *) PyModule_GetFilename(PyObject *);

Include/object.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ typedef struct {
225225
typedef void (*freefunc)(void *);
226226
typedef void (*destructor)(PyObject *);
227227
typedef int (*printfunc)(PyObject *, FILE *, int);
228-
typedef PyObject *(*getattrfunc)(PyObject *, char *);
228+
typedef PyObject *(*getattrfunc)(PyObject *, const char *);
229229
typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
230-
typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
230+
typedef int (*setattrfunc)(PyObject *, const char *, PyObject *);
231231
typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
232232
typedef int (*cmpfunc)(PyObject *, PyObject *);
233233
typedef PyObject *(*reprfunc)(PyObject *);
@@ -243,7 +243,7 @@ typedef PyObject *(*allocfunc)(struct _typeobject *, int);
243243

244244
typedef struct _typeobject {
245245
PyObject_VAR_HEAD
246-
char *tp_name; /* For printing, in format "<module>.<name>" */
246+
const char *tp_name; /* For printing, in format "<module>.<name>" */
247247
int tp_basicsize, tp_itemsize; /* For allocation */
248248

249249
/* Methods to implement standard operations */
@@ -275,7 +275,7 @@ typedef struct _typeobject {
275275
/* Flags to define presence of optional/expanded features */
276276
long tp_flags;
277277

278-
char *tp_doc; /* Documentation string */
278+
const char *tp_doc; /* Documentation string */
279279

280280
/* Assigned meaning in release 2.0 */
281281
/* call function for all accessible objects */
@@ -379,9 +379,9 @@ PyAPI_FUNC(PyObject *) PyObject_Unicode(PyObject *);
379379
PyAPI_FUNC(int) PyObject_Compare(PyObject *, PyObject *);
380380
PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
381381
PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
382-
PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, char *);
383-
PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, char *, PyObject *);
384-
PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, char *);
382+
PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *);
383+
PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *);
384+
PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *);
385385
PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
386386
PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
387387
PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *);

Modules/_bisectmodule.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ bisect_right(PyObject *self, PyObject *args, PyObject *kw)
4040
int lo = 0;
4141
int hi = -1;
4242
int index;
43-
static char *keywords[] = {"a", "x", "lo", "hi", NULL};
43+
static const char *keywords[] = {"a", "x", "lo", "hi", NULL};
4444

4545
if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|ii:bisect_right",
4646
keywords, &list, &item, &lo, &hi))
@@ -70,7 +70,7 @@ insort_right(PyObject *self, PyObject *args, PyObject *kw)
7070
int lo = 0;
7171
int hi = -1;
7272
int index;
73-
static char *keywords[] = {"a", "x", "lo", "hi", NULL};
73+
static const char *keywords[] = {"a", "x", "lo", "hi", NULL};
7474

7575
if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|ii:insort_right",
7676
keywords, &list, &item, &lo, &hi))
@@ -137,7 +137,7 @@ bisect_left(PyObject *self, PyObject *args, PyObject *kw)
137137
int lo = 0;
138138
int hi = -1;
139139
int index;
140-
static char *keywords[] = {"a", "x", "lo", "hi", NULL};
140+
static const char *keywords[] = {"a", "x", "lo", "hi", NULL};
141141

142142
if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|ii:bisect_left",
143143
keywords, &list, &item, &lo, &hi))
@@ -167,7 +167,7 @@ insort_left(PyObject *self, PyObject *args, PyObject *kw)
167167
int lo = 0;
168168
int hi = -1;
169169
int index;
170-
static char *keywords[] = {"a", "x", "lo", "hi", NULL};
170+
static const char *keywords[] = {"a", "x", "lo", "hi", NULL};
171171

172172
if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|ii:insort_left",
173173
keywords, &list, &item, &lo, &hi))
@@ -233,4 +233,3 @@ init_bisect(void)
233233

234234
m = Py_InitModule3("_bisect", bisect_methods, module_doc);
235235
}
236-

0 commit comments

Comments
 (0)