Python 3: Replace reduce and xrange with six.moves

1. Builtin function 'reduce' in Python 2 has been moved to standard
library module in Python 3 [1]. To make code compatible, replaced
reduce(expr) with six.moves.reduce(expr).
2. xrange is renamed to range in Python 3, replaced it with
six.moves.range
3. Added __bool__() method in FeatureState class to make it python 3
compatible because Python 3 calls the __bool__() method instead of
__nonzero__ when evaluating an instance in a boolean context.
4. Added this test case to tests-py3.txt.

[1] http://python3porting.com/stdlib.html#moved-builtins

Closes-Bug: 1530249
Change-Id: I376cd643b9f58358a3e147532dafe77a7325a114
This commit is contained in:
Ankit Agrawal 2016-01-15 01:56:56 -08:00
parent 8aa62ef2b9
commit 9cc2b79301
3 changed files with 12 additions and 2 deletions

View File

@ -24,6 +24,8 @@ import time
import mock
from oslo_utils import units
import six
from six.moves import range
from six.moves import reduce
from cinder import exception
from cinder import test
@ -152,7 +154,7 @@ class NetAppEseriesLibraryTestCase(test.TestCase):
new_attr=mock.Mock(return_value=system))
self.mock_object(self.library._client, 'update_stored_system_password')
self.mock_object(time, 'time', new_attr = mock.Mock(
side_effect=xrange(0, 60, 5)))
side_effect=range(0, 60, 5)))
self.assertRaisesRegexp(exception.NetAppDriverException,
'bad.*?status',
@ -1327,7 +1329,7 @@ class NetAppEseriesLibraryMultiAttachTestCase(test.TestCase):
"""Test volume extend with a thick-provisioned volume"""
def get_copy_progress():
for eta in xrange(5, -1, -1):
for eta in range(5, -1, -1):
action_status = 'none' if eta == 0 else 'remappingDve'
complete = action_status == 'none'
yield complete, action_status, eta

View File

@ -490,3 +490,10 @@ class FeatureState(object):
:returns: True if the feature is supported, otherwise False
"""
return self.supported
def __bool__(self):
"""py3 Allow a FeatureState object to be tested for truth value
:returns: True if the feature is supported, otherwise False
"""
return self.supported

View File

@ -151,6 +151,7 @@ cinder.tests.unit.test_vzstorage
cinder.tests.unit.test_xio
cinder.tests.unit.test_zfssa
cinder.tests.unit.volume.drivers.emc.scaleio
cinder.tests.unit.volume.drivers.netapp.eseries.test_library
cinder.tests.unit.volume.drivers.test_fujitsu
cinder.tests.unit.volume.flows.test_create_volume_flow
cinder.tests.unit.windows.test_smbfs