41
43
# Files which are listed here will be skipped when testing for Copyright (or
43
COPYRIGHT_EXCEPTIONS = [
44
'bzrlib/_bencode_py.py',
45
'bzrlib/doc_generate/conf.py',
45
COPYRIGHT_EXCEPTIONS = ['bzrlib/lsprof.py', 'bzrlib/_bencode_py.py']
49
LICENSE_EXCEPTIONS = [
50
'bzrlib/_bencode_py.py',
51
'bzrlib/doc_generate/conf.py',
47
LICENSE_EXCEPTIONS = ['bzrlib/lsprof.py', 'bzrlib/_bencode_py.py']
54
48
# Technically, 'bzrlib/lsprof.py' should be 'bzrlib/util/lsprof.py',
55
49
# (we do not check bzrlib/util/, since that is code bundled from elsewhere)
56
50
# but for compatibility with previous releases, we don't want to move it.
58
# sphinx_conf is semi-autogenerated.
61
53
class TestSourceHelper(TestCase):
368
assert_re = re.compile(r'\bassert\b')
369
360
for fname, text in self.get_source_file_contents():
370
361
if not self.is_our_code(fname):
372
if not assert_re.search(text):
374
ast = parser.ast2tuple(parser.suite(text))
363
ast = parser.ast2tuple(parser.suite(''.join(text)))
376
365
badfiles.append(fname)
379
368
"these files contain an assert statement and should not:\n%s"
380
369
% '\n'.join(badfiles))
382
def test_extension_exceptions(self):
383
"""Extension functions should propagate exceptions.
385
Either they should return an object, have an 'except' clause, or have a
386
"# cannot_raise" to indicate that we've audited them and defined them as not
389
both_exc_and_no_exc = []
391
class_re = re.compile(r'^(cdef\s+)?(public\s+)?'
392
r'(api\s+)?class (\w+).*:', re.MULTILINE)
393
extern_class_re = re.compile(r'## extern cdef class (\w+)',
395
except_re = re.compile(r'cdef\s+' # start with cdef
396
r'([\w *]*?)\s*' # this is the return signature
397
r'(\w+)\s*\(' # the function name
398
r'[^)]*\)\s*' # parameters
399
r'(.*)\s*:' # the except clause
400
r'\s*(#\s*cannot[- _]raise)?' # cannot raise comment
402
for fname, text in self.get_source_file_contents(
403
extensions=('.pyx',)):
404
known_classes = set([m[-1] for m in class_re.findall(text)])
405
known_classes.update(extern_class_re.findall(text))
406
cdefs = except_re.findall(text)
407
for sig, func, exc_clause, no_exc_comment in cdefs:
408
if sig.startswith('api '):
410
if not sig or sig in known_classes:
412
if 'nogil' in exc_clause:
413
exc_clause = exc_clause.replace('nogil', '').strip()
414
if exc_clause and no_exc_comment:
415
both_exc_and_no_exc.append((fname, func))
416
if sig != 'object' and not (exc_clause or no_exc_comment):
417
missing_except.append((fname, func))
419
if both_exc_and_no_exc:
420
error_msg.append('The following functions had "cannot raise" comments'
421
' but did have an except clause set:')
422
for fname, func in both_exc_and_no_exc:
423
error_msg.append('%s:%s' % (fname, func))
424
error_msg.extend(('', ''))
426
error_msg.append('The following functions have fixed return types,'
427
' but no except clause.')
428
error_msg.append('Either add an except or append "# cannot_raise".')
429
for fname, func in missing_except:
430
error_msg.append('%s:%s' % (fname, func))
431
error_msg.extend(('', ''))
433
self.fail('\n'.join(error_msg))