Different interpretation of bytes and string objects compared to CPython.
I'm trying to use jenkinsapi in my project but I'm running into an incompatibility between CPython and IronPython when it comes to handling strings. Specifically, the chardet library used by request module to detect encodings fails to analyse passed content and throws an exception:
chardet.detect(content)['encoding']
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "...\requests\packages\chardet\__init__.py", line 25, in detect
raise ValueError('Expected a bytes object, not a unicode object')
ValueError: Expected a bytes object, not a unicode object
No help to you unfortunately but I have seen it too.
I was pulling in a string containing jpeg data and I couldn't simply write it to file, it kept getting corrupted while it worked just fine in CPython.
My solution was to get the source to output an array of bytes instead and use a C# library for image files.
you very likely have bytes which are returned as string (unicode) from some of the standard library or built-in calls. This means that each unicode is in range of 0-255 and can be downgraded to bytes.
chardet.detect(content)['encoding']
try:
chardet.detect(bytes(content))['encoding']
Hello Pawel,
The string I'm passing to the chardet library is withing 0-255 range. I tried passing bytes(content) (I also tried passing bytes('a') too) instead and I still am getting the same error. It appears it's due to this little snippet in 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')
The mentioned snipped works normally under CPython. No matter what type I passed to chardet's init it always failed on the isinstance check under IronPython. It looks like an incompatibility between Cpython and IronPython's handling of string arguments in memory. Scripts depending on isinstance(x, unicode) check fail to work as intended under Iron.
When I try in interactive:
IronPython:
>>> type('hello')
<type 'str'>
>>> type(u'hello')
<type 'str'>
>>> isinstance('hello',unicode)
True
>>> isinstance(u'hello',unicode)
True
Cpython:
>>> type('hello')
<type 'str'>
>>> type(u'hello')
<type 'unicode'>
>>> isinstance('hello', unicode)
False
>>> isinstance(u'hello', unicode)
True
Also, there seems to be a weird thing happening in my program. I edited my it to support passing of bytes(content) and this is the result I'm getting under IronPython and CPython respectively:
IronPython:
>>> type(content)
<type 'str'>
>>> type(bytes(content))
<type 'str'>
>>> isinstance(content, unicode)
True
>>> isinstance(bytes(content)), unicode)
True
Cpython:
>>> type(content)
<type 'str'>
>>> type(bytes(content))
<type 'str'>
>>> isinstance(content, unicode)
True
>>> isinstance(bytes(content)), unicode)
False
I suspect in your case one of the imports redefine bytes.
The following is done with IronPython 2.7.5 console:
>>> content='foo'
>>> type(content)
<type 'str'>
>>> type(bytes(content))
<type 'bytes'>
>>> isinstance(content,unicode)
True
>>> isinstance(bytes(content),unicode)
False
>>> bytes
<type 'bytes'>
>>> bytes()
b''
>>> bytes=lambda s: s
>>> bytes
<function <lambda$86> at 0x000000000000002B>
>>> isinstance(bytes(content),unicode)
True
>>> type(bytes(content))
<type 'str'>
Yes, there was an import in the request module redefining bytes to str. Thank you :).
But that's not the problem I was trying to highlight here. This issue actually boils down to the following:
CPython 2.7.9:
>>> isinstance('abc', unicode)
False
IronPython 2.7.5:
>>> isinstance('abc', unicode)
True
which seems like very basic incompatibility between CPython (2.7 series) and IronPython 2.7.5.
Is there any possiblity this can be fixed in IronPython?
This is by design. IronPython 2.x and jython have the same string philosophy which does not match cpython. In IronPython strings are unicode, in cpython strings are bytes.
Most of the time it is possible to trick the library into recognizing IronPython and applying the 3.x type scheme. However, the functions out of standard library return values conforming to 2.7 spec, so switching to 3.x mode is not perfect solution and often must be accompanied with selective patching.
This is not something we can fix in IP2. In IP3 all strings are unicode, so there shouldn't be a problem.
Just a side note: while IronPython string handling is somehow closer to Python 3 than Python 2, it still will not be identical with IronPython 3. The reason is that .NET strings are UTF-16, while cPython 3 uses UTF-32, so addressing single code points by their index within the string will only work with BMP characters.
IMHO, in both cases, fully emulating cPython string semantics is just to costly and will break the tight .NET Integration, so I think we should just leave it as is.
@markusschaber shall we track this under PEP 393 in ironpython3? Or perhaps wiki to document it and collect relevant .net consequences. I believe .net regexp is also difficult to use, once we are out of BMP.