From bcb0f3133ff3be605688c717f5252e0f4960c9a9 Mon Sep 17 00:00:00 2001 From: Tin Lam Date: Thu, 17 Mar 2016 15:57:06 +0000 Subject: [PATCH] Add hacking check to ensure not to use xrange() Added hacking check to ensure not to use xrange for python3 compatibility. Change-Id: I1aa510660a25936dbf1b2fc5971e7571090a42d0 Closes-Bug: #1538118 --- HACKING.rst | 2 +- manila/hacking/checks.py | 7 +++++++ manila/tests/test_hacking.py | 5 +++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/HACKING.rst b/HACKING.rst index 2440afa481..ddd06360d3 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -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 ---------------- diff --git a/manila/hacking/checks.py b/manila/hacking/checks.py index c71782f5df..cce8fcec6b 100644 --- a/manila/hacking/checks.py +++ b/manila/hacking/checks.py @@ -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) diff --git a/manila/tests/test_hacking.py b/manila/tests/test_hacking.py index 9b5f187615..b504a505a5 100644 --- a/manila/tests/test_hacking.py +++ b/manila/tests/test_hacking.py @@ -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)"))))