libftdi Archives

Subject: RE: ftdi.write_data doesn’t work with some python3 unicode strings

From: Michel Zou <xantares09@xxxxxxxxxxx>
To: Thomas Jarosch <thomas.jarosch@xxxxxxxxxxxxx>, "libftdi@xxxxxxxxxxxxxxxxxxxxxxx" <libftdi@xxxxxxxxxxxxxxxxxxxxxxx>
Cc: Chris Brackert <cbrackert@xxxx>
Date: Tue, 20 Oct 2015 09:14:56 +0000



From: xantares09@xxxxxxxxxxx
To: thomas.jarosch@xxxxxxxxxxxxx; libftdi@xxxxxxxxxxxxxxxxxxxxxxx
CC: cbrackert@xxxx
Subject: RE: ftdi.write_data doesn’t work with some python3 unicode strings
Date: Sat, 17 Oct 2015 10:16:23 +0000



> From: thomas.jarosch@xxxxxxxxxxxxx
> To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
> CC: cbrackert@xxxx; xantares09@xxxxxxxxxxx
> Subject: Re: ftdi.write_data doesn’t work with some python3 unicode strings
> Date: Fri, 16 Oct 2015 13:40:41 +0200
>
> Hi Chris,
>
> On Monday, 5. October 2015 12:18:50 Chris Brackert wrote:
> > Regardless of using python2 or python3, libftdi’s write_data takes a
> > “string”. In python2 this means bytes and in python3 this means unicode.
> >
> >
> > My problem is that certain strings are not being written properly with
> > python3. For example, when ‘\xAB’ is encoded to bytes with UTF-8 (as it
> > does in libftdi), it turns into b’\xC2\xAB’.
> >
> >
> > I have a temporary fix that is working for me and it involves modifying
> > the swig built file `build/python/ftdi1PYTHON_wrap.c`. In the function
> > SWIG_AsCharPtrAndSize I replace PyUnicode_AsUTF8String with
> > PyUnicode_AsLatin1String in line 3161. Now python3 unicode strings are
> > converted using Latin-1 so my example ‘\xAB’ converts to the byte b’\AB’
> > instead of the bytes b’\xC2\xAB’.
> >
> >
> > I know this fix is not the proper way to do this but it is working for me.
> > Can you please investigate this issue and fix the library to work with
> > all python3 strings?
> >
> >
> > Another option I would be happy with is for ftdi.write_data to accept
> > python3 bytes instead of unicode. As of now, if you try and actually pass
> > the bytes b’\xAB’ to write_data, libftdi complains about the type not
> > being correct.
>
> this is probably something in the swig layer that generates
> the wrapper code. I don't know swig myself, but I guess we need
> to configure it to do better type mapping or so.
>
> @xantares09: Any idea about this?
>
> Cheers,
> Thomas

Hi,

I added a bytes conversion fonction for write_data.
Could someone test the following patch ?

xan.



libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx


Attachment: libftdi-wirte_data.patch
Description: Binary data

Current Thread