libftdi-git Archives

Subject: A library to talk to FTDI chips branch, master, updated. v1.2-19-gf64b66d

From: libftdi-git@xxxxxxxxxxxxxxxxxxxxxxx
To: libftdi-git@xxxxxxxxxxxxxxxxxxxxxxx
Date: Thu, 5 Nov 2015 17:56:08 +0100 (CET)
The branch, master has been updated
       via  f64b66d604c5d78886d47034fb2db2176fc24a09 (commit)
      from  3986243d4e7f366001276d7246e7b9822f7e39c4 (commit)


- Log -----------------------------------------------------------------
commit f64b66d604c5d78886d47034fb2db2176fc24a09
Author: Thomas Jarosch <thomas.jarosch@xxxxxxxxxxxxx>
Date:   Thu Nov 5 17:53:07 2015 +0100

    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.

-----------------------------------------------------------------------

Summary of changes:
 python/ftdi1.i |   33 ++++++++++++++++++++++++++-------
 1 files changed, 26 insertions(+), 7 deletions(-)

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 <typemaps.i>
@@ -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);
 


hooks/post-receive
-- 
A library to talk to FTDI chips

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

Current Thread
  • A library to talk to FTDI chips branch, master, updated. v1.2-19-gf64b66d, libftdi-git <=