Skip to content

Commit

Permalink
fix occasional INPUT PAST END error on INPUT$(1, #1) at CR LF with un…
Browse files Browse the repository at this point in the history
…iversal line feed convention for text files
  • Loading branch information
robhagemans committed Jul 8, 2022
1 parent 84b9b78 commit 8e7fcf2
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions pcbasic/basic/codepage.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,21 @@ def __init__(self, stream):

def read(self, n=-1):
"""Read n bytes from stream with codepage conversion."""
new_bytes = self._stream.read(n)
if new_bytes:
new_last = new_bytes[-1:]
if self._last == b'\r' and new_bytes.startswith(b'\n'):
if n == 0:
return b''
output = b''
while n < 0 or len(output) < n:
new_bytes = self._stream.read(n - len(output))
# empty means end of file
if not new_bytes:
break
last, self._last = self._last, new_bytes[-1:]
# absorb CR LF
if last == b'\r' and new_bytes[:1] == b'\n':
new_bytes = new_bytes[1:]
self._last = new_last
new_bytes = new_bytes.replace(b'\r\n', b'\r').replace(b'\n', b'\r')
return new_bytes
output += new_bytes
output = output.replace(b'\n', b'\r')
return output


##################################################
Expand Down

0 comments on commit 8e7fcf2

Please sign in to comment.