summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2009-07-28 01:13:28 +0000
committercliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2009-07-28 01:13:28 +0000
commit08840adc3bd51a32e9d71662338ff90f1cd97cc5 (patch)
treea47e9a0a448648fa2aca0c356d272a8bf5eb7849
parent984052d7eb08a27f9b546d07e846df02ba2dbee8 (diff)
downloadpyserial-08840adc3bd51a32e9d71662338ff90f1cd97cc5.tar.gz
- improve compatibility with io library (also accept bytearray for write)
- update test for Serial+io git-svn-id: http://svn.code.sf.net/p/pyserial/code/trunk/pyserial@255 f19166aa-fa4f-0410-85c2-fa1106f25c8a
-rw-r--r--examples/test_rawio.py33
-rw-r--r--serial/serialcli.py4
-rw-r--r--serial/serialjava.py4
-rw-r--r--serial/serialposix.py4
-rw-r--r--serial/serialwin32.py4
5 files changed, 30 insertions, 19 deletions
diff --git a/examples/test_rawio.py b/examples/test_rawio.py
index 674934f..441b37f 100644
--- a/examples/test_rawio.py
+++ b/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/serial/serialcli.py b/serial/serialcli.py
index 5cb76aa..d8eaf1e 100644
--- a/serial/serialcli.py
+++ b/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/serial/serialjava.py b/serial/serialjava.py
index 4a0edb3..2541534 100644
--- a/serial/serialjava.py
+++ b/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/serial/serialposix.py b/serial/serialposix.py
index c3cc284..bbf7fb9 100644
--- a/serial/serialposix.py
+++ b/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/serial/serialwin32.py b/serial/serialwin32.py
index 59572d2..df58e77 100644
--- a/serial/serialwin32.py
+++ b/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()