From abc3a514be2fa62824b54ea872e6a4176661c81c Mon Sep 17 00:00:00 2001 From: Michel Zou Date: Thu, 23 Aug 2012 11:14:59 +0200 Subject: [PATCH] python bindings: python3 support Whitespace fixes by Thomas Jarosch (detected by pylint and git) --- bindings/CMakeLists.txt | 2 +- bindings/ftdi1.i | 15 ++++++++++++- examples/python/complete.py | 46 ++++++++++++++++++++---------------------- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/bindings/CMakeLists.txt b/bindings/CMakeLists.txt index 7abf655..606e44b 100644 --- a/bindings/CMakeLists.txt +++ b/bindings/CMakeLists.txt @@ -19,7 +19,7 @@ if(PYTHON_BINDINGS AND SWIG_FOUND AND PYTHONLIBS_FOUND AND PYTHONINTERP_FOUND) swig_link_libraries ( ftdi1 ${PYTHON_LIBRARIES} ) endif () - execute_process ( COMMAND ${PYTHON_EXECUTABLE} -c "import sys; print '%d.%d' % ( sys.version_info[0], sys.version_info[1] )" + execute_process ( COMMAND ${PYTHON_EXECUTABLE} -c "import sys; print( '%d.%d' % ( sys.version_info[0], sys.version_info[1] ) )" OUTPUT_VARIABLE PYTHON_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE ) diff --git a/bindings/ftdi1.i b/bindings/ftdi1.i index f26a334..952a383 100644 --- a/bindings/ftdi1.i +++ b/bindings/ftdi1.i @@ -2,6 +2,17 @@ %module(docstring="Python interface to libftdi1") ftdi1 +%{ +#include "Python.h" + +inline PyObject* convertString( const char *v, Py_ssize_t len ) +#if PY_MAJOR_VERSION >= 3 +{ return PyUnicode_FromStringAndSize(v, len); } +#else +{ return PyString_FromStringAndSize(v, len); } +#endif +%} + %include %include @@ -36,7 +47,7 @@ %clear int mnf_len, int desc_len, int serial_len; %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, PyString_FromStringAndSize((char*)$1, $2)); free($1); %} +%typemap(argout) (unsigned char *buf, int size) %{ if(result<0) $2=0; $result = SWIG_Python_AppendOutput($result, convertString((char*)$1, $2)); free($1); %} int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size); %clear (unsigned char *buf, int size); @@ -62,7 +73,7 @@ %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, PyString_FromStringAndSize((char*)$1, $2)); free($1); %} +%typemap(argout) (unsigned char *buf, int size) %{ if(result<0) $2=0; $result = SWIG_Python_AppendOutput($result, convertString((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); diff --git a/examples/python/complete.py b/examples/python/complete.py index f1b737c..3a765c8 100644 --- a/examples/python/complete.py +++ b/examples/python/complete.py @@ -15,87 +15,85 @@ import time # initialize ftdic = ftdi.new() if ftdic == 0: - print 'new failed: %d', ret - os._exit( 1 ) - - + print( 'new failed: %d', ret ) + os._exit( 1 ) + # list all devices ret, devlist = ftdi.usb_find_all( ftdic, 0x0403, 0x6001 ) if ret < 0: - print 'ftdi_usb_find_all failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) + print( 'ftdi_usb_find_all failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) ) os._exit( 1 ) -print 'Number of FTDI devices found: %d\n' % ret +print( 'Number of FTDI devices found: %d\n' % ret ) curnode = devlist i = 0 while( curnode != None ): ret, manufacturer, description, serial = ftdi.usb_get_strings( ftdic, curnode.dev ) if ret < 0: - print 'ftdi_usb_get_strings failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) + print( 'ftdi_usb_get_strings failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) ) os._exit( 1 ) - print 'Device #%d: manufacturer="%s" description="%s" serial="%s"\n' % ( i, manufacturer, description, serial ) + print( 'Device #%d: manufacturer="%s" description="%s" serial="%s"\n' % ( i, manufacturer, description, serial ) ) curnode = curnode.next i += 1 # open usb ret = ftdi.usb_open( ftdic, 0x0403, 0x6001 ) if ret < 0: - print 'unable to open ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) + print( 'unable to open ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) ) os._exit( 1 ) # bitbang ret = ftdi.set_bitmode( ftdic, 0xff, ftdi.BITMODE_BITBANG ) if ret < 0: - print 'Cannot enable bitbang' + print( 'Cannot enable bitbang' ) os._exit( 1 ) -print 'turning everything on' +print( 'turning everything on' ) ftdi.write_data( ftdic, chr(0xff), 1 ) time.sleep( 1 ) -print 'turning everything off\n' +print( 'turning everything off\n' ) ftdi.write_data( ftdic, chr(0x00), 1 ) time.sleep( 1 ) for i in range( 8 ): val = 2**i - print 'enabling bit #%d (0x%02x)' % (i, val) + print( 'enabling bit #%d (0x%02x)' % (i, val) ) ftdi.write_data( ftdic, chr(val), 1 ) time.sleep ( 1 ) ftdi.disable_bitbang( ftdic ) -print '' +print( '\n' ) # read pins ret, pins = ftdi.read_pins( ftdic ) -print 'pins:', +print( 'pins:\n' ) if ( ret == 0 ): - print '%02x' % ord( pins[0] ) -print '' + print( '%02x' % ord( pins[0] ) ) +print( '\n' ) # read chip id ret, chipid = ftdi.read_chipid( ftdic ) -print 'FDTI chip id: %X\n' % chipid +print( 'FDTI chip id: %X\n' % chipid ) # read eeprom eeprom_addr = 1 ret, eeprom_val = ftdi.read_eeprom_location( ftdic, eeprom_addr ) if (ret==0): - print 'eeprom @ %d: 0x%04x\n' % ( eeprom_addr, eeprom_val ) + print( 'eeprom @ %d: 0x%04x\n' % ( eeprom_addr, eeprom_val ) ) -print 'complete eeprom:' +print( 'complete eeprom:' ) ret = ftdi.read_eeprom( ftdic ) size = 128 ret, eeprom = ftdi.get_eeprom_buf ( ftdic, size ) if ( ret == 0 ): for i in range( size ): - print '%02x' % ord( eeprom[i] ), + print( '%02x' % ord( eeprom[i] ) ) if ( i % 8 == 7 ): - print '' + print( '\n' ) - # close usb ret = ftdi.usb_close( ftdic ) if ret < 0: - print 'unable to close ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) + print( 'unable to close ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) ) os._exit( 1 ) ftdi.free( ftdic ) -- 1.7.1