Fix py3k issues

1) Module urllib2 is missed in Python 3, so it should be replaced by other
   http lib. "requests" is good library for such stuff, which supports both
   Python 2.* and Python 3.*

2) Function 'rally.common.utils.parse_docstring' uses built-in function
   'filter'. This function returns different types in Python 2.* and
   Python 3.*. Since results are used as a list, 'filter' should be replaced
   by list comprehension.

3) Extend H330 hacking rule to chech 'dict.iterkeys()', 'dict.itervalues()'
   and 'dict.iterlist()'. Also, fixed all places, which are failed in terms
   of this rule.

4) Function object does not have 'func_code' attribute in Python 3, but
   '__code__' attribute exist in both Python 2 and Python 3

Also, 3 tests are broken due to new release(1.6.0) of oslo.config, so we
need to skip them to unblock our gates until new version of oslo.config will
be released with appropriate fix.

Closes-Bug: #1405919

Change-Id: Icc42e220ac3f15ae6c838a4698b3c9578177513c
This commit is contained in:
Andrey Kurilin 2015-01-05 18:24:08 +02:00
parent f02726b886
commit 4f4c5791b1
2 changed files with 15 additions and 9 deletions

View File

@ -22,7 +22,7 @@ Rally Specific Commandments
* [N323] - Ensure that ``assertTrue/assertFalse(A in/not in B)`` are not used with collection contents
* [N324] - Ensure that ``assertEqual(A in/not in B, True/False)`` and ``assertEqual(True/False, A in/not in B)`` are not used with collection contents
* [N33x] - Reserved for rules related to Python 3 compatibility
* [N330] - Ensure that ``dict.iteritems()`` is not used
* [N330] - Ensure that ``dict.iterkeys()``, ``dict.itervalues()``, ``dict.iteritems()`` and ``dict.iterlist()`` are not used
* [N331] - Ensure that ``basestring`` is not used
* [N332] - Ensure that ``StringIO.StringIO`` is not used
* [N333] - Ensure that ``urlparse`` is not used

View File

@ -45,7 +45,6 @@ re_assert_equal_in_end_with_true_or_false = re.compile(
r"assertEqual\((\w|[][.'\"])+( not)? in (\w|[][.'\", ])+, (True|False)\)")
re_assert_equal_in_start_with_true_or_false = re.compile(
r"assertEqual\((True|False), (\w|[][.'\"])+( not)? in (\w|[][.'\", ])+\)")
re_iteritems_method = re.compile(r"\.iteritems\(\)")
re_basestring_method = re.compile(r"(^|[\s,(\[=])basestring([\s,)\]]|$)")
re_StringIO_method = re.compile(r"StringIO\.StringIO\(")
re_urlparse_method = re.compile(r"(^|[\s=])urlparse\.")
@ -233,17 +232,24 @@ def assert_equal_in(logical_line):
def check_iteritems_method(logical_line):
"""Check if iteritems is properly called for compatibility with Python 3
"""Check that collections are iterated in Python 3 compatible way
The correct form is six.iteritems(dict) or dict.items(), instead of
dict.iteritems()
The correct forms:
six.iterkeys(collection)
six.itervalues(collection)
six.iteritems(collection)
six.iterlist(collection)
N330
"""
res = re_iteritems_method.search(logical_line)
if res:
yield (0, "N330: Use six.iteritems(dict) or dict.items() rather than "
"dict.iteritems() to iterate a collection.")
iter_functions = ["iterkeys()", "itervalues()",
"iteritems()", "iterlist()"]
for func in iter_functions:
pos = logical_line.find(func)
if pos != -1:
yield (pos, "N330: Use six.%(func)s(dict) rather than "
"dict.%(func)s() to iterate a collection." %
{"func": func[:-2]})
def check_basestring_method(logical_line):