summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2013-10-16 15:35:11 +0000
committercliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2013-10-16 15:35:11 +0000
commite0984e1bd7e8afc4c8f04d191a11ce87492fdf38 (patch)
tree2b3249e693dd82f7a607c446a4fb97f4edc2f707
parentf911f1be9b07d0489f4e51c8d39e277c552ed9ae (diff)
downloadpyserial-e0984e1bd7e8afc4c8f04d191a11ce87492fdf38.tar.gz
do not allow negative baudrates, improve Python 3 compatibility of tests
git-svn-id: http://svn.code.sf.net/p/pyserial/code/trunk/pyserial@480 f19166aa-fa4f-0410-85c2-fa1106f25c8a
-rw-r--r--serial/serialutil.py7
-rw-r--r--test/handlers/protocol_test.py2
-rw-r--r--test/run_all_tests.py12
-rw-r--r--test/test_readline.py22
4 files changed, 24 insertions, 19 deletions
diff --git a/serial/serialutil.py b/serial/serialutil.py
index f575963..f28ece4 100644
--- a/serial/serialutil.py
+++ b/serial/serialutil.py
@@ -73,7 +73,7 @@ def to_bytes(seq):
else:
b = bytearray()
for item in seq:
- b.append(item) # this one handles int and str
+ b.append(item) # this one handles int and str for our emulation and ints for Python 3.x
return bytes(b)
# create control bytes
@@ -335,10 +335,13 @@ class SerialBase(object):
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)
+ b = int(baudrate)
except TypeError:
raise ValueError("Not a valid baudrate: %r" % (baudrate,))
else:
+ if b <= 0:
+ raise ValueError("Not a valid baudrate: %r" % (baudrate,))
+ self._baudrate = b
if self._isOpen: self._reconfigurePort()
def getBaudrate(self):
diff --git a/test/handlers/protocol_test.py b/test/handlers/protocol_test.py
index 57cdf58..42ac4b2 100644
--- a/test/handlers/protocol_test.py
+++ b/test/handlers/protocol_test.py
@@ -72,7 +72,7 @@ class DummySerial(SerialBase):
self.logger.debug('enabled logging')
else:
raise ValueError('unknown option: %r' % (option,))
- except ValueError, e:
+ except ValueError as e:
raise SerialException('expected a string in the form "[test://][option[/option...]]": %s' % e)
return (host, port)
diff --git a/test/run_all_tests.py b/test/run_all_tests.py
index 3bb971a..e7f115d 100644
--- a/test/run_all_tests.py
+++ b/test/run_all_tests.py
@@ -12,11 +12,13 @@ import os
import time
# inject local copy to avoid testing the installed version instead of the
-# working copy
-sys.path.insert(0, '..')
+# working copy (only for 2.x as the sources would need to be translated with
+# 2to3 for Python 3, use installed module instead for Python 3).
+if sys.version_info < (3, 0):
+ sys.path.insert(0, '..')
import serial
-print "Patching sys.path to test local version. Testing Version: %s" % (serial.VERSION,)
+print("Patching sys.path to test local version. Testing Version: %s" % (serial.VERSION,))
PORT = 'loop://'
if len(sys.argv) > 1:
@@ -31,11 +33,11 @@ for modulename in [os.path.splitext(x)[0]
try:
module = __import__(modulename)
except ImportError:
- print "skipping %s" % modulename
+ print("skipping %s" % (modulename,))
else:
module.PORT = PORT
testsuite = unittest.findTestCases(module)
- print "found %s tests in %r" % (testsuite.countTestCases(), modulename)
+ print("found %s tests in %r" % (testsuite.countTestCases(), modulename))
mainsuite.addTest(testsuite)
verbosity = 1
diff --git a/test/test_readline.py b/test/test_readline.py
index 8577ca9..677ce85 100644
--- a/test/test_readline.py
+++ b/test/test_readline.py
@@ -52,39 +52,39 @@ class Test_Readline(unittest.TestCase):
def test_readline(self):
"""Test readline method"""
- self.s.write(serial.to_bytes("1\n2\n3\n"))
- self.failUnlessEqual(self.s.readline(), serial.to_bytes("1\n"))
- self.failUnlessEqual(self.s.readline(), serial.to_bytes("2\n"))
- self.failUnlessEqual(self.s.readline(), serial.to_bytes("3\n"))
+ self.s.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]))
+ self.failUnlessEqual(self.s.readline(), serial.to_bytes([0x31, 0x0a]))
+ self.failUnlessEqual(self.s.readline(), serial.to_bytes([0x32, 0x0a]))
+ self.failUnlessEqual(self.s.readline(), serial.to_bytes([0x33, 0x0a]))
# this time we will get a timeout
- self.failUnlessEqual(self.s.readline(), serial.to_bytes(""))
+ self.failUnlessEqual(self.s.readline(), serial.to_bytes([]))
def test_readlines(self):
"""Test readlines method"""
- self.s.write(serial.to_bytes("1\n2\n3\n"))
+ self.s.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]))
self.failUnlessEqual(
self.s.readlines(),
- [serial.to_bytes("1\n"), serial.to_bytes("2\n"), serial.to_bytes("3\n")]
+ [serial.to_bytes([0x31, 0x0a]), serial.to_bytes([0x32, 0x0a]), serial.to_bytes([0x33, 0x0a])]
)
def test_xreadlines(self):
"""Test xreadlines method (skipped for io based systems)"""
if hasattr(self.s, 'xreadlines'):
- self.s.write(serial.to_bytes("1\n2\n3\n"))
+ self.s.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]))
self.failUnlessEqual(
list(self.s.xreadlines()),
- [serial.to_bytes("1\n"), serial.to_bytes("2\n"), serial.to_bytes("3\n")]
+ [serial.to_bytes([0x31, 0x0a]), serial.to_bytes([0x32, 0x0a]), serial.to_bytes([0x33, 0x0a])]
)
def test_for_in(self):
"""Test for line in s"""
- self.s.write(serial.to_bytes("1\n2\n3\n"))
+ self.s.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]))
lines = []
for line in self.s:
lines.append(line)
self.failUnlessEqual(
lines,
- [serial.to_bytes("1\n"), serial.to_bytes("2\n"), serial.to_bytes("3\n")]
+ [serial.to_bytes([0x31, 0x0a]), serial.to_bytes([0x32, 0x0a]), serial.to_bytes([0x33, 0x0a])]
)
def test_alternate_eol(self):