Py3 fix in fake image service

In py3, a file opened with 'b' must be written with a bytes object, not
a string (this restriction does not exist in py2):

 >>> f = open('/tmp/foo', 'wb+')
 >>> f.write('foo')
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
 TypeError: a bytes-like object is required, not 'str'

The fake image service's download method was writing '' by default when
no image data was present, which would result in the above exception if
we ever hit that code path. This fix changes it to b''.

Change-Id: Id4644f611fd48936b9ef036c18174dba0013c84a
This commit is contained in:
Matthew Booth 2018-08-15 14:42:00 +01:00 committed by Eric Fried
parent c049061bd4
commit 725f0d1e75

View File

@ -167,10 +167,10 @@ class _FakeImageService(object):
trusted_certs=None):
self.show(context, image_id)
if data:
data.write(self._imagedata.get(image_id, ''))
data.write(self._imagedata.get(image_id, b''))
elif dst_path:
with open(dst_path, 'wb') as data:
data.write(self._imagedata.get(image_id, ''))
data.write(self._imagedata.get(image_id, b''))
def show(self, context, image_id, include_locations=False,
show_deleted=True):