Rev 4718: Add a test_source test, to ensure that all funcs have either an except in http://bazaar.launchpad.net/~jameinel/bzr/2.0.4-pyrex-propagation

John Arbash Meinel john at arbash-meinel.com
Mon Jan 4 23:02:22 GMT 2010


At http://bazaar.launchpad.net/~jameinel/bzr/2.0.4-pyrex-propagation

------------------------------------------------------------
revno: 4718
revision-id: john at arbash-meinel.com-20100104230207-xcijrzo22fowu9jg
parent: john at arbash-meinel.com-20100104222957-qxulyfwqtb1s7hw7
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.0.4-pyrex-propagation
timestamp: Mon 2010-01-04 17:02:07 -0600
message:
  Add a test_source test, to ensure that all funcs have either an except
  clause, or are documented as not raising an exception.
-------------- next part --------------
=== modified file 'bzrlib/_annotator_pyx.pyx'
--- a/bzrlib/_annotator_pyx.pyx	2010-01-04 22:29:57 +0000
+++ b/bzrlib/_annotator_pyx.pyx	2010-01-04 23:02:07 +0000
@@ -83,7 +83,7 @@
     return 0
 
 
-cdef PyObject *_next_tuple_entry(object tpl, Py_ssize_t *pos):
+cdef PyObject *_next_tuple_entry(object tpl, Py_ssize_t *pos): # no except
     """Return the next entry from this tuple.
 
     :param tpl: The tuple we are investigating, *must* be a PyTuple

=== modified file 'bzrlib/tests/test_source.py'
--- a/bzrlib/tests/test_source.py	2009-09-10 03:44:53 +0000
+++ b/bzrlib/tests/test_source.py	2010-01-04 23:02:07 +0000
@@ -369,3 +369,46 @@
             self.fail(
                 "these files contain an assert statement and should not:\n%s"
                 % '\n'.join(badfiles))
+
+    def test_extension_exceptions(self):
+        """Extension functions should propagate exceptions.
+
+        Either they should return an object, have an 'except' clause, or have a
+        #no except to indicate that we've audited them and defined them as not
+        raising exceptions.
+        """
+        both_exc_and_no_exc = []
+        missing_except = []
+        except_re = re.compile(r'\s*cdef\s*' # start with cdef
+                               r'([\w *]*?)\s*' # this is the return signature
+                               r'(\w+)\s*\(' # the function name
+                               r'[^)]*\)\s*' # parameters
+                               r'(.*)\s*:' # the except clause
+                               r'\s*(#\s*no except)?' # no except comment
+                              )
+        for fname, text in self.get_source_file_contents(
+                extensions=('.pyx',)):
+            cdefs = except_re.findall(text)
+            for sig, func, exc_clause, no_exc_comment in cdefs:
+                if not sig:
+                    sig = 'object'
+                if exc_clause and no_exc_comment:
+                    both_exc_and_no_exc.append((fname, func))
+                if sig != 'object' and not (exc_clause or no_exc_comment):
+                    missing_except.append((fname, func))
+        error_msg = []
+        if both_exc_and_no_exc:
+            error_msg.append('The following functions had no except comments'
+                             ' but did have an except clause set:')
+            for fname, func in both_exc_and_no_exc:
+                error_msg.append('%s:%s' % (fname, func))
+            error_msg.extend(('', ''))
+        if missing_except:
+            error_msg.append('The following functions have fixed return types,'
+                             ' but no except clause. Either add an except'
+                             ' or append "# no except".')
+            for fname, func in missing_except:
+                error_msg.append('%s:%s' % (fname, func))
+            error_msg.extend(('', ''))
+        if error_msg:
+            self.fail('\n'.join(error_msg))



More information about the bazaar-commits mailing list