[Python-checkins] cpython (3.4): Closes #21892, #21893: Use PY_FORMAT_SIZE_T instead of %zi or %zu to format C

victor.stinner python-checkins at python.org
Tue Jul 1 08:58:07 CEST 2014


http://hg.python.org/cpython/rev/4f55e802baf0
changeset:   91501:4f55e802baf0
branch:      3.4
parent:      91499:50e924d26ba6
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Tue Jul 01 08:57:10 2014 +0200
summary:
  Closes #21892, #21893: Use PY_FORMAT_SIZE_T instead of %zi or %zu to format C
size_t, because %zi/%u is not supported on all platforms.

files:
  Modules/hashtable.c     |   5 ++-
  Objects/unicodeobject.c |  34 +++++++++++++++++++---------
  2 files changed, 26 insertions(+), 13 deletions(-)


diff --git a/Modules/hashtable.c b/Modules/hashtable.c
--- a/Modules/hashtable.c
+++ b/Modules/hashtable.c
@@ -233,11 +233,12 @@
             nchains++;
         }
     }
-    printf("hash table %p: entries=%zu/%zu (%.0f%%), ",
+    printf("hash table %p: entries=%"
+           PY_FORMAT_SIZE_T "u/%" PY_FORMAT_SIZE_T "u (%.0f%%), ",
            ht, ht->entries, ht->num_buckets, load * 100.0);
     if (nchains)
         printf("avg_chain_len=%.1f, ", (double)total_chain_len / nchains);
-    printf("max_chain_len=%zu, %zu kB\n",
+    printf("max_chain_len=%" PY_FORMAT_SIZE_T "u, %" PY_FORMAT_SIZE_T "u kB\n",
            max_chain_len, size / 1024);
 }
 #endif
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -1011,17 +1011,19 @@
     }
     else
         data = unicode->data.any;
-    printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length);
+    printf("%s: len=%" PY_FORMAT_SIZE_T "u, ",
+           unicode_kind_name(op), ascii->length);
 
     if (ascii->wstr == data)
         printf("shared ");
     printf("wstr=%p", ascii->wstr);
 
     if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) {
-        printf(" (%zu), ", compact->wstr_length);
+        printf(" (%" PY_FORMAT_SIZE_T "u), ", compact->wstr_length);
         if (!ascii->state.compact && compact->utf8 == unicode->data.any)
             printf("shared ");
-        printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length);
+        printf("utf8=%p (%" PY_FORMAT_SIZE_T "u)",
+               compact->utf8, compact->utf8_length);
     }
     printf(", data=%p\n", data);
 }
@@ -1370,8 +1372,9 @@
     how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many);
     if (to_start + how_many > PyUnicode_GET_LENGTH(to)) {
         PyErr_Format(PyExc_SystemError,
-                     "Cannot write %zi characters at %zi "
-                     "in a string of %zi characters",
+                     "Cannot write %" PY_FORMAT_SIZE_T "i characters at %"
+                     PY_FORMAT_SIZE_T "i in a string of %"
+                     PY_FORMAT_SIZE_T "i characters",
                      how_many, to_start, PyUnicode_GET_LENGTH(to));
         return -1;
     }
@@ -4080,7 +4083,9 @@
     if (newpos<0)
         newpos = insize+newpos;
     if (newpos<0 || newpos>insize) {
-        PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos);
+        PyErr_Format(PyExc_IndexError,
+                     "position %" PY_FORMAT_SIZE_T
+                     "d from error handler out of bounds", newpos);
         goto onError;
     }
 
@@ -4173,7 +4178,9 @@
     if (newpos<0)
         newpos = insize+newpos;
     if (newpos<0 || newpos>insize) {
-        PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos);
+        PyErr_Format(PyExc_IndexError,
+                     "position %" PY_FORMAT_SIZE_T
+                     "d from error handler out of bounds", newpos);
         goto onError;
     }
 
@@ -6436,7 +6443,9 @@
     if (*newpos<0)
         *newpos = len + *newpos;
     if (*newpos<0 || *newpos>len) {
-        PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos);
+        PyErr_Format(PyExc_IndexError,
+                     "position %" PY_FORMAT_SIZE_T
+                     "d from error handler out of bounds", *newpos);
         Py_DECREF(restuple);
         return NULL;
     }
@@ -8459,7 +8468,9 @@
     else
         *newpos = i_newpos;
     if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) {
-        PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos);
+        PyErr_Format(PyExc_IndexError,
+                     "position %" PY_FORMAT_SIZE_T
+                     "d from error handler out of bounds", *newpos);
         Py_DECREF(restuple);
         return NULL;
     }
@@ -9741,7 +9752,8 @@
         item = items[i];
         if (!PyUnicode_Check(item)) {
             PyErr_Format(PyExc_TypeError,
-                         "sequence item %zd: expected str instance,"
+                         "sequence item %" PY_FORMAT_SIZE_T
+                         "d: expected str instance,"
                          " %.80s found",
                          i, Py_TYPE(item)->tp_name);
             goto onError;
@@ -14440,7 +14452,7 @@
     default:
         PyErr_Format(PyExc_ValueError,
                      "unsupported format character '%c' (0x%x) "
-                     "at index %zd",
+                     "at index %" PY_FORMAT_SIZE_T "d",
                      (31<=arg->ch && arg->ch<=126) ? (char)arg->ch : '?',
                      (int)arg->ch,
                      ctx->fmtpos - 1);

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


More information about the Python-checkins mailing list