[Mesa-dev] [PATCH 17/41] glapi: replace RuntimeError with asserts or Exception

Dylan Baker baker.dylan.c at gmail.com
Fri Apr 1 00:04:34 UTC 2016


RuntimeError is a very specific type of error. In almost very case it's
being raised where an assert is the right choice, and in the rest using
a plain Exception is better, at least there it's obvious it's not that
the python runtime hit an internal error and died.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 src/mapi/glapi/gen/glX_XML.py        | 18 ++++++++++------
 src/mapi/glapi/gen/glX_proto_size.py |  7 +++---
 src/mapi/glapi/gen/gl_XML.py         | 41 +++++++++++++++++++++---------------
 src/mapi/glapi/gen/typeexpr.py       | 36 ++++++++++++++-----------------
 4 files changed, 55 insertions(+), 47 deletions(-)

diff --git a/src/mapi/glapi/gen/glX_XML.py b/src/mapi/glapi/gen/glX_XML.py
index 9586323..5604732 100644
--- a/src/mapi/glapi/gen/glX_XML.py
+++ b/src/mapi/glapi/gen/glX_XML.py
@@ -159,7 +159,9 @@ class glx_function(gl_XML.gl_function):
                     self.server_handcode = 1
                     self.client_handcode = 0
                 else:
-                    raise RuntimeError('Invalid handcode mode "%s" in function "%s".' % (handcode, self.name))
+                    raise Exception(
+                        'Invalid handcode mode "{}" in function "{}".'.format(
+                            handcode, self.name))
 
                 self.ignore = gl_XML.is_attr_true(child, 'ignore')
                 self.can_be_large = gl_XML.is_attr_true(child, 'large')
@@ -171,8 +173,9 @@ class glx_function(gl_XML.gl_function):
         # new tests are discovered, they should be added here.
 
         for param in self.parameters:
-            if param.is_output and self.glx_rop != 0:
-                raise RuntimeError("Render / RenderLarge commands cannot have outputs (%s)." % (self.name))
+            assert not (param.is_output and self.glx_rop != 0), \
+                "Render / RenderLarge commands cannot have outputs ({}).".format(
+                    self.name)
 
     def has_variable_size_request(self):
         """Determine if the GLX request packet is variable sized.
@@ -236,7 +239,10 @@ class glx_function(gl_XML.gl_function):
                 elif dim <= 4:
                     offset = 36
                 else:
-                    raise RuntimeError('Invalid number of dimensions %u for parameter "%s" in function "%s".' % (dim, self.image.name, self.name))
+                    raise Exception(
+                        'Invalid number of dimensions {} for parameter '
+                        '"{}" in function "{}".'.format(
+                            dim, self.image.name, self.name))
             else:
                 offset = 0
 
@@ -412,13 +418,13 @@ class glx_function(gl_XML.gl_function):
         elif self.glx_vendorpriv != 0:
             return "X_GLvop_%s" % (self.name)
         else:
-            raise RuntimeError('Function "%s" has no opcode.' % (self.name))
+            raise Exception('Function "%s" has no opcode.' % (self.name))
 
     def opcode_vendor_name(self, name):
         if name in self.glx_vendorpriv_names:
             return "X_GLvop_%s" % (name)
         else:
-            raise RuntimeError('Function "%s" has no VendorPrivate opcode.' % (name))
+            raise Exception('Function "%s" has no VendorPrivate opcode.' % (name))
 
     def opcode_real_name(self):
         """Get the true protocol enum name for the GLX opcode
diff --git a/src/mapi/glapi/gen/glX_proto_size.py b/src/mapi/glapi/gen/glX_proto_size.py
index e1e1a74..b8625a6 100644
--- a/src/mapi/glapi/gen/glX_proto_size.py
+++ b/src/mapi/glapi/gen/glX_proto_size.py
@@ -74,8 +74,8 @@ class glx_enum_function(object):
             if match_name in e.functions:
                 [count, mode] = e.functions[match_name]
 
-                if mode_set and mode != self.mode:
-                    raise RuntimeError("Not all enums for %s have the same mode." % (func_name))
+                assert not (mode_set and mode != self.mode), \
+                    "Not all enums for %s have the same mode." % (func_name)
 
                 self.mode = mode
 
@@ -93,8 +93,7 @@ class glx_enum_function(object):
         if self.sig is None:
             self.sig = ""
             for i in self.count:
-                if i is None:
-                    raise RuntimeError("i is None.  WTF?")
+                assert i is not None, 'Impossible count of None'
 
                 self.count[i].sort()
                 for e in self.count[i]:
diff --git a/src/mapi/glapi/gen/gl_XML.py b/src/mapi/glapi/gen/gl_XML.py
index 7e08c3b..d273ff0 100644
--- a/src/mapi/glapi/gen/gl_XML.py
+++ b/src/mapi/glapi/gen/gl_XML.py
@@ -274,7 +274,7 @@ def classify_category(name, number):
 
     try:
         core_version = float(name)
-    except Exception, e:
+    except (ValueError, TypeError):
         core_version = 0.0
 
     if core_version > 0.0:
@@ -352,8 +352,11 @@ class gl_enum(gl_item):
         else:
             try:
                 c = int(temp)
-            except Exception, e:
-                raise RuntimeError('Invalid count value "%s" for enum "%s" in function "%s" when an integer was expected.' % (temp, self.name, n))
+            except (TypeError, ValueError):
+                raise Exception(
+                    'invalid count value "{}" for enum "{}" in function '
+                    '"{}" when an integer was expected.'.format(
+                        c, temp, self.name))
 
             self.default_count = c
 
@@ -410,7 +413,7 @@ class gl_parameter(object):
             count = int(c)
             self.count = count
             self.counter = None
