Commits

Lenard Lindstrom  committed 35c6f1c

replace ambiguous switch statement default clauses with specific comments

In several cases, a do-nothing default clause was used to indicate the
containing switch statement was not exhaustive; unhandled switch values were
expected. This intention was not obvious. These are replaced with
explanatory comments. Other default clauses were used as assertions, or bug
traps, catching invalid switch values and raising a Python SystemError
exception. These are better documented and made conditionally compiled,
like C asserts.

  • Participants
  • Parent commits 9b76f5f

Comments (0)

Files changed (5)

File src/freetype/ft_render.c

 {
     int i = 0;
 
+    /* Check if the format starts with a size/byte order code or a item count */
     switch (format[i]) {
 
     case '@':
     case '7':
     case '8':
     case '9':
+        /* Only allowed for fill bytes */
         if (format[i + 1] == 'x') {
             ++i;
         }
         break;
-    default:
-        /* Unrecognized */
-        break;
+
+    /* default: assume the first character is a format character */
     }
+    /* Verify the next character is a format character */
     switch (format[i]) {
 
     case 'x':
     case 'Q':
         ++i;
         break;
-    default:
-        /* Unrecognized */
-        break;
+
+    /* default: an unrecognized format character; raise exception later */
     }
     if (format[i] != '\0') {
         PyErr_SetString(PyExc_ValueError, "Unsupport array item type");

File src/pixelarray.c

         return -1;
     }
     if (PyBUF_HAS_FLAG(flags, PyBUF_FORMAT)) {
+        /* Find the appropriate format for given pixel size */
         switch (itemsize) {
+            /* This switch statement is exhaustive over possible itemsize
+               values, the supported surface pixel byte sizes */
 
         case 1:
             view_p->format = FormatUint8;
         case 4:
             view_p->format = FormatUint32;
             break;
+
+#ifndef NDEBUG
+            /* Assert that itemsize is valid */
         default:
+            /* Should not get here */
             PyErr_Format(PyExc_SystemError,
                          "Internal Pygame error at line %d in %s: "
                          "unknown item size %d; please report",
                          (int)__LINE__, __FILE__, (int)itemsize);
             return -1;
+#endif
         }
     }
     else {

File src/pixelarray_methods.c

 
     bpp = surf->format->BytesPerPixel;
 
+    /* Find the start of the pixel */
     switch (bpp) {
+        /* This switch statement is exhaustive over all surface pixel sizes */
 
     case 1:
         pixel = (Uint32)*pixel_p;
     case 4:
         pixel = *((Uint32 *)pixel_p);
         break;
-    default: /* Should not get here. */
+
+#ifndef NDEBUG
+        /* Assert that bpp is valid */
+    default:
+        /* Should not get here */
         PyErr_Format(PyExc_SystemError,
                      "Pygame internal error in _get_single_pixel: "
                      "unexpected pixel size %i", bpp);
         return 0;
+#endif
     }
     
     return PyInt_FromLong((long)pixel);

File src/pixelcopy.c

                      intsize);
         return -1;
     }
+    /* Select appropriate color plane element within the pixel */
     switch (view_kind) {
+        /* This switch statement is exhaustive over possible view_kind values */
+
     case VIEWKIND_RED:
         element = &r;
         break;
         break;
     case VIEWKIND_COLORKEY:
         break;
+
+#ifndef NDEBUG
+        /* Assert this switch statement is exhaustive */
     default:
+        /* Should not get here */
         PyErr_Format(PyExc_SystemError,
                      "pygame bug in _copy_colorplane: unknown view kind %d",
                      (int)view_kind);
         return -1;
+#endif
     }
 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
     dz_pix = 0;

