From: Thomas Jarosch Date: Thu, 5 Nov 2015 16:53:07 +0000 (+0100) Subject: swig wrapper: Fix handling of binary strings in ftdi_write_data() for python 3 X-Git-Tag: v1.3rc1~14 X-Git-Url: http://developer.intra2net.com/git/?p=libftdi;a=commitdiff_plain;h=f64b66d604c5d78886d47034fb2db2176fc24a09 swig wrapper: Fix handling of binary strings in ftdi_write_data() for python 3 Thanks to xantares09 for developing this patch! Also thanks to Chris Bracket for testing it on python 2 and python 3. --- diff --git a/python/ftdi1.i b/python/ftdi1.i index 1e820dd..cc71a3c 100644 --- a/python/ftdi1.i +++ b/python/ftdi1.i @@ -10,14 +10,25 @@ %{ #include "Python.h" -PyObject* convertString( const char *v, Py_ssize_t len ) +inline PyObject* charp2str(const char *v_, long len) { #if PY_MAJOR_VERSION >= 3 - return PyBytes_FromStringAndSize(v, len); + return PyBytes_FromStringAndSize(v_, len); #else - return PyString_FromStringAndSize(v, len); + return PyString_FromStringAndSize(v_, len); #endif } + +inline char * str2charp_size(PyObject* pyObj, int * size) +{ + char * v_ = 0; +#if PY_MAJOR_VERSION >= 3 + PyBytes_AsStringAndSize(pyObj, &v_, (Py_ssize_t*)size); +#else + PyString_AsStringAndSize(pyObj, &v_, (Py_ssize_t*)size); +#endif + return v_; +} %} %include @@ -66,10 +77,18 @@ PyObject* convertString( const char *v, Py_ssize_t len ) %enddef %feature("autodoc", ftdi_read_data_docstring) ftdi_read_data; %typemap(in,numinputs=1) (unsigned char *buf, int size) %{ $2 = PyInt_AsLong($input);$1 = (unsigned char*)malloc($2*sizeof(char)); %} -%typemap(argout) (unsigned char *buf, int size) %{ if(result<0) $2=0; $result = SWIG_Python_AppendOutput($result, convertString((char*)$1, $2)); free($1); %} +%typemap(argout) (unsigned char *buf, int size) %{ if(result<0) $2=0; $result = SWIG_Python_AppendOutput($result, charp2str((char*)$1, $2)); free($1); %} int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size); %clear (unsigned char *buf, int size); +%define ftdi_write_data_docstring +"write_data(context, data) -> return_code" +%enddef +%feature("autodoc", ftdi_write_data_docstring) ftdi_write_data; +%typemap(in,numinputs=1) (const unsigned char *buf, int size) %{ $1 = (unsigned char*)str2charp_size($input, &$2); %} + int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size); +%clear (const unsigned char *buf, int size); + %apply int *OUTPUT { unsigned int *chunksize }; int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize); int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize); @@ -80,12 +99,12 @@ PyObject* convertString( const char *v, Py_ssize_t len ) %enddef %feature("autodoc", ftdi_read_pins_docstring) ftdi_read_pins; %typemap(in,numinputs=0) unsigned char *pins ($*ltype temp) %{ $1 = &temp; %} -%typemap(argout) (unsigned char *pins) %{ $result = SWIG_Python_AppendOutput($result, convertString((char*)$1, 1)); %} +%typemap(argout) (unsigned char *pins) %{ $result = SWIG_Python_AppendOutput($result, charp2str((char*)$1, 1)); %} int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins); %clear unsigned char *pins; %typemap(in,numinputs=0) unsigned char *latency ($*ltype temp) %{ $1 = &temp; %} -%typemap(argout) (unsigned char *latency) %{ $result = SWIG_Python_AppendOutput($result, convertString((char*)$1, 1)); %} +%typemap(argout) (unsigned char *latency) %{ $result = SWIG_Python_AppendOutput($result, charp2str((char*)$1, 1)); %} int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency); %clear unsigned char *latency; @@ -98,7 +117,7 @@ PyObject* convertString( const char *v, Py_ssize_t len ) %clear int* value; %typemap(in,numinputs=1) (unsigned char *buf, int size) %{ $2 = PyInt_AsLong($input);$1 = (unsigned char*)malloc($2*sizeof(char)); %} -%typemap(argout) (unsigned char *buf, int size) %{ if(result<0) $2=0; $result = SWIG_Python_AppendOutput($result, convertString((char*)$1, $2)); free($1); %} +%typemap(argout) (unsigned char *buf, int size) %{ if(result<0) $2=0; $result = SWIG_Python_AppendOutput($result, charp2str((char*)$1, $2)); free($1); %} int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size); %clear (unsigned char *buf, int size);