-        except Exception, e:
+        except (ValueError, TypeError):
             count = 1
             self.count = 0
             self.counter = c
@@ -652,8 +655,9 @@ class gl_function(gl_item):
 
         if not self.name:
             self.name = true_name
-        elif self.name != true_name:
-            raise RuntimeError("Function true name redefined.  Was %s, now %s." % (self.name, true_name))
+        assert self.name == true_name, \
+            'Function true name redefined. Was "{}", now "{}".'.format(
+                self.name, true_name)
 
         # There are two possible cases.  The first time an entry-point
         # with data is seen, self.initialized will be False.  On that
@@ -672,17 +676,21 @@ class gl_function(gl_item):
                 parameters.append(param)
 
         if self.initialized:
-            if self.return_type != return_type:
-                raise RuntimeError("Return type changed in %s.  Was %s, now %s." % (name, self.return_type, return_type))
-
-            if len(parameters) != len(self.parameters):
-                raise RuntimeError("Parameter count mismatch in %s.  Was %d, now %d." % (name, len(self.parameters), len(parameters)))
+            assert self.return_type == return_type, \
+                'Return type changed in "{}". Was "{}", now "{}".'.format(
+                    name, self.return_type, return_type)
+            assert len(parameters) == len(self.parameters), \
+                'Parameter count mismatch in "{}".  Was "{}", now "{}".'.format(
+                    name, len(self.parameters), len(parameters))
 
             for j in xrange(len(parameters)):
                 p1 = parameters[j]
                 p2 = self.parameters[j]
-                if not p1.compatible(p2):
-                    raise RuntimeError('Parameter type mismatch in %s.  "%s" was "%s", now "%s".' % (name, p2.name, p2.type_expr.original_string, p1.type_expr.original_string))
+                assert p1.compatible(p2), \
+                    ('Parameter type mismatch in "{}". '
+                     '"{}" was "{}", now "{}".'.format(
+                         name, p2.name, p2.type_expr.original_string,
+                         p1.type_expr.original_string))
 
         if true_name == name or not self.initialized:
             self.return_type = return_type
@@ -700,8 +708,7 @@ class gl_function(gl_item):
 
     def filter_entry_points(self, entry_point_list):
         """Filter out entry points not in entry_point_list."""
-        if not self.initialized:
-            raise RuntimeError('%s is not initialized yet' % self.name)
+        assert self.initialized, '{} is not initialized yet'.format(self.name)
 
         entry_points = []
         for ent in self.entry_points:
@@ -712,8 +719,8 @@ class gl_function(gl_item):
             else:
                 entry_points.append(ent)
 
-        if not entry_points:
-            raise RuntimeError('%s has no entry point after filtering' % self.name)
+        assert entry_points, \
+            '{} has no entry point after filtering'.format(self.name)
 
         self.entry_points = entry_points
         if self.name not in entry_points:
diff --git a/src/mapi/glapi/gen/typeexpr.py b/src/mapi/glapi/gen/typeexpr.py
index adb54c1..9a900a0 100644
--- a/src/mapi/glapi/gen/typeexpr.py
+++ b/src/mapi/glapi/gen/typeexpr.py
@@ -117,8 +117,9 @@ class type_expression(object):
 
         self.original_string = type_string
 
-        if not type_expression.built_in_types:
-            raise RuntimeError("create_initial_types must be called before creating type_expression objects.")
+        assert type_expression.built_in_types, \
+            ("create_initial_types must be called before "
+             "creating type_expression objects.")
 
         # Replace '*' with ' * ' in type_string.  Then, split the string
         # into tokens, separated by spaces.
@@ -151,43 +152,38 @@ class type_expression(object):
                     signed = 0
                     unsigned = 0
 
-                if not self.expr:
-                    raise RuntimeError("Invalid type expression (dangling pointer)")
+                assert self.expr, "Invalid type expression (dangling pointer)"
 
-                if signed:
-                    raise RuntimeError("Invalid type expression (signed / unsigned applied to pointer)")
+                assert not signed, \
+                    "Invalid type expression (signed / unsigned applied to pointer)"
 
                 t = type_node()
                 t.pointer = 1
                 self.expr.append(t)
             else:
-                if self.expr:
-                    raise RuntimeError('Invalid type expression (garbage after pointer qualifier -> "%s")' % (self.original_string))
+                assert not self.expr, \
+                    ('Invalid type expression '
+                     '(garbage after pointer qualifier -> "{}")'.format(
+                         self.original_string))
 
                 self.set_base_type(i, signed, unsigned, const, extra_types)
                 const = 0
                 signed = 0
                 unsigned = 0
 
-            if signed and unsigned:
-                raise RuntimeError("Invalid type expression (both signed and unsigned specified)")
+            assert not (signed and unsigned), \
+                "Invalid type expression (both signed and unsigned specified)"
 
-        if const:
-            raise RuntimeError("Invalid type expression (dangling const)")
-
-        if unsigned:
-            raise RuntimeError("Invalid type expression (dangling signed)")
-
-        if signed:
-            raise RuntimeError("Invalid type expression (dangling unsigned)")
+        assert not const, "Invalid type expression (dangling const)"
+        assert not unsigned, "Invalid type expression (dangling signed)"
+        assert not signed, "Invalid type expression (dangling unsigned)"
 
     def set_base_type(self, type_name, signed, unsigned, const, extra_types):
         te = type_expression.built_in_types.find_type(type_name)
         if not te:
             te = extra_types.find_type(type_name)
 
-        if not te:
-            raise RuntimeError('Unknown base type "%s".' % (type_name))
+        assert te, 'Unknown base type "{}".'.format(type_name)
 
         self.expr = copy.deepcopy(te.expr)
 
-- 
2.8.0



More information about the mesa-dev mailing list