Skip to content

Commit

Permalink
new readline test passes
Browse files Browse the repository at this point in the history
Changed file.py readline() to always check for a newline. Had to make a
few changes for what went into self._rbuffer in the case where buffer
size was met or exceeded and we found a newline.
  • Loading branch information
achapp authored and bitprophet committed Dec 17, 2014
1 parent 34c4d0c commit 0a54853
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
12 changes: 6 additions & 6 deletions paramiko/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ def readline(self, size=None):
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
Expand All @@ -218,11 +219,11 @@ def readline(self, size=None):
# 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
Expand All @@ -245,14 +246,13 @@ def readline(self, size=None):
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):
Expand Down
1 change: 1 addition & 0 deletions tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def test_2_readline(self):
# 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.')
Expand Down

0 comments on commit 0a54853

Please sign in to comment.