]> granicus.if.org Git - python/commitdiff
_range_error(): Speed and simplify (there's no real need for
authorTim Peters <tim.peters@gmail.com>
Wed, 31 May 2006 15:33:22 +0000 (15:33 +0000)
committerTim Peters <tim.peters@gmail.com>
Wed, 31 May 2006 15:33:22 +0000 (15:33 +0000)
loops here).  Assert that size_t is actually big enough, and
that f->size is at least one.  Wrap a long line.

Modules/_struct.c

index b8a76d291b672bb3382bf6d8497987608228ad40..8065717a5f438f9b97c0fbf359b011212e6a2fd3 100644 (file)
@@ -308,26 +308,27 @@ unpack_double(const char *p,  /* start of 8-byte string */
 static int
 _range_error(const formatdef *f, int is_unsigned)
 {
-       if (is_unsigned == 0) {
-               Py_ssize_t smallest, largest = 0;
-               Py_ssize_t i = f->size * 8;
-               while (--i > 0) {
-                       largest = (largest * 2) + 1;
-               }
-               smallest = -largest - 1;
+       /* ulargest is the largest unsigned value with f->size bytes.
+        * Note that the simpler:
+        *     ((size_t)1 << (f->size * 8)) - 1
+        * doesn't work when f->size == size_t because C doesn't define what
+        * happens when a left shift count is >= the number of bits in the
+        * integer being shifted; e.g., on some boxes it doesn't shift at
+        * all when they're equal.
+        */
+       const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
+       assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
+       if (is_unsigned)
                PyErr_Format(StructError,
-                       "'%c' format requires %zd <= number <= %zd",
+                       "'%c' format requires 0 <= number <= %zu",
                        f->format,
-                       smallest,
-                       largest);
-       } else {
-               size_t largest = 0;
-               Py_ssize_t i = f->size * 8;
-               while (--i >= 0)
-                       largest = (largest * 2) + 1;
+                       ulargest);
+       else {
+               const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
                PyErr_Format(StructError,
-                       "'%c' format requires 0 <= number <= %zu",
+                       "'%c' format requires %zd <= number <= %zd",
                        f->format,
+                       ~ largest,
                        largest);
        }
 #ifdef PY_STRUCT_OVERFLOW_MASKING
@@ -343,7 +344,8 @@ _range_error(const formatdef *f, int is_unsigned)
                Py_XDECREF(ptraceback);
                if (msg == NULL)
                        return -1;
-               rval = PyErr_Warn(PyExc_DeprecationWarning, PyString_AS_STRING(msg));
+               rval = PyErr_Warn(PyExc_DeprecationWarning,
+                                 PyString_AS_STRING(msg));
                Py_DECREF(msg);
                if (rval == 0)
                        return 0;