From ddd781321d76b0144e3b3a4e31222724bdbaf34d Mon Sep 17 00:00:00 2001 From: cliechti Date: Tue, 28 Jul 2009 01:13:28 +0000 Subject: - improve compatibility with io library (also accept bytearray for write) - update test for Serial+io --- documentation/pyserial_api.rst | 4 ++-- pyserial/examples/test_rawio.py | 33 ++++++++++++++++++++++----------- pyserial/serial/serialcli.py | 4 ++-- pyserial/serial/serialjava.py | 4 ++-- pyserial/serial/serialposix.py | 4 ++-- pyserial/serial/serialwin32.py | 4 ++-- 6 files changed, 32 insertions(+), 21 deletions(-) diff --git a/documentation/pyserial_api.rst b/documentation/pyserial_api.rst index 6b913a5..08f1b45 100644 --- a/documentation/pyserial_api.rst +++ b/documentation/pyserial_api.rst @@ -104,8 +104,8 @@ Classes Write the string *data* to the port. .. versionchanged:: 2.5 - Accepts an instance of :class:`bytes` when available (Python 2.6 - and newer) and :class:`str` otherwiese. + Accepts instances of :class:`bytes` and :class:`bytearray` when + available (Python 2.6 and newer) and :class:`str` otherwiese. .. method:: inWaiting() diff --git a/pyserial/examples/test_rawio.py b/pyserial/examples/test_rawio.py index 674934f..441b37f 100644 --- a/pyserial/examples/test_rawio.py +++ b/pyserial/examples/test_rawio.py @@ -12,8 +12,8 @@ Part of pyserial (http://pyserial.sf.net) (C)2001-2009 cliechti@gmx.net Intended to be run on different platforms, to ensure portability of the code. -This modules contains test for RawSerial. This only works on Python 2.6+ with -the io library. +This modules contains test for the interaction between Serial and the io +library. This only works on Python 2.6+ that introduced the io library. For all these tests a simple hardware is required. Loopback HW adapter: @@ -25,25 +25,36 @@ Shortcut these pin pairs: On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8) """ -import unittest, threading, time +import unittest +import sys +import io import serial +# trick to make that this test run under 2.6 and 3.x without modification. +# problem is, io library on 2.6 does NOT accept type 'str' and 3.x doesn't +# like u'nicode' strings with the prefix and it is not providing an unicode +# function ('str' is now what 'unicode' used to be) +if sys.version_info >= (3, 0): + def unicode(x): return x + + # on which port should the tests be performed: -PORT=0 +PORT = 0 -class Test_RawSerial(unittest.TestCase): +class Test_SerialAndIO(unittest.TestCase): def setUp(self): - self.s = serial.RawSerial(PORT) + self.s = serial.Serial(PORT, timeout=1) + self.io = io.TextIOWrapper(io.BufferedRWPair(self.s, self.s)) def tearDown(self): self.s.close() - def test_hello(self): - self.s.write(bytes("hello")) - hello = self.s.read(5) - #~ print hello - self.failUnlessEqual(hello, bytes("hello")) + def test_hello_raw(self): + self.io.write(unicode("hello\n")) + self.io.flush() + hello = self.io.readline() + self.failUnlessEqual(hello, unicode("hello\n")) if __name__ == '__main__': diff --git a/pyserial/serial/serialcli.py b/pyserial/serial/serialcli.py index 5cb76aa..d8eaf1e 100644 --- a/pyserial/serial/serialcli.py +++ b/pyserial/serial/serialcli.py @@ -167,8 +167,8 @@ class IronSerial(SerialBase): def write(self, data): """Output the given string over the serial port.""" if not self._port_handle: raise portNotOpenError - if not isinstance(data, bytes): - raise TypeError('expected %s, got %s' % (bytes, type(data))) + if not isinstance(data, (bytes, bytearray)): + raise TypeError('expected %s or bytearray, got %s' % (bytes, type(data))) try: # must call overloaded method with byte array argument # as this is the only one not applying encodings diff --git a/pyserial/serial/serialjava.py b/pyserial/serial/serialjava.py index 4a0edb3..2541534 100644 --- a/pyserial/serial/serialjava.py +++ b/pyserial/serial/serialjava.py @@ -166,8 +166,8 @@ class JavaSerial(SerialBase): def write(self, data): """Output the given string over the serial port.""" if not self.sPort: raise portNotOpenError - if not isinstance(data, bytes): - raise TypeError('expected %s, got %s' % (bytes, type(data))) + if not isinstance(data, (bytes, bytearray)): + raise TypeError('expected %s or bytearray, got %s' % (bytes, type(data))) self._outstream.write(data) return len(data) diff --git a/pyserial/serial/serialposix.py b/pyserial/serial/serialposix.py index c3cc284..bbf7fb9 100644 --- a/pyserial/serial/serialposix.py +++ b/pyserial/serial/serialposix.py @@ -462,8 +462,8 @@ class PosixSerial(SerialBase): def write(self, data): """Output the given string over the serial port.""" if self.fd is None: raise portNotOpenError - if not isinstance(data, bytes): - raise TypeError('expected %s, got %s' % (bytes, type(data))) + if not isinstance(data, (bytes, bytearray)): + raise TypeError('expected %s or bytearray, got %s' % (bytes, type(data))) t = len(data) d = data while t > 0: diff --git a/pyserial/serial/serialwin32.py b/pyserial/serial/serialwin32.py index 59572d2..df58e77 100644 --- a/pyserial/serial/serialwin32.py +++ b/pyserial/serial/serialwin32.py @@ -240,8 +240,8 @@ class Win32Serial(SerialBase): def write(self, data): """Output the given string over the serial port.""" if not self.hComPort: raise portNotOpenError - if not isinstance(data, bytes): - raise TypeError('expected %s, got %s' % (bytes, type(data))) + if not isinstance(data, (bytes, bytearray)): + raise TypeError('expected %s or bytearray, got %s' % (bytes, type(data))) if data: #~ win32event.ResetEvent(self._overlappedWrite.hEvent) n = win32.DWORD() -- cgit v1.2.1