From 87602418af939b83661b84330affeaaf316dfc43 Mon Sep 17 00:00:00 2001 From: Oleh Anufriiev Date: Wed, 1 Jul 2015 21:39:50 +0300 Subject: [PATCH] Replace 'unicode()' function with 'six.text_type' 'unicode' is absent in py3 Closes-Bug: #1470596 Change-Id: Iee127133603d98a7cbe1d14fa54c1203a950018e --- rally/exceptions.py | 3 ++- tests/hacking/README.rst | 3 ++- tests/hacking/checks.py | 13 +++++++++++++ tests/unit/test_hacking.py | 12 ++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/rally/exceptions.py b/rally/exceptions.py index 24cbef9d14..08d52ba9e5 100644 --- a/rally/exceptions.py +++ b/rally/exceptions.py @@ -17,6 +17,7 @@ import sys from oslo_config import cfg +import six from rally.common.i18n import _ from rally.common import log as logging @@ -76,7 +77,7 @@ class RallyException(Exception): if self.__class__.__name__.endswith("_Remote"): return self.args[0] else: - return unicode(self) + return six.text_type(self) class ImmutableException(RallyException): diff --git a/tests/hacking/README.rst b/tests/hacking/README.rst index 233d4c919a..49dd2240a0 100644 --- a/tests/hacking/README.rst +++ b/tests/hacking/README.rst @@ -25,4 +25,5 @@ Rally Specific Commandments * [N341] - Ensure that we are importing oslo_xyz packages instead of deprecated oslo.xyz ones * [N350] - Ensure that single quotes are not used * [N351] - Ensure that data structs (i.e Lists and Dicts) are declared literally rather than using constructors -* [N353] - Ensure that string formatting only uses a mapping if multiple mapping keys are used. +* [N352] - Ensure that string formatting only uses a mapping if multiple mapping keys are used. +* [N353] - Ensure that unicode() function is not uset because of absence in py3 diff --git a/tests/hacking/checks.py b/tests/hacking/checks.py index 04f9c72c56..a413233b1a 100644 --- a/tests/hacking/checks.py +++ b/tests/hacking/checks.py @@ -427,6 +427,18 @@ def check_dict_formatting_in_string(logical_line, tokens): current_string = "" +@skip_ignored_lines +def check_using_unicode(logical_line, filename): + """Check crosspython unicode usage + + N353 + """ + + if re.search(r"\bunicode\(", logical_line): + yield (0, "N353 'unicode' function is absent in python3. Please " + "use 'six.text_type' instead.") + + def factory(register): register(check_assert_methods_from_mock) register(check_import_of_logging) @@ -442,3 +454,4 @@ def factory(register): register(check_quotes) register(check_no_constructor_data_struct) register(check_dict_formatting_in_string) + register(check_using_unicode) diff --git a/tests/unit/test_hacking.py b/tests/unit/test_hacking.py index cc4a12e916..21ea3696cd 100644 --- a/tests/unit/test_hacking.py +++ b/tests/unit/test_hacking.py @@ -306,3 +306,15 @@ class HackingTestCase(test.TestCase): self.assertEqual( [], list(checks.check_dict_formatting_in_string(sample, tokens))) + + def test_check_using_unicode(self): + + checkres = checks.check_using_unicode("text = unicode('sometext')", + "fakefile") + self.assertIsNotNone(next(checkres)) + self.assertEqual([], list(checkres)) + + checkres = checks.check_using_unicode( + "text = process(unicode('sometext'))", "fakefile") + self.assertIsNotNone(next(checkres)) + self.assertEqual([], list(checkres))