Python 3.x 为什么将字节字符串传递给magic.from_buffer()时会出现类型错误?
我正在尝试使用python magic的Python 3.x 为什么将字节字符串传递给magic.from_buffer()时会出现类型错误?,python-3.x,python-requests,python-magic,Python 3.x,Python Requests,Python Magic,我正在尝试使用python magic的from_buffer从url获取图像的文件类型,它在实践中运行得很好,但在尝试运行unittest时失败 这是我的职责: def get_uri_from_url(url): if url != '': response = requests.get(url) data_type = magic.from_buffer(response.content, mime=True) image_bytes
from_buffer
从url获取图像的文件类型,它在实践中运行得很好,但在尝试运行unittest时失败
这是我的职责:
def get_uri_from_url(url):
if url != '':
response = requests.get(url)
data_type = magic.from_buffer(response.content, mime=True)
image_bytes = str(base64.b64encode(response.content).decode('utf-8'))
return f'data:{data_type};base64,{image_bytes}'
return url
这是目前为止的测试:
def test_get_uri_from_url(self):
test_bytes = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x05\x00\x00\x00\x05\x08\x06\x00\x00\x00\x8do&' \
b'\xe5\x00\x00\x00\x1cIDAT\x08\xd7c\xf8\xff\xff?\xc3\x7f\x06 \x05\xc3 \x12\x84\xd01\xf1\x82X\xcd' \
b'\x04\x00\x0e\xf55\xcb\xd1\x8e\x0e\x1f\x00\x00\x00\x00IEND\xaeB`\x82'
class Requests:
content = test_bytes
def get(self, url):
return self
with patch('path_to_my_function.requests', return_value=Requests):
self.assertEqual(
get_uri_from_url('any_url'),
f'data:image/png;base64,{test_bytes}'
)
此测试需要很长时间才能运行,最后返回以下错误:
Traceback (most recent call last):
File "/<path>/test_file.py", line 90, in test_get_uri_from_url
get_uri_from_url('any_url'),
File "/<path>/file.py", line 165, in get_uri_from_url
data_type = magic.from_buffer(response.content, mime=True)
File "/<path_to_python>/python3.6/site-packages/magic.py", line 148, in from_buffer
return m.from_buffer(buffer)
File "/<path_to_python>/python3.6/site-packages/magic.py", line 80, in from_buffer
return maybe_decode(magic_buffer(self.cookie, buf))
File "/<path_to_python>/python3.6/site-packages/magic.py", line 255, in magic_buffer
return _magic_buffer(cookie, buf, len(buf))
ctypes.ArgumentError: argument 2: <class 'TypeError'>: wrong type
我已通过以下方法使其正常工作:
def test_get_uri_from_url(self):
class Response:
content = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x05\x00\x00\x00\x05\x08\x06\x00\x00\x00\x8do&' \
b'\xe5\x00\x00\x00\x1cIDAT\x08\xd7c\xf8\xff\xff?\xc3\x7f\x06 \x05\xc3 \x12\x84\xd01\xf1\x82X' \
b'\xcd\x04\x00\x0e\xf55\xcb\xd1\x8e\x0e\x1f\x00\x00\x00\x00IEND\xaeB`\x82'
with patch('touchsurgery.apps.apiv3.helpers.requests.get', return_value=Response):
self.assertEqual(
get_uri_from_url('any_url'),
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHx'
'gljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='
)
我相信问题在于我的嘲笑和修补。由于我修补了“requests”的返回值,但从未显式调用请求本身(),因此传递给magic.from_buffer的对象只是一个magicmock对象,因此它失败了
此函数现在可以工作,因为get()在函数中被显式调用。如果其他人能对此有所了解或给出更详细的解释,我们将不胜感激。我已通过以下方法使其发挥作用:
def test_get_uri_from_url(self):
class Response:
content = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x05\x00\x00\x00\x05\x08\x06\x00\x00\x00\x8do&' \
b'\xe5\x00\x00\x00\x1cIDAT\x08\xd7c\xf8\xff\xff?\xc3\x7f\x06 \x05\xc3 \x12\x84\xd01\xf1\x82X' \
b'\xcd\x04\x00\x0e\xf55\xcb\xd1\x8e\x0e\x1f\x00\x00\x00\x00IEND\xaeB`\x82'
with patch('touchsurgery.apps.apiv3.helpers.requests.get', return_value=Response):
self.assertEqual(
get_uri_from_url('any_url'),
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHx'
'gljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='
)
我相信问题在于我的嘲笑和修补。由于我修补了“requests”的返回值,但从未显式调用请求本身(),因此传递给magic.from_buffer的对象只是一个magicmock对象,因此它失败了
此函数现在可以工作,因为get()在函数中被显式调用。如果其他任何人能够对此提供一些说明或给出更详细的解释,我们将不胜感激。您是否可以包含完整的回溯,而不仅仅是最后一节?更新为完整的回溯?您是否可以包含完整的回溯,而不仅仅是最后一节?更新为完整的回溯