Fix object has no attribute 'readinto' in Python3

File Object in Python3 do not have readinfo function.

Change-Id: Ifcda45d7641b895a58472a533655ca4d3f33f246
This commit is contained in:
xuel 2020-03-11 11:35:36 +08:00
parent 7f8a8dbe2e
commit e9583c6963
2 changed files with 8 additions and 9 deletions

View File

@ -1,4 +1,4 @@
# Copyright (c) 2014 VMware, Inc.
# Copyright (c) 2014-2020 VMware, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
@ -18,7 +18,6 @@ Common classes that provide access to vSphere services.
"""
import logging
import os
import netaddr
from oslo_utils import timeutils
@ -144,9 +143,9 @@ class LocalFileAdapter(requests.adapters.HTTPAdapter):
def _build_response_from_file(self, request):
file_path = request.url[7:]
with open(file_path, 'r') as f:
buff = bytearray(os.path.getsize(file_path))
f.readinto(buff)
with open(file_path, 'rb') as f:
file_content = f.read()
buff = bytearray(file_content.decode(), "utf-8")
resp = Response(buff)
return self.build_response(request, resp)

View File

@ -1,4 +1,4 @@
# Copyright (c) 2014 VMware, Inc.
# Copyright (c) 2014-2020 VMware, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
@ -500,8 +500,8 @@ class RequestsTransportTest(base.TestCase):
data = b"Hello World"
get_size_mock.return_value = len(data)
def readinto_mock(buf):
buf[0:] = data
def read_mock():
return data
if six.PY3:
builtin_open = 'builtins.open'
@ -515,7 +515,7 @@ class RequestsTransportTest(base.TestCase):
file_handle = mock.MagicMock(spec=file_spec)
file_handle.write.return_value = None
file_handle.__enter__.return_value = file_handle
file_handle.readinto.side_effect = readinto_mock
file_handle.read.side_effect = read_mock
open_mock.return_value = file_handle
with mock.patch(builtin_open, open_mock, create=True):