[Python-checkins] cpython: Added the const qualifier to char* variables that refer to readonly internal

serhiy.storchaka python-checkins at python.org
Sun Nov 20 03:17:34 EST 2016


https://hg.python.org/cpython/rev/6e5404da46b1
changeset:   105210:6e5404da46b1
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sun Nov 20 10:16:47 2016 +0200
summary:
  Added the const qualifier to char* variables that refer to readonly internal
UTF-8 represenatation of Unicode objects.

files:
  Modules/_ctypes/_ctypes.c    |   4 ++--
  Modules/_ctypes/stgdict.c    |   4 ++--
  Modules/_elementtree.c       |   2 +-
  Modules/_io/bufferedio.c     |   2 +-
  Modules/_io/stringio.c       |   2 +-
  Modules/_pickle.c            |   4 ++--
  Modules/_sqlite/connection.c |   2 +-
  Modules/_sqlite/row.c        |   8 ++++----
  Modules/_sqlite/statement.c  |   2 +-
  Modules/_tkinter.c           |   6 +++---
  Modules/socketmodule.c       |   2 +-
  Modules/syslogmodule.c       |   2 +-
  Objects/fileobject.c         |   8 +++-----
  Objects/floatobject.c        |   6 +++---
  Objects/longobject.c         |   3 ++-
  Objects/moduleobject.c       |   4 ++--
  Objects/setobject.c          |   2 +-
  Objects/structseq.c          |   2 +-
  Objects/typeobject.c         |   4 ++--
  Python/bltinmodule.c         |   4 ++--
  Python/ceval.c               |  10 +++++-----
  Python/codecs.c              |   2 +-
  Python/compile.c             |   2 +-
  Python/getargs.c             |  14 +++++++-------
  Python/import.c              |   2 +-
  Python/pylifecycle.c         |   6 +++---
  Python/pythonrun.c           |   6 +++---
  Python/structmember.c        |   2 +-
  Python/sysmodule.c           |   4 ++--
  29 files changed, 60 insertions(+), 61 deletions(-)


diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -1829,7 +1829,7 @@
 PyCSimpleType_paramfunc(CDataObject *self)
 {
     StgDictObject *dict;
-    char *fmt;
+    const char *fmt;
     PyCArgObject *parg;
     struct fielddesc *fd;
 
@@ -2039,7 +2039,7 @@
 PyCSimpleType_from_param(PyObject *type, PyObject *value)
 {
     StgDictObject *dict;
-    char *fmt;
+    const char *fmt;
     PyCArgObject *parg;
     struct fielddesc *fd;
     PyObject *as_parameter;
diff --git a/Modules/_ctypes/stgdict.c b/Modules/_ctypes/stgdict.c
--- a/Modules/_ctypes/stgdict.c
+++ b/Modules/_ctypes/stgdict.c
@@ -488,8 +488,8 @@
             bitsize = 0;
 
         if (isStruct && !isPacked) {
-            char *fieldfmt = dict->format ? dict->format : "B";
-            char *fieldname = PyUnicode_AsUTF8(name);
+            const char *fieldfmt = dict->format ? dict->format : "B";
+            const char *fieldname = PyUnicode_AsUTF8(name);
             char *ptr;
             Py_ssize_t len;
             char *buf;
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -3613,7 +3613,7 @@
 
     for (i = 0; i < PySequence_Size(events_seq); ++i) {
         PyObject *event_name_obj = PySequence_Fast_GET_ITEM(events_seq, i);
-        char *event_name = NULL;
+        const char *event_name = NULL;
         if (PyUnicode_Check(event_name_obj)) {
             event_name = PyUnicode_AsUTF8(event_name_obj);
         } else if (PyBytes_Check(event_name_obj)) {
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -305,7 +305,7 @@
             "could not acquire lock for %A at interpreter "
             "shutdown, possibly due to daemon threads",
             (PyObject *) self);
-        char *msg = PyUnicode_AsUTF8(msgobj);
+        const char *msg = PyUnicode_AsUTF8(msgobj);
         Py_FatalError(msg);
     }
     return 1;
diff --git a/Modules/_io/stringio.c b/Modules/_io/stringio.c
--- a/Modules/_io/stringio.c
+++ b/Modules/_io/stringio.c
@@ -693,7 +693,7 @@
                            PyObject *newline_obj)
 /*[clinic end generated code: output=a421ea023b22ef4e input=cee2d9181b2577a3]*/
 {
-    char *newline = "\n";
+    const char *newline = "\n";
     Py_ssize_t value_len;
 
     /* Parse the newline argument. We only want to allow unicode objects or
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -1941,7 +1941,7 @@
             goto error;
     }
     else {
-        char *string;
+        const char *string;
 
         /* proto < 2: write the repr and newline.  This is quadratic-time (in
            the number of digits), in both directions.  We add a trailing 'L'
@@ -2218,7 +2218,7 @@
 {
     PyObject *encoded = NULL;
     Py_ssize_t size;
-    char *data;
+    const char *data;
     int r;
 
     if (PyUnicode_READY(obj))
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -1489,7 +1489,7 @@
     PyObject* retval;
     Py_ssize_t i, len;
     _Py_IDENTIFIER(upper);
-    char *uppercase_name_str;
+    const char *uppercase_name_str;
     int rc;
     unsigned int kind;
     void *data;
diff --git a/Modules/_sqlite/row.c b/Modules/_sqlite/row.c
--- a/Modules/_sqlite/row.c
+++ b/Modules/_sqlite/row.c
@@ -79,12 +79,12 @@
 PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
 {
     Py_ssize_t _idx;
-    char* key;
+    const char *key;
     Py_ssize_t nitems, i;
-    char* compare_key;
+    const char *compare_key;
 
-    char* p1;
-    char* p2;
+    const char *p1;
+    const char *p2;
 
     PyObject* item;
 
diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c
--- a/Modules/_sqlite/statement.c
+++ b/Modules/_sqlite/statement.c
@@ -112,7 +112,7 @@
 int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter)
 {
     int rc = SQLITE_OK;
-    char* string;
+    const char *string;
     Py_ssize_t buflen;
     parameter_type paramtype;
 
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
--- a/Modules/_tkinter.c
+++ b/Modules/_tkinter.c
@@ -536,7 +536,7 @@
     else if (PyUnicode_Check(arg)) {
         int argc;
         const char **argv;
-        char *list = PyUnicode_AsUTF8(arg);
+        const char *list = PyUnicode_AsUTF8(arg);
 
         if (list == NULL ||
             Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) {
@@ -993,7 +993,7 @@
     Tcl_Obj *result;
     int neg;
     PyObject *hexstr;
-    char *hexchars;
+    const char *hexchars;
     mp_int bigValue;
 
     neg = Py_SIZE(value) < 0;
@@ -1723,7 +1723,7 @@
 static int
 varname_converter(PyObject *in, void *_out)
 {
-    char *s;
+    const char *s;
     const char **out = (const char**)_out;
     if (PyBytes_Check(in)) {
         if (PyBytes_GET_SIZE(in) > INT_MAX) {
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -5982,7 +5982,7 @@
     PyObject *hobj = NULL;
     PyObject *pobj = (PyObject *)NULL;
     char pbuf[30];
-    char *hptr, *pptr;
+    const char *hptr, *pptr;
     int family, socktype, protocol, flags;
     int error;
     PyObject *all = (PyObject *)NULL;
diff --git a/Modules/syslogmodule.c b/Modules/syslogmodule.c
--- a/Modules/syslogmodule.c
+++ b/Modules/syslogmodule.c
@@ -116,7 +116,7 @@
     long facility = LOG_USER;
     PyObject *new_S_ident_o = NULL;
     static char *keywords[] = {"ident", "logoption", "facility", 0};
-    char *ident = NULL;
+    const char *ident = NULL;
 
     if (!PyArg_ParseTupleAndKeywords(args, kwds,
                           "|Ull:openlog", keywords, &new_S_ident_o, &logopt, &facility))
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -367,7 +367,7 @@
 {
     PyObject *unicode;
     PyObject *bytes = NULL;
-    char *str;
+    const char *str;
     Py_ssize_t n;
     int err;
 
@@ -389,10 +389,8 @@
         bytes = _PyUnicode_AsUTF8String(unicode, "backslashreplace");
         if (bytes == NULL)
             return NULL;
-        if (PyBytes_AsStringAndSize(bytes, &str, &n) < 0) {
-            Py_DECREF(bytes);
-            return NULL;
-        }
+        str = PyBytes_AS_STRING(bytes);
+        n = PyBytes_GET_SIZE(bytes);
     }
 
     n = _Py_write(self->fd, str, n);
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -1225,7 +1225,7 @@
     PyObject *result;
     double x;
     long exp, top_exp, lsb, key_digit;
-    char *s, *coeff_start, *s_store, *coeff_end, *exp_start, *s_end;
+    const char *s, *coeff_start, *s_store, *coeff_end, *exp_start, *s_end;
     int half_eps, digit, round_up, negate=0;
     Py_ssize_t length, ndigits, fdigits, i;
 
@@ -1288,7 +1288,7 @@
         s++;
 
     /* infinities and nans */
-    x = _Py_parse_inf_or_nan(s, &coeff_end);
+    x = _Py_parse_inf_or_nan(s, (char **)&coeff_end);
     if (coeff_end != s) {
         s = coeff_end;
         goto finished;
@@ -1619,7 +1619,7 @@
 static PyObject *
 float_getformat(PyTypeObject *v, PyObject* arg)
 {
-    char* s;
+    const char *s;
     float_format_type r;
 
     if (!PyUnicode_Check(arg)) {
diff --git a/Objects/longobject.c b/Objects/longobject.c
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -2491,7 +2491,8 @@
 PyLong_FromUnicodeObject(PyObject *u, int base)
 {
     PyObject *result, *asciidig;
-    char *buffer, *end = NULL;
+    const char *buffer;
+    char *end = NULL;
     Py_ssize_t buflen;
 
     asciidig = _PyUnicode_TransformDecimalAndSpaceToASCII(u);
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -231,7 +231,7 @@
     PyObject *nameobj;
     PyObject *m = NULL;
     int has_execution_slots = 0;
-    char *name;
+    const char *name;
     int ret;
 
     PyModuleDef_Init(def);
@@ -512,7 +512,7 @@
 PyModule_GetFilename(PyObject *m)
 {
     PyObject *fileobj;
-    char *utf8;
+    const char *utf8;
     fileobj = PyModule_GetFilenameObject(m);
     if (fileobj == NULL)
         return NULL;
diff --git a/Objects/setobject.c b/Objects/setobject.c
--- a/Objects/setobject.c
+++ b/Objects/setobject.c
@@ -2398,7 +2398,7 @@
 test_c_api(PySetObject *so)
 {
     Py_ssize_t count;
-    char *s;
+    const char *s;
     Py_ssize_t i;
     PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
     PyObject *ob = (PyObject *)so;
diff --git a/Objects/structseq.c b/Objects/structseq.c
--- a/Objects/structseq.c
+++ b/Objects/structseq.c
@@ -182,7 +182,7 @@
 
     for (i=0; i < VISIBLE_SIZE(obj); i++) {
         PyObject *val, *repr;
-        char *cname, *crepr;
+        const char *cname, *crepr;
 
         cname = typ->tp_members[i].name;
         if (cname == NULL) {
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -1624,7 +1624,7 @@
     i = 0;
     while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
         PyObject *name = class_name(k);
-        char *name_str;
+        const char *name_str;
         if (name != NULL) {
             name_str = PyUnicode_AsUTF8(name);
             if (name_str == NULL)
@@ -2572,7 +2572,7 @@
         PyObject *doc = _PyDict_GetItemId(dict, &PyId___doc__);
         if (doc != NULL && PyUnicode_Check(doc)) {
             Py_ssize_t len;
-            char *doc_str;
+            const char *doc_str;
             char *tp_doc;
 
             doc_str = PyUnicode_AsUTF8(doc);
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -1893,7 +1893,7 @@
         char *s = NULL;
         PyObject *stdin_encoding = NULL, *stdin_errors = NULL;
         PyObject *stdout_encoding = NULL, *stdout_errors = NULL;
-        char *stdin_encoding_str, *stdin_errors_str;
+        const char *stdin_encoding_str, *stdin_errors_str;
         PyObject *result;
         size_t len;
 
@@ -1914,7 +1914,7 @@
             Py_DECREF(tmp);
         if (prompt != NULL) {
             /* We have a prompt, encode it as stdout would */
-            char *stdout_encoding_str, *stdout_errors_str;
+            const char *stdout_encoding_str, *stdout_errors_str;
             PyObject *stringpo;
             stdout_encoding = _PyObject_GetAttrId(fout, &PyId_encoding);
             stdout_errors = _PyObject_GetAttrId(fout, &PyId_errors);
diff --git a/Python/ceval.c b/Python/ceval.c
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -5440,8 +5440,8 @@
 static void
 dtrace_function_entry(PyFrameObject *f)
 {
-    char* filename;
-    char* funcname;
+    const char *filename;
+    const char *funcname;
     int lineno;
 
     filename = PyUnicode_AsUTF8(f->f_code->co_filename);
@@ -5454,8 +5454,8 @@
 static void
 dtrace_function_return(PyFrameObject *f)
 {
-    char* filename;
-    char* funcname;
+    const char *filename;
+    const char *funcname;
     int lineno;
 
     filename = PyUnicode_AsUTF8(f->f_code->co_filename);
@@ -5471,7 +5471,7 @@
                   int *instr_lb, int *instr_ub, int *instr_prev)
 {
     int line = frame->f_lineno;
-    char *co_filename, *co_name;
+    const char *co_filename, *co_name;
 
     /* If the last instruction executed isn't in the current
        instruction window, reset the window.
diff --git a/Python/codecs.c b/Python/codecs.c
--- a/Python/codecs.c
+++ b/Python/codecs.c
@@ -1131,7 +1131,7 @@
     PyObject *restuple;
     PyObject *object;
     PyObject *encode;
-    char *encoding;
+    const char *encoding;
     int code;
     int bytelength;
     Py_ssize_t i;
diff --git a/Python/compile.c b/Python/compile.c
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -4039,7 +4039,7 @@
 static int
 expr_constant(struct compiler *c, expr_ty e)
 {
-    char *id;
+    const char *id;
     switch (e->kind) {
     case Ellipsis_kind:
         return 1;
diff --git a/Python/getargs.c b/Python/getargs.c
--- a/Python/getargs.c
+++ b/Python/getargs.c
@@ -74,7 +74,7 @@
                                 int *, char *, size_t, int, freelist_t *);
 static const char *convertsimple(PyObject *, const char **, va_list *, int,
                                  char *, size_t, freelist_t *);
-static Py_ssize_t convertbuffer(PyObject *, void **p, const char **);
+static Py_ssize_t convertbuffer(PyObject *, const void **p, const char **);
 static int getbuffer(PyObject *, Py_buffer *, const char**);
 
 static int vgetargskeywords(PyObject *, PyObject *,
@@ -625,7 +625,7 @@
 
     const char *format = *p_format;
     char c = *format++;
-    char *sarg;
+    const char *sarg;
 
     switch (c) {
 
@@ -897,7 +897,7 @@
             }
             break;
         }
-        count = convertbuffer(arg, p, &buf);
+        count = convertbuffer(arg, (const void **)p, &buf);
         if (count < 0)
             return converterr(buf, arg, msgbuf, bufsize);
         if (*format == '#') {
@@ -928,7 +928,7 @@
                 if (sarg == NULL)
                     return converterr(CONV_UNICODE,
                                       arg, msgbuf, bufsize);
-                PyBuffer_FillInfo(p, arg, sarg, len, 1, 0);
+                PyBuffer_FillInfo(p, arg, (void *)sarg, len, 1, 0);
             }
             else { /* any bytes-like object */
                 const char *buf;
@@ -943,7 +943,7 @@
             format++;
         } else if (*format == '#') { /* a string or read-only bytes-like object */
             /* "s#" or "z#" */
-            void **p = (void **)va_arg(*p_va, char **);
+            const void **p = (const void **)va_arg(*p_va, const char **);
             FETCH_SIZE;
 
             if (c == 'z' && arg == Py_None) {
@@ -970,7 +970,7 @@
             format++;
         } else {
             /* "s" or "z" */
-            char **p = va_arg(*p_va, char **);
+            const char **p = va_arg(*p_va, const char **);
             Py_ssize_t len;
             sarg = NULL;
 
@@ -1300,7 +1300,7 @@
 }
 
 static Py_ssize_t
-convertbuffer(PyObject *arg, void **p, const char **errmsg)
+convertbuffer(PyObject *arg, const void **p, const char **errmsg)
 {
     PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer;
     Py_ssize_t count;
diff --git a/Python/import.c b/Python/import.c
--- a/Python/import.c
+++ b/Python/import.c
@@ -1035,7 +1035,7 @@
 {
     struct _inittab *p;
     PyObject *name;
-    char *namestr;
+    const char *namestr;
     PyObject *mod;
 
     name = PyObject_GetAttrString(spec, "name");
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -193,7 +193,8 @@
 static char*
 get_codec_name(const char *encoding)
 {
-    char *name_utf8, *name_str;
+    const char *name_utf8;
+    char *name_str;
     PyObject *codec, *name = NULL;
 
     codec = _PyCodec_Lookup(encoding);
@@ -1284,8 +1285,7 @@
        when import.c tries to write to stderr in verbose mode. */
     encoding_attr = PyObject_GetAttrString(std, "encoding");
     if (encoding_attr != NULL) {
-        const char * std_encoding;
-        std_encoding = PyUnicode_AsUTF8(encoding_attr);
+        const char *std_encoding = PyUnicode_AsUTF8(encoding_attr);
         if (std_encoding != NULL) {
             PyObject *codec_info = _PyCodec_Lookup(std_encoding);
             Py_XDECREF(codec_info);
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -154,7 +154,7 @@
     PyObject *m, *d, *v, *w, *oenc = NULL, *mod_name;
     mod_ty mod;
     PyArena *arena;
-    char *ps1 = "", *ps2 = "", *enc = NULL;
+    const char *ps1 = "", *ps2 = "", *enc = NULL;
     int errcode = 0;
     _Py_IDENTIFIER(encoding);
     _Py_IDENTIFIER(__main__);
@@ -511,8 +511,8 @@
 static void
 print_error_text(PyObject *f, int offset, PyObject *text_obj)
 {
-    char *text;
-    char *nl;
+    const char *text;
+    const char *nl;
 
     text = PyUnicode_AsUTF8(text_obj);
     if (text == NULL)
diff --git a/Python/structmember.c b/Python/structmember.c
--- a/Python/structmember.c
+++ b/Python/structmember.c
@@ -249,7 +249,7 @@
         Py_XDECREF(oldv);
         break;
     case T_CHAR: {
-        char *string;
+        const char *string;
         Py_ssize_t len;
 
         string = PyUnicode_AsUTF8AndSize(v, &len);
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -108,7 +108,7 @@
 {
     PyObject *stdout_encoding = NULL;
     PyObject *encoded, *escaped_str, *repr_str, *buffer, *result;
-    char *stdout_encoding_str;
+    const char *stdout_encoding_str;
     int ret;
 
     stdout_encoding = _PyObject_GetAttrId(outf, &PyId_encoding);
@@ -2404,7 +2404,7 @@
 {
     PyObject *file, *message;
     PyObject *error_type, *error_value, *error_traceback;
-    char *utf8;
+    const char *utf8;
 
     PyErr_Fetch(&error_type, &error_value, &error_traceback);
     file = _PySys_GetObjectId(key);

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list