Replace 'unicode()' function with 'six.text_type'

'unicode' is absent in py3

Closes-Bug: #1470596

Change-Id: Iee127133603d98a7cbe1d14fa54c1203a950018e
This commit is contained in:
Oleh Anufriiev 2015-07-01 21:39:50 +03:00
parent 4d2dea3a33
commit 87602418af
4 changed files with 29 additions and 2 deletions

View File

@ -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):

View File

@ -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

View File

@ -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)

View File

@ -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))