Skip to content

Commit 369aa5f

Browse files
author
willmcgugan@gmail.com
committed
Fixes, including hang bug in readline
1 parent d622621 commit 369aa5f

7 files changed

Lines changed: 47 additions & 36 deletions

File tree

CHANGES.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
* Removed obsolete module fs.objectree; use fs.path.PathMap instead.
8080
* Added setcontents_async method to base
8181
* Added `appdirfs` module to abstract per-user application directories
82-
82+
8383
0.5:
8484

8585
* Ported to Python 3.X
@@ -88,3 +88,8 @@
8888
* Added sqlitefs to fs.contrib, contributed by Nitin Bhide
8989
* Added archivefs to fs.contrib, contributed by btimby
9090
* Added some polish to fstree command and unicode box lines rather than ascii art
91+
92+
0.5:
93+
94+
* Fixed a hang bug in readline
95+

fs/commands/runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def output_table(self, table, col_process=None, verbose=False):
258258

259259
def error(self, *msgs):
260260
for msg in msgs:
261-
self.error_file.write('%s: %s' % (self.name, self.text_encode(msg)))
261+
self.error_file.write("{}: {}".format(self.name, msg).encode(self.encoding))
262262

263263
def get_optparse(self):
264264
optparse = OptionParser(usage=self.usage, version=self.version)

fs/filelike.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ def readline(self,size=-1):
494494
nextBit = self.read(self._bufsize)
495495
bits.append(nextBit)
496496
sizeSoFar += len(nextBit)
497-
if nextBit == b(""):
497+
if not nextBit:
498498
break
499499
if size > 0 and sizeSoFar >= size:
500500
break

fs/ftpfs.py

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -804,40 +804,15 @@ def close(self):
804804
pass
805805
self.closed = True
806806

807-
def __iter__(self):
808-
return self.next()
809-
810-
@synchronize
811807
def next(self):
812-
""" Line iterator
808+
return self.readline()
809+
810+
def readline(self, size=None):
811+
return next(iotools.line_iterator(self, size))
812+
813+
def __iter__(self):
814+
return iotools.line_iterator(self)
813815

814-
This isn't terribly efficient. It would probably be better to do
815-
a read followed by splitlines.
816-
"""
817-
endings = b('\r\n')
818-
chars = []
819-
append = chars.append
820-
read = self.read
821-
join = b('').join
822-
while True:
823-
char = read(1)
824-
if not char:
825-
if chars:
826-
yield join(chars)
827-
break
828-
append(char)
829-
if char in endings:
830-
line = join(chars)
831-
del chars[:]
832-
c = read(1)
833-
if not char:
834-
yield line
835-
break
836-
if c in endings and c != char:
837-
yield line + c
838-
else:
839-
yield line
840-
append(c)
841816

842817
def ftperrors(f):
843818
@wraps(f)

fs/iotools.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,30 @@ def copy_file_to_fs(f, fs, path, encoding=None, errors=None, progress_callback=N
211211
return bytes_written
212212

213213

214+
def line_iterator(f, size=None):
215+
"""A not terribly efficient char by char line iterator"""
216+
read = f.read
217+
line = []
218+
append = line.append
219+
c = 1
220+
if size is None or size < 0:
221+
while c:
222+
c = read(1)
223+
if c:
224+
append(c)
225+
if c in (b'\n', b''):
226+
yield b''.join(line)
227+
del line[:]
228+
else:
229+
while c:
230+
c = read(1)
231+
if c:
232+
append(c)
233+
if c in (b'\n', b'') or len(line) >= size:
234+
yield b''.join(line)
235+
del line[:]
236+
237+
214238
if __name__ == "__main__":
215239
print("Reading a binary file")
216240
bin_file = open('tests/data/UTF-8-demo.txt', 'rb')

fs/path.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ def isprefix(path1, path2):
376376

377377

378378
def forcedir(path):
379-
"""Ensure the path ends with a trailing /
379+
"""Ensure the path ends with a trailing formward slash
380380
381381
:param path: An FS path
382382

fs/tests/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ def test_createfile(self):
147147
self.fs.createfile("test.txt", wipe=True)
148148
self.assertEqual(self.fs.getcontents("test.txt", "rb"), b(''))
149149

150+
def test_readline(self):
151+
text = b"Hello\nWorld\n"
152+
self.fs.setcontents('a.txt', text)
153+
with self.fs.open('a.txt', 'rb') as f:
154+
line = f.readline()
155+
self.assertEqual(line, b"Hello\n")
156+
150157
def test_setcontents(self):
151158
# setcontents() should accept both a string...
152159
self.fs.setcontents("hello", b("world"))

0 commit comments

Comments
 (0)