requests icon indicating copy to clipboard operation
requests copied to clipboard

chardet.detect() call in models.py fails when using IronPython.

Open dxtodorovic opened this issue 11 years ago • 2 comments

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?

dxtodorovic avatar Mar 31 '15 10:03 dxtodorovic

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.

Lukasa avatar Mar 31 '15 10:03 Lukasa

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.

sigmavirus24 avatar Mar 31 '15 12:03 sigmavirus24