From 70beb119245cea3f5f290952ea516f3ea933098c Mon Sep 17 00:00:00 2001 From: Louis Taylor Date: Wed, 17 Sep 2014 16:38:27 +0000 Subject: [PATCH] Enforce using six.text_type() over unicode() For python 3 compatibility, this adds a hacking check to prevent the usage of unicode(). Change-Id: I791bbf251ac0f9615a9bedc2dd295a7982d72e0a Closes-bug: #1282893 --- HACKING.rst | 1 + glance/hacking/checks.py | 11 +++++++++++ glance/tests/test_hacking.py | 11 +++++++++++ 3 files changed, 23 insertions(+) diff --git a/HACKING.rst b/HACKING.rst index 73cf0ef8d1..60029e2785 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -15,3 +15,4 @@ glance Specific Commandments - [G318] Change assertEqual(A, None) or assertEqual(None, A) by optimal assert like assertIsNone(A) - [G319] Validate that debug level logs are not translated +- [G320] For python 3 compatibility, use six.text_type() instead of unicode() diff --git a/glance/hacking/checks.py b/glance/hacking/checks.py index c03ac4bcc5..507218b32a 100644 --- a/glance/hacking/checks.py +++ b/glance/hacking/checks.py @@ -40,6 +40,7 @@ asse_equal_end_with_none_re = re.compile( r"(.)*assertEqual\((\w|\.|\'|\"|\[|\])+, None\)") asse_equal_start_with_none_re = re.compile( r"(.)*assertEqual\(None, (\w|\.|\'|\"|\[|\])+\)") +unicode_func_re = re.compile(r"(\s|\W|^)unicode\(") def assert_true_instance(logical_line): @@ -91,8 +92,18 @@ def no_translate_debug_logs(logical_line, filename): yield(0, "G319: Don't translate debug level logs") +def no_direct_use_of_unicode_function(logical_line): + """Check for use of unicode() builtin + + G320 + """ + if unicode_func_re.match(logical_line): + yield(0, "G320: Use six.text_type() instead of unicode()") + + def factory(register): register(assert_true_instance) register(assert_equal_type) register(assert_equal_none) register(no_translate_debug_logs) + register(no_direct_use_of_unicode_function) diff --git a/glance/tests/test_hacking.py b/glance/tests/test_hacking.py index dbad0f6e55..9ad47d8000 100644 --- a/glance/tests/test_hacking.py +++ b/glance/tests/test_hacking.py @@ -51,3 +51,14 @@ class HackingTestCase(utils.BaseTestCase): self.assertEqual(0, len(list(checks.no_translate_debug_logs( "LOG.info(_('foo'))", "glance/store/foo.py")))) + + def test_no_direct_use_of_unicode_function(self): + self.assertEqual(1, len(list(checks.no_direct_use_of_unicode_function( + "unicode('the party dont start til the unicode walks in')")))) + self.assertEqual(1, len(list(checks.no_direct_use_of_unicode_function( + """unicode('something ' + 'something else""")))) + self.assertEqual(0, len(list(checks.no_direct_use_of_unicode_function( + "six.text_type('party over')")))) + self.assertEqual(0, len(list(checks.no_direct_use_of_unicode_function( + "not_actually_unicode('something completely different')"))))