From: Victor Stinner Date: Wed, 7 Oct 2015 16:07:38 +0000 (+0200) Subject: Port ceph driver to Python 3 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=aebf52528babffd559642b8b77ee1589a3b5de93;p=openstack-build%2Fcinder-build.git Port ceph driver to Python 3 * Replace xrange() with range(). cinder/volume/utils.py uses "from six.moves import range", so "range" is xrange on Python 2. * Replace '' with b'' for image content to get a bytes string on Python 3. Note: _transfer_data() of cinder.volume.utils is tested by test_backup_ceph which is already run on Python 3, but the test only failed with python3 run with -bb. This bug is now fixed. Partial-Implements: blueprint cinder-python3 Change-Id: Ib69b9ee4669d3b627747c754b1bda1994f0ed2a5 --- diff --git a/cinder/backup/drivers/ceph.py b/cinder/backup/drivers/ceph.py index e197eb3fa..26d6f7c1a 100644 --- a/cinder/backup/drivers/ceph.py +++ b/cinder/backup/drivers/ceph.py @@ -310,7 +310,7 @@ class CephBackupDriver(driver.BackupDriver): data = src.read(self.chunk_size) # If we have reach end of source, discard any extraneous bytes from # destination volume if trim is enabled and stop writing. - if data == '': + if data == b'': if CONF.restore_discard_excess_bytes: self._discard_bytes(dest, dest.tell(), length - dest.tell()) @@ -334,7 +334,7 @@ class CephBackupDriver(driver.BackupDriver): if rem: LOG.debug("Transferring remaining %s bytes", rem) data = src.read(rem) - if data == '': + if data == b'': if CONF.restore_discard_excess_bytes: self._discard_bytes(dest, dest.tell(), rem) else: diff --git a/cinder/tests/unit/test_rbd.py b/cinder/tests/unit/test_rbd.py index a3d2b68e1..bb60f4aa8 100644 --- a/cinder/tests/unit/test_rbd.py +++ b/cinder/tests/unit/test_rbd.py @@ -966,7 +966,7 @@ class RBDImageIOWrapperTestCase(test.TestCase): self.meta.image.size = mock.Mock() self.mock_rbd_wrapper = driver.RBDImageIOWrapper(self.meta) self.data_length = 1024 - self.full_data = 'abcd' * 256 + self.full_data = b'abcd' * 256 def test_init(self): self.assertEqual(self.mock_rbd_wrapper._rbd_meta, self.meta) @@ -1001,7 +1001,7 @@ class RBDImageIOWrapperTestCase(test.TestCase): self.assertEqual(self.full_data, data) data = self.mock_rbd_wrapper.read() - self.assertEqual('', data) + self.assertEqual(b'', data) self.mock_rbd_wrapper.seek(0) data = self.mock_rbd_wrapper.read() diff --git a/cinder/volume/drivers/rbd.py b/cinder/volume/drivers/rbd.py index e5eefe8fc..b3be6e1cb 100644 --- a/cinder/volume/drivers/rbd.py +++ b/cinder/volume/drivers/rbd.py @@ -142,7 +142,7 @@ class RBDImageIOWrapper(io.RawIOBase): # length (they just return nothing) but rbd images do so we need to # return empty string if we have reached the end of the image. if (offset >= total): - return '' + return b'' if length is None: length = total diff --git a/cinder/volume/utils.py b/cinder/volume/utils.py index 621296b43..9e1e2c72f 100644 --- a/cinder/volume/utils.py +++ b/cinder/volume/utils.py @@ -377,13 +377,13 @@ def _transfer_data(src, dest, length, chunk_size): LOG.debug("%(chunks)s chunks of %(bytes)s bytes to be transferred.", {'chunks': chunks, 'bytes': chunk_size}) - for chunk in xrange(0, chunks): + for chunk in range(0, chunks): before = time.time() data = tpool.execute(src.read, min(chunk_size, remaining_length)) # If we have reached end of source, discard any extraneous bytes from # destination volume if trim is enabled and stop writing. - if data == '': + if data == b'': break tpool.execute(dest.write, data)