From 33eda68e4c6dd6d725baec99a8a50ef9970c8138 Mon Sep 17 00:00:00 2001 From: Daniel Casner Date: Mon, 6 Jul 2015 16:07:35 -0700 Subject: [PATCH 1/2] Fix python3 compatibility. Still compatible with Python 2.7, untested below that. --- docs/{README.PYTHON => README.PYTHON.html} | 0 src/configure | 4 ++-- src/mpsse.i | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename docs/{README.PYTHON => README.PYTHON.html} (100%) diff --git a/docs/README.PYTHON b/docs/README.PYTHON.html similarity index 100% rename from docs/README.PYTHON rename to docs/README.PYTHON.html diff --git a/src/configure b/src/configure index 7d49e2e..e342a1e 100755 --- a/src/configure +++ b/src/configure @@ -3517,8 +3517,8 @@ done if ! test $PYLIB then - echo "import sys" > pylib.py - echo "for path in sys.path: print path" >> pylib.py + echo "import sys, os" > pylib.py + echo "for path in sys.path: sys.stdout.write(path + os.linesep)" >> pylib.py PYLIB="$(python pylib.py | grep -e '-packages$' | head -1)" rm -f pylib.py fi diff --git a/src/mpsse.i b/src/mpsse.i index 976a583..542096b 100644 --- a/src/mpsse.i +++ b/src/mpsse.i @@ -17,7 +17,7 @@ %typemap(out) swig_string_data { - $result = PyString_FromStringAndSize($1.data, $1.size); + $result = PyBytes_FromStringAndSize($1.data, $1.size); free($1.data); } From bc81f63b9ca9f37c5cfb1bbb6f25d95e78011b9f Mon Sep 17 00:00:00 2001 From: Daniel Casner Date: Mon, 6 Jul 2015 18:13:43 -0700 Subject: [PATCH 2/2] Make mpsse.py compatible with python3 --- src/mpsse.py | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/mpsse.py b/src/mpsse.py index bc67f8b..a98c166 100644 --- a/src/mpsse.py +++ b/src/mpsse.py @@ -2,7 +2,7 @@ MPSSE_OK = _mpsse.MPSSE_OK MPSSE_FAIL = _mpsse.MPSSE_FAIL - + MSB = _mpsse.MSB LSB = _mpsse.LSB @@ -67,7 +67,7 @@ def __init__(self, mode=None, frequency=ONE_HUNDRED_KHZ, endianess=MSB): if mode is not None: self.context = _mpsse.MPSSE(mode, frequency, endianess) if self.context.open == 0: - raise Exception, self.ErrorString() + raise Exception(self.ErrorString()) def __enter__(self): return self @@ -100,7 +100,7 @@ def Open(self, vid, pid, mode, frequency=ONE_HUNDRED_KHZ, endianess=MSB, interfa """ self.context = _mpsse.OpenIndex(vid, pid, mode, frequency, endianess, interface, description, serial, index) if self.context.open == 0: - raise Exception, self.ErrorString() + raise Exception(self.ErrorString()) return MPSSE_OK def Close(self): @@ -111,7 +111,7 @@ def Close(self): """ retval = _mpsse.Close(self.context) self.context = None - + def ErrorString(self): """ Returns the last libftdi error string. @@ -125,12 +125,12 @@ def SetMode(self, mode, endianess): @mode - The MPSSE mode to use, one of: SPI0, SPI1, SPI2, SPI3, I2C, GPIO, BITBANG. @endianess - The endianess of data transfers, one of: MSB, LSB. - + Returns MPSSE_OK on success. Raises an exception on failure. """ if _mpsse.SetMode(self.context, mode, endianess) == MPSSE_FAIL: - raise Exception, self.ErrorString() + raise Exception(self.ErrorString()) return MPSSE_OK def EnableBitmode(self, tf): @@ -165,7 +165,7 @@ def SetClock(self, frequency): Raises an exception on failure. """ if _mpsse.SetClock(self.context, frequency) == MPSSE_FAIL: - raise Exception, self.ErrorString() + raise Exception(self.ErrorString()) return MPSSE_OK def GetClock(self): @@ -188,7 +188,7 @@ def GetPid(self): def GetDescription(self): """ - Returns the description of the FTDI chip, if any. + Returns the description of the FTDI chip, if any. This will only be populated if __init__ was used to open the device. """ return _mpsse.GetDescription(self.context) @@ -203,7 +203,7 @@ def SetLoopback(self, enable): Raises an exception on failure. """ if _mpsse.SetLoopback(self.context, enable) == MPSSE_FAIL: - raise Exception, self.ErrorString() + raise Exception(self.ErrorString()) return MPSSE_OK def SetCSIdle(self, idle): @@ -224,7 +224,7 @@ def Start(self): Raises an exception on failure. """ if _mpsse.Start(self.context) == MPSSE_FAIL: - raise Exception, self.ErrorString() + raise Exception(self.ErrorString()) return MPSSE_OK def Stop(self): @@ -235,20 +235,20 @@ def Stop(self): Raises an exception on failure. """ if _mpsse.Stop(self.context) == MPSSE_FAIL: - raise Exception, self.ErrorString() + raise Exception(self.ErrorString()) return MPSSE_OK def Write(self, data): """ Writes bytes out via the selected serial protocol. - + @data - A string of bytes to be written. Returns MPSSE_OK on success. Raises an exception on failure. """ if _mpsse.Write(self.context, data) == MPSSE_FAIL: - raise Exception, self.ErrorString() + raise Exception(self.ErrorString()) return MPSSE_OK def Read(self, size): @@ -310,7 +310,7 @@ def GetAck(self): def PinHigh(self, pin): """ Sets the specified GPIO pin high. - + @pin - Pin number 0 - 11 in GPIO mode. In all other modes, one of: GPIOL0, GPIOL1, GPIOL2, GPIOL3, GPIOH0, GPIOH1, GPIOH2, GPIOH3, GPIOH4, GPIOH5, GPIOH6, GPIOH7. @@ -318,41 +318,41 @@ def PinHigh(self, pin): Raises an exception on failure. """ if _mpsse.PinHigh(self.context, pin) == MPSSE_FAIL: - raise Exception, self.ErrorString() + raise Exception(self.ErrorString()) return MPSSE_OK def PinLow(self, pin): """ Sets the specified GPIO pin low. - + @pin - Pin number 0 - 11 in GPIO mode. In all other modes, one of: GPIOL0, GPIOL1, GPIOL2, GPIOL3, GPIOH0, GPIOH1, GPIOH2, GPIOH3, GPIOH4, GPIOH5, GPIOH6, GPIOH7. - + Returns MPSSE_OK on success. Raises an exception on failure. """ if _mpsse.PinLow(self.context, pin) == MPSSE_FAIL: - raise Exception, self.ErrorString() + raise Exception(self.ErrorString()) return MPSSE_OK def SetDirection(self, direction): """ - Sets the input/output direction of pins as determined by direction (1 = Output, 0 = Input). + Sets the input/output direction of pins as determined by direction (1 = Output, 0 = Input). For use in BITBANG mode only. - @direction - Byte indicating input/output direction of each bit (1 is output, 0 is input). + @direction - Byte indicating input/output direction of each bit (1 is output, 0 is input). Returns MPSSE_OK on success. Raises an exception on failure. """ if _mpsse.SetDirection(self.context, direction) == MPSSE_FAIL: - raise Exception, self.ErrorString() + raise Exception(self.ErrorString()) return MPSSE_OK def WriteBits(self, bits, n): """ Performs a bitwise write of up to 8 bits at a time. - + @bits - An integer of bits to be written. @n - Transmit n number of least-significant bits. @@ -360,7 +360,7 @@ def WriteBits(self, bits, n): Raises an exception on failure. """ if _mpsse.WriteBits(self.context, bits, n) == MPSSE_FAIL: - raise Exception, self.ErrorString() + raise Exception(self.ErrorString()) return MPSSE_OK def ReadBits(self, n): @@ -384,7 +384,7 @@ def WritePins(self, data): Raises an exception on failure. """ if _mpsse.WritePins(self.context, data) == MPSSE_FAIL: - raise Exception, self.ErrorString() + raise Exception(self.ErrorString()) return MPSSE_OK def ReadPins(self): @@ -401,7 +401,7 @@ def PinState(self, pin, state=-1): Checks the current state of the pins. For use in BITBANG mode only. - @pin - The pin number whose state you want to check. + @pin - The pin number whose state you want to check. @state - The value returned by ReadPins. If not specified, ReadPins will be called automatically. Returns a 1 if the pin is high, 0 if the pin is low.