chardet.detect() call in models.py fails when using IronPython.
In models.py, chardet.detect(self.content)['encoding'] fails to work with IronPython with an exception: 'Expected a bytes object, not a unicode object'. It is due to a check in chardet's init ( isinstance(aBuf, unicode) ) and IronPython's way of handling strings as unicode type.
Excerpt from chardet's init:
def detect(aBuf):
if ((version_info < (3, 0) and isinstance(aBuf, unicode)) or
(version_info >= (3, 0) and not isinstance(aBuf, bytes))):
raise ValueError('Expected a bytes object, not a unicode object')
I tried passing bytes(self.content) to chardet instead but that solution fails short because in compat.py, we are redefining bytes to str, which under IronPython is unicode.
Can we perhaps add some way of handling IronPython properly or is it something that should be fixed in chardet instead?
I think this needs to be fixed in chardet itself, sadly. Happily, it's now under active development, over at chardet/chardet, so opening the issue over there should get it promptly looked at.
Actually this looks like a problem with IronPython that voids requests expectations. Response.content should always be a bytes object. That's what we expect to get back from the socket via httplib. If we aren't getting bytes back, that sounds like a bug in the way IronPython has copied in the standard library. We have no way of encoding it back to bytes intelligently and IronPython should really really really not be doing that.