diff options
author | cliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a> | 2009-07-28 00:13:52 +0000 |
---|---|---|
committer | cliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a> | 2009-07-28 00:13:52 +0000 |
commit | 2750b83755484a5ba70a2ad364c3160def6ab5b0 (patch) | |
tree | cdaf0e7b8b3f27590925de8e907c0ba6e1e4ba18 | |
parent | a75900cb162fcddad0f63f81f577cc0e0299d109 (diff) | |
download | pyserial-git-2750b83755484a5ba70a2ad364c3160def6ab5b0.tar.gz |
fix some minor issues that were uncovered when run with 3.x
-rw-r--r-- | pyserial/serial/serialposix.py | 24 | ||||
-rw-r--r-- | pyserial/serial/serialutil.py | 18 |
2 files changed, 24 insertions, 18 deletions
diff --git a/pyserial/serial/serialposix.py b/pyserial/serial/serialposix.py index 41bac8b..c3cc284 100644 --- a/pyserial/serial/serialposix.py +++ b/pyserial/serial/serialposix.py @@ -282,12 +282,17 @@ class PosixSerial(SerialBase): except Exception, msg: self.fd = None raise SerialException("could not open port %s: %s" % (self._port, msg)) - #~ fcntl.fcntl(self.fd, FCNTL.F_SETFL, 0) #set blocking + #~ fcntl.fcntl(self.fd, FCNTL.F_SETFL, 0) # set blocking try: self._reconfigurePort() except: - os.close(self.fd) + try: + os.close(self.fd) + except: + # ignore any exception when closing the port + # also to keep original exception that happened when setting up + pass self.fd = None raise else: @@ -301,19 +306,19 @@ class PosixSerial(SerialBase): raise SerialException("Can only operate on a valid port handle") custom_baud = None - vmin = vtime = 0 #timeout is done via select + vmin = vtime = 0 # timeout is done via select if self._interCharTimeout is not None: vmin = 1 vtime = int(self._interCharTimeout * 10) try: iflag, oflag, cflag, lflag, ispeed, ospeed, cc = termios.tcgetattr(self.fd) - except termios.error, msg: #if a port is nonexistent but has a /dev file, it'll fail here + except termios.error, msg: # if a port is nonexistent but has a /dev file, it'll fail here raise SerialException("Could not configure port: %s" % msg) # set up raw mode / no echo / binary cflag |= (TERMIOS.CLOCAL|TERMIOS.CREAD) lflag &= ~(TERMIOS.ICANON|TERMIOS.ECHO|TERMIOS.ECHOE|TERMIOS.ECHOK|TERMIOS.ECHONL| TERMIOS.ISIG|TERMIOS.IEXTEN) #|TERMIOS.ECHOPRT - for flag in ('ECHOCTL', 'ECHOKE'): #netbsd workaround for Erk + for flag in ('ECHOCTL', 'ECHOKE'): # netbsd workaround for Erk if hasattr(TERMIOS, flag): lflag &= ~getattr(TERMIOS, flag) @@ -326,7 +331,7 @@ class PosixSerial(SerialBase): # setup baud rate try: - ispeed = ospeed = getattr(TERMIOS,'B%s' % (self._baudrate)) + ispeed = ospeed = getattr(TERMIOS, 'B%s' % (self._baudrate)) except AttributeError: try: ispeed = ospeed = baudrate_constants[self._baudrate] @@ -392,12 +397,12 @@ class PosixSerial(SerialBase): cflag |= (TERMIOS.CRTSCTS) else: cflag &= ~(TERMIOS.CRTSCTS) - elif hasattr(TERMIOS, 'CNEW_RTSCTS'): #try it with alternate constant name + elif hasattr(TERMIOS, 'CNEW_RTSCTS'): # try it with alternate constant name if self._rtscts: cflag |= (TERMIOS.CNEW_RTSCTS) else: cflag &= ~(TERMIOS.CNEW_RTSCTS) - #XXX should there be a warning if setting up rtscts (and xonxoff etc) fails?? + # XXX should there be a warning if setting up rtscts (and xonxoff etc) fails?? # buffer # vmin "minimal number of characters to be read. = for non blocking" @@ -449,7 +454,8 @@ class PosixSerial(SerialBase): break # timeout buf = os.read(self.fd, size - len(read)) read.extend(buf) - if (self._timeout >= 0 or self._interCharTimeout > 0) and not buf: + if ((self._timeout is not None and self._timeout >= 0) or + (self._interCharTimeout is not None and self._interCharTimeout > 0)) and not buf: break # early abort on timeout return bytes(read) diff --git a/pyserial/serial/serialutil.py b/pyserial/serial/serialutil.py index 92bbcdd..38e3d89 100644 --- a/pyserial/serial/serialutil.py +++ b/pyserial/serial/serialutil.py @@ -11,8 +11,8 @@ try: bytearray except AttributeError: # Python older than 2.6 do not have these types. Like for Python 2.6 they - # should behave like str. for Python older than 3.0 we want to work with - # strings anyway, only later versions have a trues bytes type. + # should behave like str. For Python older than 3.0 we want to work with + # strings anyway, only later versions have a true bytes type. bytes = str # bytearray is a mutable type that is easily turned into an instance of # bytes @@ -25,7 +25,11 @@ except AttributeError: list.append(self, item) else: list.append(self, chr(item)) - + XON = chr(17) + XOFF = chr(19) +else: + XON = bytes([17]) + XOFF = bytes([19]) PARITY_NONE, PARITY_EVEN, PARITY_ODD, PARITY_MARK, PARITY_SPACE = 'N', 'E', 'O', 'M', 'S' STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO = (1, 1.5, 2) @@ -39,9 +43,6 @@ PARITY_NAMES = { PARITY_SPACE: 'Space', } -XON = chr(17) -XOFF = chr(19) - class SerialException(IOError): """Base class for serial port related exceptions.""" @@ -236,7 +237,7 @@ class SerialBase(object): def setBaudrate(self, baudrate): """Change baud rate. It raises a ValueError if the port is open and the - baud rate is not possible. If the port is closed, then tha value is + baud rate is not possible. If the port is closed, then the value is accepted and the exception is raised when the port is opened.""" try: self._baudrate = int(baudrate) @@ -294,12 +295,11 @@ class SerialBase(object): def setTimeout(self, timeout): """Change timeout setting.""" if timeout is not None: - if timeout < 0: raise ValueError("Not a valid timeout: %r" % (timeout,)) try: timeout + 1 # test if it's a number, will throw a TypeError if not... except TypeError: raise ValueError("Not a valid timeout: %r" % (timeout,)) - + if timeout < 0: raise ValueError("Not a valid timeout: %r" % (timeout,)) self._timeout = timeout if self._isOpen: self._reconfigurePort() |