[Python-checkins] Add the const qualifier to "char *" variables that refer to literal strings. (#4370)

Serhiy Storchaka webhook-mailer at python.org
Sat Nov 11 06:06:34 EST 2017


https://github.com/python/cpython/commit/e2f92de6a90ae11a6d8e01bd17fd88b005516835
commit: e2f92de6a90ae11a6d8e01bd17fd88b005516835
branch: master
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2017-11-11T13:06:26+02:00
summary:

Add the const qualifier to "char *" variables that refer to literal strings. (#4370)

files:
M Modules/_ctypes/callproc.c
M Modules/_cursesmodule.c
M Modules/_sqlite/connection.c
M Modules/_testcapimodule.c
M Modules/fpectlmodule.c
M Modules/gcmodule.c
M Modules/getaddrinfo.c
M Modules/main.c
M Modules/mmapmodule.c
M Modules/ossaudiodev.c
M Objects/bytearrayobject.c
M Objects/classobject.c
M Objects/complexobject.c
M Objects/genobject.c
M Objects/obmalloc.c
M Objects/odictobject.c
M Objects/unicodeobject.c
M PC/bdist_wininst/install.c
M Python/bltinmodule.c
M Python/compile.c
M Python/pyhash.c
M Python/pystrtod.c
M Python/pythonrun.c

diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
index 3a6ad86e87e..bdc37281159 100644
--- a/Modules/_ctypes/callproc.c
+++ b/Modules/_ctypes/callproc.c
@@ -1336,7 +1336,7 @@ static PyObject *py_dl_open(PyObject *self, PyObject *args)
     handle = ctypes_dlopen(name_str, mode);
     Py_XDECREF(name2);
     if (!handle) {
-        char *errmsg = ctypes_dlerror();
+        const char *errmsg = ctypes_dlerror();
         if (!errmsg)
             errmsg = "dlopen() error";
         PyErr_SetString(PyExc_OSError,
diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c
index ae1905b8a90..42f4a85e356 100644
--- a/Modules/_cursesmodule.c
+++ b/Modules/_cursesmodule.c
@@ -96,7 +96,7 @@
 
 /* Release Number */
 
-char *PyCursesVersion = "2.2";
+static const char PyCursesVersion[] = "2.2";
 
 /* Includes */
 
@@ -2562,7 +2562,7 @@ PyCurses_setupterm(PyObject* self, PyObject *args, PyObject* keywds)
     }
 
     if (!initialised_setupterm && setupterm(termstr,fd,&err) == ERR) {
-        char* s = "setupterm: unknown error";
+        const char* s = "setupterm: unknown error";
 
         if (err == 0) {
             s = "setupterm: could not find terminal";
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index 57eee2d32df..3e83fb662bb 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -1550,7 +1550,7 @@ static PyObject *
 pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
 {
     PyObject* exc_type, *exc_value, *exc_tb;
-    char* method_name;
+    const char* method_name;
     PyObject* result;
 
     if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 52108091f56..7a5771921b2 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -2413,7 +2413,7 @@ test_with_docstring(PyObject *self)
 static PyObject *
 test_string_to_double(PyObject *self) {
     double result;
-    char *msg;
+    const char *msg;
 
 #define CHECK_STRING(STR, expected)                             \
     result = PyOS_string_to_double(STR, NULL, NULL);            \
diff --git a/Modules/fpectlmodule.c b/Modules/fpectlmodule.c
index 404f69269aa..42ef0f6072e 100644
--- a/Modules/fpectlmodule.c
+++ b/Modules/fpectlmodule.c
@@ -125,7 +125,8 @@ static void fpe_reset(Sigfunc *handler)
     extern long ieee_handler(const char*, const char*, sigfpe_handler_type);
 #endif
 
-    char *mode="exception", *in="all", *out;
+    const char *mode="exception", *in="all";
+    char *out;
     (void) nonstandard_arithmetic();
     (void) ieee_flags("clearall",mode,in,&out);
     (void) ieee_handler("set","common",(sigfpe_handler_type)handler);
diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c
index 6e26c7a68f6..121eb46012c 100644
--- a/Modules/gcmodule.c
+++ b/Modules/gcmodule.c
@@ -1608,7 +1608,7 @@ _PyGC_DumpShutdownStats(void)
 {
     if (!(_PyRuntime.gc.debug & DEBUG_SAVEALL)
         && _PyRuntime.gc.garbage != NULL && PyList_GET_SIZE(_PyRuntime.gc.garbage) > 0) {
-        char *message;
+        const char *message;
         if (_PyRuntime.gc.debug & DEBUG_UNCOLLECTABLE)
             message = "gc: %zd uncollectable objects at " \
                 "shutdown";
diff --git a/Modules/getaddrinfo.c b/Modules/getaddrinfo.c
index b6fb53cb3d8..06e87bfc8ce 100644
--- a/Modules/getaddrinfo.c
+++ b/Modules/getaddrinfo.c
@@ -342,7 +342,7 @@ getaddrinfo(const char*hostname, const char*servname,
             port = htons((u_short)atoi(servname));
         } else {
             struct servent *sp;
-            char *proto;
+            const char *proto;
 
             proto = NULL;
             switch (pai->ai_socktype) {
diff --git a/Modules/main.c b/Modules/main.c
index 846ecb6170d..54abbcc5474 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -312,7 +312,7 @@ static int
 run_file(FILE *fp, const wchar_t *filename, PyCompilerFlags *p_cf)
 {
     PyObject *unicode, *bytes = NULL;
-    char *filename_str;
+    const char *filename_str;
     int run;
 
     /* call pending calls like signal handlers (SIGINT) */
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index ea7baf4bd08..6cf454573e9 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -1210,7 +1210,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
     DWORD off_lo;       /* lower 32 bits of offset */
     DWORD size_hi;      /* upper 32 bits of size */
     DWORD size_lo;      /* lower 32 bits of size */
-    char *tagname = "";
+    const char *tagname = "";
     DWORD dwErr = 0;
     int fileno;
     HANDLE fh = 0;
diff --git a/Modules/ossaudiodev.c b/Modules/ossaudiodev.c
index 8bb4d0d3d43..58ee71f9010 100644
--- a/Modules/ossaudiodev.c
+++ b/Modules/ossaudiodev.c
@@ -53,7 +53,7 @@ typedef unsigned long uint32_t;
 
 typedef struct {
     PyObject_HEAD
-    char    *devicename;              /* name of the device file */
+    const char *devicename;           /* name of the device file */
     int      fd;                      /* file descriptor */
     int      mode;                    /* file mode (O_RDONLY, etc.) */
     Py_ssize_t icount;                /* input count */
@@ -82,8 +82,8 @@ newossobject(PyObject *arg)
 {
     oss_audio_t *self;
     int fd, afmts, imode;
-    char *devicename = NULL;
-    char *mode = NULL;
+    const char *devicename = NULL;
+    const char *mode = NULL;
 
     /* Two ways to call open():
          open(device, mode) (for consistency with builtin open())
@@ -167,7 +167,7 @@ oss_dealloc(oss_audio_t *self)
 static oss_mixer_t *
 newossmixerobject(PyObject *arg)
 {
-    char *devicename = NULL;
+    const char *devicename = NULL;
     int fd;
     oss_mixer_t *self;
 
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index 83c3549d50b..dc1515a059c 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -1806,7 +1806,8 @@ bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
 /*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
 {
     Py_ssize_t left, right, mysize, byteslen;
-    char *myptr, *bytesptr;
+    char *myptr;
+    const char *bytesptr;
     Py_buffer vbytes;
 
     if (bytes == Py_None) {
@@ -1816,7 +1817,7 @@ bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
     else {
         if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
             return NULL;
-        bytesptr = (char *) vbytes.buf;
+        bytesptr = (const char *) vbytes.buf;
         byteslen = vbytes.len;
     }
     myptr = PyByteArray_AS_STRING(self);
@@ -1847,7 +1848,8 @@ bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
 /*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
 {
     Py_ssize_t left, right, mysize, byteslen;
-    char *myptr, *bytesptr;
+    char *myptr;
+    const char *bytesptr;
     Py_buffer vbytes;
 
     if (bytes == Py_None) {
@@ -1857,7 +1859,7 @@ bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
     else {
         if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
             return NULL;
-        bytesptr = (char *) vbytes.buf;
+        bytesptr = (const char *) vbytes.buf;
         byteslen = vbytes.len;
     }
     myptr = PyByteArray_AS_STRING(self);
@@ -1885,7 +1887,8 @@ bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
 /*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
 {
     Py_ssize_t right, mysize, byteslen;
-    char *myptr, *bytesptr;
+    char *myptr;
+    const char *bytesptr;
     Py_buffer vbytes;
 
     if (bytes == Py_None) {
@@ -1895,7 +1898,7 @@ bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
     else {
         if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
             return NULL;
-        bytesptr = (char *) vbytes.buf;
+        bytesptr = (const char *) vbytes.buf;
         byteslen = vbytes.len;
     }
     myptr = PyByteArray_AS_STRING(self);
diff --git a/Objects/classobject.c b/Objects/classobject.c
index 063c24a7171..e88c95cbfbd 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -543,7 +543,7 @@ instancemethod_repr(PyObject *self)
 {
     PyObject *func = PyInstanceMethod_Function(self);
     PyObject *funcname = NULL , *result = NULL;
-    char *defname = "?";
+    const char *defname = "?";
 
     if (func == NULL) {
         PyErr_BadInternalCall();
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 3bf37ee437f..4bcf2cefd80 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -363,9 +363,9 @@ complex_repr(PyComplexObject *v)
     /* These do not need to be freed. re is either an alias
        for pre or a pointer to a constant.  lead and tail
        are pointers to constants. */
-    char *re = NULL;
-    char *lead = "";
-    char *tail = "";
+    const char *re = NULL;
+    const char *lead = "";
+    const char *tail = "";
 
     if (v->cval.real == 0. && copysign(1.0, v->cval.real)==1.0) {
         /* Real part is +0: just output the imaginary part and do not
diff --git a/Objects/genobject.c b/Objects/genobject.c
index 7793a54fb3d..00a882379fc 100644
--- a/Objects/genobject.c
+++ b/Objects/genobject.c
@@ -152,7 +152,7 @@ gen_send_ex(PyGenObject *gen, PyObject *arg, int exc, int closing)
     PyObject *result;
 
     if (gen->gi_running) {
-        char *msg = "generator already executing";
+        const char *msg = "generator already executing";
         if (PyCoro_CheckExact(gen)) {
             msg = "coroutine already executing";
         }
@@ -186,8 +186,8 @@ gen_send_ex(PyGenObject *gen, PyObject *arg, int exc, int closing)
 
     if (f->f_lasti == -1) {
         if (arg && arg != Py_None) {
-            char *msg = "can't send non-None value to a "
-                        "just-started generator";
+            const char *msg = "can't send non-None value to a "
+                              "just-started generator";
             if (PyCoro_CheckExact(gen)) {
                 msg = NON_INIT_CORO_MSG;
             }
@@ -410,7 +410,7 @@ gen_close(PyGenObject *gen, PyObject *args)
         PyErr_SetNone(PyExc_GeneratorExit);
     retval = gen_send_ex(gen, Py_None, 1, 1);
     if (retval) {
-        char *msg = "generator ignored GeneratorExit";
+        const char *msg = "generator ignored GeneratorExit";
         if (PyCoro_CheckExact(gen)) {
             msg = "coroutine ignored GeneratorExit";
         } else if (PyAsyncGen_CheckExact(gen)) {
diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c
index 7f5306f9dcb..4441c82ad86 100644
--- a/Objects/obmalloc.c
+++ b/Objects/obmalloc.c
@@ -1646,7 +1646,7 @@ _PyMem_DebugCheckAddress(char api, const void *p)
 {
     const uint8_t *q = (const uint8_t *)p;
     char msgbuf[64];
-    char *msg;
+    const char *msg;
     size_t nbytes;
     const uint8_t *tail;
     int i;
@@ -1661,7 +1661,7 @@ _PyMem_DebugCheckAddress(char api, const void *p)
     id = (char)q[-SST];
     if (id != api) {
         msg = msgbuf;
-        snprintf(msg, sizeof(msgbuf), "bad ID: Allocated using API '%c', verified using API '%c'", id, api);
+        snprintf(msgbuf, sizeof(msgbuf), "bad ID: Allocated using API '%c', verified using API '%c'", id, api);
         msgbuf[sizeof(msgbuf)-1] = 0;
         goto error;
     }
diff --git a/Objects/odictobject.c b/Objects/odictobject.c
index afacb36f6b3..5d22ce71588 100644
--- a/Objects/odictobject.c
+++ b/Objects/odictobject.c
@@ -1625,7 +1625,7 @@ odict_init(PyObject *self, PyObject *args, PyObject *kwds)
     if (len == -1)
         return -1;
     if (len > 1) {
-        char *msg = "expected at most 1 arguments, got %d";
+        const char *msg = "expected at most 1 arguments, got %d";
         PyErr_Format(PyExc_TypeError, msg, len);
         return -1;
     }
@@ -2337,7 +2337,7 @@ mutablemapping_update(PyObject *self, PyObject *args, PyObject *kwargs)
     assert(args == NULL || PyTuple_Check(args));
     len = (args != NULL) ? PyTuple_GET_SIZE(args) : 0;
     if (len > 1) {
-        char *msg = "update() takes at most 1 positional argument (%d given)";
+        const char *msg = "update() takes at most 1 positional argument (%d given)";
         PyErr_Format(PyExc_TypeError, msg, len);
         return NULL;
     }
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 194c5bcdcf1..fdc3197470c 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -8396,8 +8396,8 @@ charmap_encoding_error(
     Py_ssize_t collstartpos = *inpos;
     Py_ssize_t collendpos = *inpos+1;
     Py_ssize_t collpos;
-    char *encoding = "charmap";
-    char *reason = "character maps to <undefined>";
+    const char *encoding = "charmap";
+    const char *reason = "character maps to <undefined>";
     charmapencode_result x;
     Py_UCS4 ch;
     int val;
@@ -8928,7 +8928,7 @@ _PyUnicode_TranslateCharmap(PyObject *input,
     /* output buffer */
     _PyUnicodeWriter writer;
     /* error handler */
-    char *reason = "character maps to <undefined>";
+    const char *reason = "character maps to <undefined>";
     PyObject *errorHandler = NULL;
     PyObject *exc = NULL;
     int ignore;
diff --git a/PC/bdist_wininst/install.c b/PC/bdist_wininst/install.c
index 4f9ef6c2cc4..04323ebd170 100644
--- a/PC/bdist_wininst/install.c
+++ b/PC/bdist_wininst/install.c
@@ -1614,7 +1614,7 @@ SelectPythonDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
 
             if (count == 0) {
                 char Buffer[4096];
-                char *msg;
+                const char *msg;
                 if (target_version && target_version[0]) {
                     wsprintf(Buffer,
                              "Python version %s required, which was not found"
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 6215a638c94..8eac0af3520 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -1942,7 +1942,7 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
     /* If we're interactive, use (GNU) readline */
     if (tty) {
         PyObject *po = NULL;
-        char *promptstr;
+        const char *promptstr;
         char *s = NULL;
         PyObject *stdin_encoding = NULL, *stdin_errors = NULL;
         PyObject *stdout_encoding = NULL, *stdout_errors = NULL;
diff --git a/Python/compile.c b/Python/compile.c
index 58a708ce23e..a3ea60d07c0 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -4846,7 +4846,7 @@ compiler_visit_nested_slice(struct compiler *c, slice_ty s,
 static int
 compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
 {
-    char * kindname = NULL;
+    const char * kindname = NULL;
     switch (s->kind) {
     case Index_kind:
         kindname = "index";
diff --git a/Python/pyhash.c b/Python/pyhash.c
index 8a6bd60c105..aa49eeb35bb 100644
--- a/Python/pyhash.c
+++ b/Python/pyhash.c
@@ -196,7 +196,7 @@ _PyHash_Fini(void)
 #ifdef Py_HASH_STATS
     int i;
     Py_ssize_t total = 0;
-    char *fmt = "%2i %8" PY_FORMAT_SIZE_T "d %8" PY_FORMAT_SIZE_T "d\n";
+    const char *fmt = "%2i %8" PY_FORMAT_SIZE_T "d %8" PY_FORMAT_SIZE_T "d\n";
 
     fprintf(stderr, "len   calls    total\n");
     for (i = 1; i <= Py_HASH_STATS_MAX; i++) {
diff --git a/Python/pystrtod.c b/Python/pystrtod.c
index f19d2399b38..9bf93638621 100644
--- a/Python/pystrtod.c
+++ b/Python/pystrtod.c
@@ -597,7 +597,8 @@ Py_LOCAL_INLINE(char *)
 ensure_decimal_point(char* buffer, size_t buf_size, int precision)
 {
     int digit_count, insert_count = 0, convert_to_exp = 0;
-    char *chars_to_insert, *digits_start;
+    const char *chars_to_insert;
+    char *digits_start;
 
     /* search for the first non-digit character */
     char *p = buffer;
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 17ec182b74c..3d63186a36a 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -1300,7 +1300,7 @@ err_input(perrdetail *err)
 {
     PyObject *v, *w, *errtype, *errtext;
     PyObject *msg_obj = NULL;
-    char *msg = NULL;
+    const char *msg = NULL;
     int offset = err->offset;
 
     errtype = PyExc_SyntaxError;



More information about the Python-checkins mailing list