Merge "Add hacking check to ensure not to use xrange()"

This commit is contained in:
Jenkins 2016-03-22 13:18:56 +00:00 committed by Gerrit Code Review
commit d7fd4c76a0
3 changed files with 13 additions and 1 deletions

View File

@ -22,7 +22,7 @@ Manila Specific Commandments
- [M333] 'oslo_' should be used instead of 'oslo.'
- [M336] Must use a dict comprehension instead of a dict constructor
with a sequence of key-value pairs.
- [M337] Ensure to not use xrange().
LOG Translations
----------------

View File

@ -55,6 +55,7 @@ underscore_import_check_multi = re.compile(r"(.)*import (.)*_, (.)*")
custom_underscore_check = re.compile(r"(.)*_\s*=\s*(.)*")
oslo_namespace_imports = re.compile(r"from[\s]*oslo[.](.*)")
dict_constructor_with_list_copy_re = re.compile(r".*\bdict\((\[)?(\(|\[)")
assert_no_xrange_re = re.compile(r"\s*xrange\s*\(")
class BaseASTChecker(ast.NodeVisitor):
@ -243,6 +244,11 @@ def dict_constructor_with_list_copy(logical_line):
yield (0, msg)
def no_xrange(logical_line):
if assert_no_xrange_re.match(logical_line):
yield(0, "M337: Do not use xrange().")
def factory(register):
register(validate_log_translations)
register(check_explicit_underscore_import)
@ -251,3 +257,4 @@ def factory(register):
register(CheckForTransAdd)
register(check_oslo_namespace_imports)
register(dict_constructor_with_list_copy)
register(no_xrange)

View File

@ -238,3 +238,8 @@ class HackingTestCase(test.TestCase):
self.assertEqual(0, len(list(checks.dict_constructor_with_list_copy(
" self._render_dict(xml, data_el, data.__dict__)"))))
def test_no_xrange(self):
self.assertEqual(1, len(list(checks.no_xrange("xrange(45)"))))
self.assertEqual(0, len(list(checks.no_xrange("range(45)"))))