char should accept int in the constructor (at least in Python 3)

Issue #99 resolved
Mark Harviston
created an issue

I'm trying to stuff uuid.UUID data into a windows GUID, but I can't stuff a bytes object into a char* because in python 3 iterating over a bytes gives an int not a bytes of length one (like it does in Python 2)

guid ='GUID*')
ffi.cast('char[16]', guid)[0:16] = uu.bytes_le
return guid

Results in:

TypeError: initializer for ctype 'char' must be a bytes of length 1, not int

(built-in support for uuid.UUID <-> GUID* would be nice too actually, be sure to watch endianness)

Comments (4)

  1. Armin Rigo

    I need someone with "Python 3 design powers" here. Should it be fixed by special-casing "bytes" objects during slice assignment, or by allowing ints as 'char' objects everywhere (on Python 3 only)?

  2. Mark Harviston reporter

    My inclination is that char objects should come out as bytes length 1, but accept either bytes length 1 or int objects (that are in range).

    But I'm not sure I qualify as having "Python 3 design powers"

  3. Armin Rigo

    Fixed in 26390b01617e. I just added a fast-path in the slice assignment logic, which makes a lot of sense anyway for performance reasons: iterating over the string using the Python iterator protocol is horribly slow, compared to a simple memcpy(). Feel free to complain if you'd like ints to be accepted for the ctype 'char' elsewhere...

  4. Log in to comment