IronLanguages / main

Work for this repo has moved to https://github.com/IronLanguages/ironpython2
1.16k stars 347 forks source link

Wrong c_char array field value when creating ctypes.Structure from raw data #825

Open ironpythonbot opened 9 years ago

ironpythonbot commented 9 years ago

Run the following code in both CPython and IronPython:

import ctypes

class BarHeader(ctypes.Structure):

_fields_ = [("id", ctypes.c_char * 4)]

header = BarHeader()
ctypes.memmove(ctypes.addressof(header), "GIGI", 4)
print repr(header.id)

header2 = BarHeader()
header2.id = "MIKI"

print repr(buffer(header2)[:])

CPython output:
'GIGI'
'MIKI'

IronPython output:
'G'
b'MIKI'

Work Item Details

Original CodePlex Issue: Issue 27002 Status: Active Reason Closed: Unassigned Assigned to: Unassigned Reported on: May 8, 2010 at 11:25 PM Reported by: adal Updated on: Feb 22, 2013 at 2:10 AM Updated by: dwelden

ironpythonbot commented 9 years ago

On 2010-05-09 06:55:58 UTC, adal commented:

The problem seems to be that "GIGI" is a Unicode object, not a bytes object.

In my original code, that value is read from a file, which was opened in BINARY mode ("rb"). Why does reading from a "rb" file return a Unicode object instead of a bytes one as specified in the CPython manual?

ironpythonbot commented 9 years ago

On 2010-05-11 06:07:00 UTC, dinov commented:

In IronPython we only have Unicode strings - so that's what you always get back. This will all get fixed up when we move to 3.x. We could try and do that today but we'll likely introduce more incompatibilities for programs that expect strings back. Unfortunately we're in a weird world trying to balance the 2 right now.

ironpythonbot commented 9 years ago

On 2010-05-11 19:25:38 UTC, adal commented:

Is it possible for IronPython to add a special flag to the open function, which will make all further read/write operations on that handle work with bytes instead of strings?

Something like "f = open(path, 'rbx')". Or maybe "f.forceReturnBytes()".

Since migrating to 3.x will take years, this could improve the current situation for IronPython aware apps.

The code I write needs to work in both CPython and IronPython, and working with strings instead of bytes really complicates my life.