From 005526111dc2cfb90c53ea1577b2d847e19b1a40 Mon Sep 17 00:00:00 2001 From: Bo Wang Date: Fri, 22 Jan 2016 19:28:22 +0800 Subject: [PATCH] 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 --- HACKING.rst | 1 + glance/hacking/checks.py | 7 +++++++ glance/tests/test_hacking.py | 8 ++++++++ 3 files changed, 16 insertions(+) diff --git a/HACKING.rst b/HACKING.rst index 76a5f69002..62ff5eb9aa 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -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. diff --git a/glance/hacking/checks.py b/glance/hacking/checks.py index 66a88666ba..95a4f8c176 100644 --- a/glance/hacking/checks.py +++ b/glance/hacking/checks.py @@ -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) diff --git a/glance/tests/test_hacking.py b/glance/tests/test_hacking.py index dd921958e1..258bf54183 100644 --- a/glance/tests/test_hacking.py +++ b/glance/tests/test_hacking.py @@ -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)'))))