Add hacking checks for xrange()

Partially-Implements: blueprint goal-python35

Change-Id: Iea2e9e5d5782b898ba5b18314745962cc059a213
This commit is contained in:
sonu.kumar 2016-06-13 12:00:59 +05:30 committed by sonu
parent 48a4fee10f
commit 059d257b94
3 changed files with 15 additions and 0 deletions

View File

@ -37,6 +37,7 @@ Nova Specific Commandments
- [N324] Ensure that jsonutils.%(fun)s must be used instead of json.%(fun)s - [N324] Ensure that jsonutils.%(fun)s must be used instead of json.%(fun)s
- [N325] str() and unicode() cannot be used on an exception. Remove use or use six.text_type() - [N325] str() and unicode() cannot be used on an exception. Remove use or use six.text_type()
- [N326] Translated messages cannot be concatenated. String should be included in translated message. - [N326] Translated messages cannot be concatenated. String should be included in translated message.
- [N327] Do not use xrange(). xrange() is not compatible with Python 3. Use range() or six.moves.range() instead.
- [N328] Validate that LOG.info messages use _LI. - [N328] Validate that LOG.info messages use _LI.
- [N329] Validate that LOG.exception messages use _LE. - [N329] Validate that LOG.exception messages use _LE.
- [N330] Validate that LOG.warning and LOG.warn messages use _LW. - [N330] Validate that LOG.warning and LOG.warn messages use _LW.

View File

@ -290,6 +290,12 @@ def assert_equal_none(logical_line):
"sentences not allowed") "sentences not allowed")
def check_python3_xrange(logical_line):
if re.search(r"\bxrange\s*\(", logical_line):
yield(0, "N327: Do not use xrange(). 'xrange()' is not compatible "
"with Python 3. Use range() or six.moves.range() instead.")
def no_translate_debug_logs(logical_line, filename): def no_translate_debug_logs(logical_line, filename):
"""Check for 'LOG.debug(_(' """Check for 'LOG.debug(_('
@ -793,6 +799,7 @@ def factory(register):
register(check_python3_no_iteritems) register(check_python3_no_iteritems)
register(check_python3_no_iterkeys) register(check_python3_no_iterkeys)
register(check_python3_no_itervalues) register(check_python3_no_itervalues)
register(check_python3_xrange)
register(no_os_popen) register(no_os_popen)
register(no_log_warn) register(no_log_warn)
register(CheckForUncalledTestClosure) register(CheckForUncalledTestClosure)

View File

@ -749,3 +749,10 @@ class HackingTestCase(test.NoDBTestCase):
foo.enforce() foo.enforce()
""" """
self._assert_has_no_errors(code, checks.check_policy_enforce) self._assert_has_no_errors(code, checks.check_policy_enforce)
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)'))))