File src/surface.c

 
 static PyObject *
 _raise_get_view_ndim_error(int bitsize, SurfViewKind kind) {
-    const char *name;
-
+    const char *name = "<unknown>";  /* guard against a segfault */
+
+    /* Put a human readable name to a surface view kind */
     switch (kind) {
+        /* This switch statement is exhaustive over the SurfViewKind enum */
 
     case VIEWKIND_RAW:
         name = "raw";
         break;
+    case VIEWKIND_0D:
+        name = "contiguous bytes";
+        break;
+    case VIEWKIND_1D:
+        name = "contigous pixels";
+        break;
     case VIEWKIND_2D:
         name = "2D";
         break;
     case VIEWKIND_ALPHA:
         name = "alpha";
         break;
+
+#ifndef NDEBUG
+        /* Assert this switch statement is exhaustive */
     default:
+        /* Should not be here */
         PyErr_Format(PyExc_SystemError,
                      "pygame bug in _raise_get_view_ndim_error:"
                      " unknown view kind %d", (int) kind);
         return 0;
+#endif
     }
     PyErr_Format(PyExc_ValueError,
          "unsupported bit depth %d for %s reference array",
 
     format = surface->format;
     switch (view_kind) {
+        /* This switch statement is exhaustive over the SurfViewKind enum */
 
     case VIEWKIND_0D:
         if (surface->pitch != format->BytesPerPixel * surface->w) {
     case VIEWKIND_RAW:
         get_buffer = _get_buffer_0D;
         break;
+
+#ifndef NDEBUG
+        /* Assert this switch statement is exhaustive */
     default:
+        /* Should not be here */
         PyErr_Format (PyExc_SystemError,
                       "pygame bug in surf_get_view:"
                       " unrecognized view kind %d", (int)view_kind);
         return 0;
+#endif
     }
     assert (get_buffer);
     proxy_obj = PgBufproxy_New (self, get_buffer);
     }
     if (PyBUF_HAS_FLAG (flags, PyBUF_FORMAT)) {
         switch (itemsize) {
+            /* This switch statement is exhaustive over all remaining possible
+               itemsize values, the valid pixel byte sizes of non color-mapped
+               images */
 
         case 2:
             view_p->format = FormatUint16;
         case 4:
             view_p->format = FormatUint32;
             break;
+
+#ifndef NDEBUG
+            /* Assert this switch statement is exhaustive */
         default:
-            /* Should not get here! */
+            /* Should not be here */
             PyErr_Format (PyExc_SystemError,
                           "Pygame bug caught at line %i in file %s: "
                           "unknown pixel size %i. Please report",
                           (int)__LINE__, __FILE__, itemsize);
             return -1;
+#endif
         }
     }
     view_p->buf = surface->pixels;
     }
     if (PyBUF_HAS_FLAG (flags, PyBUF_FORMAT)) {
         switch (itemsize) {
+            /* This switch statement is exhaustive over all possible itemsize
+               values, valid pixel byte sizes */
 
         case 1:
             view_p->format = FormatUint8;
         case 4:
             view_p->format = FormatUint32;
             break;
+
+#ifndef NDEBUG
+            /* Assert this switch statement is exhaustive */
         default:
-            /* Should not get here! */
+            /* Should not be here */
             PyErr_Format (PyExc_SystemError,
                           "Pygame bug caught at line %i in file %s: "
                           "unknown pixel size %i. Please report",
                           (int)__LINE__, __FILE__, itemsize);
             return -1;
+#endif
         }
     }
     view_p->buf = surface->pixels;
         return -1;
     }
     switch (mask) {
+        /* This switch statement is exhaustive over possible mask value,
+           the allowable masks for 24 bit and 32 bit surfaces */
 
     case 0x000000ffU:
         startpixel += lilendian ? 0 : 3;
     case 0xff000000U:
         startpixel += lilendian ? 3 : 0;
         break;
+
+#ifndef NDEBUG
+        /* Assert this switch statement is exhaustive */
     default:
-        /* Should not get here! */
+        /* Should not be here */
         PyErr_Format (PyExc_SystemError,
                       "Pygame bug caught at line %i in file %s: "
                       "unknown mask value %p. Please report",
                       (int)__LINE__, __FILE__, (void *)mask);
         return -1;
+#endif
     }
     if (_init_buffer (obj, pg_view_p, flags)) {
         return -1;