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
This commit is contained in:
Victor Stinner 2015-10-07 18:07:38 +02:00
parent 8c455a9f1e
commit aebf52528b
4 changed files with 7 additions and 7 deletions

View File

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

View File

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

View File

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

View File

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