ClientResponse.get_encoding() throws TypeError if it hasn't been read #4214
Closed
Description
Long story short
ClientResponse.get_encoding() throws TypeError: object of type 'NoneType' has no len() if we don't first call text() or json() to read from it, i.e. _body is None
Related: #4112
Expected behaviour
Does not throw an error, returns some default value
Actual behaviour
Traceback (most recent call last):
File "/home/transfusion/aiohttp/aiohttp_issue_4112.py", line 14, in <module>
loop.run_until_complete(test())
File "/usr/lib/python3.7/asyncio/base_events.py", line 579, in run_until_complete
return future.result()
File "/home/transfusion/aiohttp/aiohttp_issue_4112.py", line 10, in test
print(r.get_encoding())
File "/home/transfusion/aiohttp/aiohttp/client_reqrep.py", line 942, in get_encoding
encoding = chardet.detect(self._body)['encoding']
File "/home/transfusion/python37/lib/python3.7/site-packages/cchardet/__init__.py", line 15, in detect
encoding, confidence = _cchardet.detect_with_confidence(msg)
File "src/cchardet/_cchardet.pyx", line 15, in cchardet._cchardet.detect_with_confidence
TypeError: object of type 'NoneType' has no len()Steps to reproduce
Find any webpage that doesn't include a charset parameter in its Content-Type response header; you may check headers only with curl -v -s http://example.com 1> /dev/null
import aiohttp
import asyncio
async def test():
async with aiohttp.ClientSession() as session:
r = await session.get('http://www.africau.edu', ssl=None)
# print(await r.text())
print(r.get_encoding())
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(test())Your environment
4.0.0a1, Commit f356eeb