diff options
author | cliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a> | 2010-07-22 00:14:26 +0000 |
---|---|---|
committer | cliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a> | 2010-07-22 00:14:26 +0000 |
commit | c323f1faf74302209d73c6752d4f68a82a93541b (patch) | |
tree | bffcbd32cf7d81ff8f850c1ca299ade60fcf60d9 /pyserial/serial/serialutil.py | |
parent | de5ef3706fabba618dc4ccc83a4d317e033337c3 (diff) | |
download | pyserial-git-c323f1faf74302209d73c6752d4f68a82a93541b.tar.gz |
fix bytearray class so that readline works again with Python 2.5 and older
Diffstat (limited to 'pyserial/serial/serialutil.py')
-rw-r--r-- | pyserial/serial/serialutil.py | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/pyserial/serial/serialutil.py b/pyserial/serial/serialutil.py index 0f37218..1d8714d 100644 --- a/pyserial/serial/serialutil.py +++ b/pyserial/serial/serialutil.py @@ -2,7 +2,7 @@ # Python Serial Port Extension for Win32, Linux, BSD, Jython # see __init__.py # -# (C) 2001-2009 Chris Liechti <cliechti@gmx.net> +# (C) 2001-2010 Chris Liechti <cliechti@gmx.net> # this is distributed under a free software license, see license.txt # compatibility for older Python < 2.6 @@ -19,6 +19,7 @@ except (NameError, AttributeError): class bytearray(list): # for bytes(bytearray()) usage def __str__(self): return ''.join(self) + def __repr__(self): return 'bytearray(%r)' % ''.join(self) # append automatically converts integers to characters def append(self, item): if isinstance(item, str): @@ -31,6 +32,20 @@ except (NameError, AttributeError): self.append(byte) return self + def __getslice__(self, i, j): + return bytearray(list.__getslice__(self, i, j)) + + def __getitem__(self, item): + if isinstance(item, slice): + return bytearray(list.__getitem__(self, item)) + else: + return ord(list.__getitem__(self, item)) + + def __eq__(self, other): + if isinstance(other, basestring): + other = bytearray(other) + return list.__eq__(self, other) + # all Python versions prior 3.x convert str([17]) to '[17]' instead of '\x11' # so a simple bytes(sequence) doesn't work for all versions def to_bytes(seq): @@ -123,7 +138,6 @@ class FileLike(object): def __iter__(self): return self - def readline(self, size=None, eol=LF): """read a line which is terminated with end-of-line (eol) character ('\n' by default) or until timeout.""" @@ -141,7 +155,7 @@ class FileLike(object): break return bytes(line) - def readlines(self, sizehint=None, eol='\n'): + def readlines(self, sizehint=None, eol=LF): """read a list of lines, until timeout. sizehint is ignored.""" if self.timeout is None: |