From 059d257b94b6e1dfb9ee5640360d2213ff63d198 Mon Sep 17 00:00:00 2001 From: "sonu.kumar" Date: Mon, 13 Jun 2016 12:00:59 +0530 Subject: [PATCH] Add hacking checks for xrange() Partially-Implements: blueprint goal-python35 Change-Id: Iea2e9e5d5782b898ba5b18314745962cc059a213 --- HACKING.rst | 1 + nova/hacking/checks.py | 7 +++++++ nova/tests/unit/test_hacking.py | 7 +++++++ 3 files changed, 15 insertions(+) diff --git a/HACKING.rst b/HACKING.rst index 8c09f530a014..4ef3f28752d4 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -37,6 +37,7 @@ Nova Specific Commandments - [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() - [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. - [N329] Validate that LOG.exception messages use _LE. - [N330] Validate that LOG.warning and LOG.warn messages use _LW. diff --git a/nova/hacking/checks.py b/nova/hacking/checks.py index b60e87b5da4d..8ea17908403e 100644 --- a/nova/hacking/checks.py +++ b/nova/hacking/checks.py @@ -290,6 +290,12 @@ def assert_equal_none(logical_line): "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): """Check for 'LOG.debug(_(' @@ -793,6 +799,7 @@ def factory(register): register(check_python3_no_iteritems) register(check_python3_no_iterkeys) register(check_python3_no_itervalues) + register(check_python3_xrange) register(no_os_popen) register(no_log_warn) register(CheckForUncalledTestClosure) diff --git a/nova/tests/unit/test_hacking.py b/nova/tests/unit/test_hacking.py index 774a6d06443a..f3b8ab985537 100644 --- a/nova/tests/unit/test_hacking.py +++ b/nova/tests/unit/test_hacking.py @@ -749,3 +749,10 @@ class HackingTestCase(test.NoDBTestCase): foo.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)'))))