Add hacking check to ensure not use xrange()

xrange() does not exist in python3.
Add hacking check rule so that codes with xragne() never pass test.

Change-Id: I40f6d9aab3ef334da46bd460eb6a4ed3948e951e
This commit is contained in:
Bo Wang 2016-01-22 19:28:22 +08:00
parent f4d46d1c34
commit 005526111d
3 changed files with 16 additions and 0 deletions

View File

@ -24,3 +24,4 @@ glance Specific Commandments
- [G326] Validate that LOG.warning messages use _LW.
- [G327] Prevent use of deprecated contextlib.nested
- [G328] Must use a dict comprehension instead of a dict constructor with a sequence of key-value pairs
- [G329] Python 3: Do not use xrange.

View File

@ -156,6 +156,12 @@ def dict_constructor_with_list_copy(logical_line):
yield (0, msg)
def check_python3_xrange(logical_line):
if re.search(r"\bxrange\s*\(", logical_line):
yield(0, "G329: Do not use xrange. Use range, or six.moves.range for "
"large loops.")
def factory(register):
register(assert_true_instance)
register(assert_equal_type)
@ -165,3 +171,4 @@ def factory(register):
register(validate_log_translations)
register(check_no_contextlib_nested)
register(dict_constructor_with_list_copy)
register(check_python3_xrange)

View File

@ -100,3 +100,11 @@ class HackingTestCase(utils.BaseTestCase):
self.assertEqual(0, len(list(checks.dict_constructor_with_list_copy(
" self._render_dict(xml, data_el, data.__dict__)"))))
def test_check_python3_xrange(self):
func = checks.check_python3_xrange
self.assertEqual(1, len(list(func('for i in xrange(10)'))))
self.assertEqual(1, len(list(func('for i in xrange (10)'))))
self.assertEqual(0, len(list(func('for i in range(10)'))))
self.assertEqual(0, len(list(func('for i in six.moves.range(10)'))))
self.assertEqual(0, len(list(func('testxrange(10)'))))