[Python-checkins] cpython: Issue #13706: Add assertions to detect bugs earlier

victor.stinner python-checkins at python.org
Wed Feb 1 00:22:11 CET 2012


http://hg.python.org/cpython/rev/056f5cc8885d
changeset:   74702:056f5cc8885d
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Wed Feb 01 00:22:23 2012 +0100
summary:
  Issue #13706: Add assertions to detect bugs earlier

files:
  Include/unicodeobject.h    |   3 +++
  Python/formatter_unicode.c |  12 +++++++++---
  2 files changed, 12 insertions(+), 3 deletions(-)


diff --git a/Include/unicodeobject.h b/Include/unicodeobject.h
--- a/Include/unicodeobject.h
+++ b/Include/unicodeobject.h
@@ -499,14 +499,17 @@
     do { \
         switch ((kind)) { \
         case PyUnicode_1BYTE_KIND: { \
+            assert(value <= 0xff); \
             ((Py_UCS1 *)(data))[(index)] = (Py_UCS1)(value); \
             break; \
         } \
         case PyUnicode_2BYTE_KIND: { \
+            assert(value <= 0xffff); \
             ((Py_UCS2 *)(data))[(index)] = (Py_UCS2)(value); \
             break; \
         } \
         default: { \
+            assert(value <= 0x10ffff); \
             assert((kind) == PyUnicode_4BYTE_KIND); \
             ((Py_UCS4 *)(data))[(index)] = (Py_UCS4)(value); \
         } \
diff --git a/Python/formatter_unicode.c b/Python/formatter_unicode.c
--- a/Python/formatter_unicode.c
+++ b/Python/formatter_unicode.c
@@ -559,8 +559,9 @@
             Py_ssize_t t;
             for (t = 0; t < spec->n_prefix; t++) {
                 Py_UCS4 c = PyUnicode_READ(kind, data, pos + t);
+                c = Py_TOUPPER(c);
                 assert (c <= 127);
-                PyUnicode_WRITE(kind, data, pos + t, Py_TOUPPER(c));
+                PyUnicode_WRITE(kind, data, pos + t, c);
             }
         }
         pos += spec->n_prefix;
@@ -603,11 +604,12 @@
         Py_ssize_t t;
         for (t = 0; t < spec->n_grouped_digits; t++) {
             Py_UCS4 c = PyUnicode_READ(kind, data, pos + t);
+            c = Py_TOUPPER(c);
             if (c > 127) {
                 PyErr_SetString(PyExc_SystemError, "non-ascii grouped digit");
                 return -1;
             }
-            PyUnicode_WRITE(kind, data, pos + t, Py_TOUPPER(c));
+            PyUnicode_WRITE(kind, data, pos + t, c);
         }
     }
     pos += spec->n_grouped_digits;
@@ -733,6 +735,7 @@
         Py_CLEAR(result);
 
 done:
+    assert(!result || _PyUnicode_CheckConsistency(result, 1));
     return result;
 }
 
@@ -759,7 +762,7 @@
                                    produces non-digits */
     Py_ssize_t n_prefix = 0;   /* Count of prefix chars, (e.g., '0x') */
     Py_ssize_t n_total;
-    Py_ssize_t prefix;
+    Py_ssize_t prefix = 0;
     NumberFieldWidths spec;
     long x;
     int err;
@@ -894,6 +897,7 @@
 
 done:
     Py_XDECREF(tmp);
+    assert(!result || _PyUnicode_CheckConsistency(result, 1));
     return result;
 }
 
@@ -1036,6 +1040,7 @@
 done:
     PyMem_Free(buf);
     Py_DECREF(unicode_tmp);
+    assert(!result || _PyUnicode_CheckConsistency(result, 1));
     return result;
 }
 
@@ -1270,6 +1275,7 @@
     PyMem_Free(im_buf);
     Py_XDECREF(re_unicode_tmp);
     Py_XDECREF(im_unicode_tmp);
+    assert(!result || _PyUnicode_CheckConsistency(result, 1));
     return result;
 }
 

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


More information about the Python-checkins mailing list