diff options
-rw-r--r-- | paramiko/file.py | 12 | ||||
-rwxr-xr-x | tests/test_file.py | 1 |
2 files changed, 7 insertions, 6 deletions
diff --git a/paramiko/file.py b/paramiko/file.py index 139c453b..f549cb99 100644 --- a/paramiko/file.py +++ b/paramiko/file.py @@ -204,6 +204,7 @@ class BufferedFile (object): if not (self._flags & self.FLAG_READ): raise IOError('File not open for reading') line = self._rbuffer + truncated = False; while True: if self._at_trailing_cr and (self._flags & self.FLAG_UNIVERSAL_NEWLINE) and (len(line) > 0): # edge case: the newline may be '\r\n' and we may have read @@ -218,11 +219,11 @@ class BufferedFile (object): # enough. if (size is not None) and (size >= 0): if len(line) >= size: - # truncate line and return + # truncate line self._rbuffer = line[size:] line = line[:size] - #self._pos += len(line) - break#return line if self._flags & self.FLAG_BINARY else u(line) + truncated = True + break n = size - len(line) else: n = self._bufsize @@ -245,14 +246,13 @@ class BufferedFile (object): if (rpos >= 0) and (rpos < pos or pos < 0): pos = rpos if pos == -1: - #self._rbuffer = line[size:] - #line = line[:size] self._pos += len(line) return line if self._flags & self.FLAG_BINARY else u(line) xpos = pos + 1 if (line[pos] == cr_byte_value) and (xpos < len(line)) and (line[xpos] == linefeed_byte_value): xpos += 1 - self._rbuffer = line[xpos:] + + self._rbuffer = line[xpos:] + self._rbuffer if truncated else line[xpos:] lf = line[pos:xpos] line = line[:pos] + linefeed_byte if (len(self._rbuffer) == 0) and (lf == cr_byte): diff --git a/tests/test_file.py b/tests/test_file.py index 24a7fa9b..044dc591 100755 --- a/tests/test_file.py +++ b/tests/test_file.py @@ -77,6 +77,7 @@ class BufferedFileTest (unittest.TestCase): # truncated line: self.assertEqual(f.readline(7), 'Third l') self.assertEqual(f.readline(), 'ine.\n') + # readline should not read past the fourth line self.assertEqual(f.readline(25), 'Fourth line.\n') self.assertEqual(f.readline(), 'Fifth line.\n') self.assertEqual(f.readline(), 'Final line non-terminated.') |