def test_fileno(self): t1 = self.writeTmp("A\nB") t2 = self.writeTmp("C\nD") fi = FileInput(files=(t1, t2)) self.assertEqual(fi.fileno(), -1) line = next(fi) self.assertNotEqual(fi.fileno(), -1) fi.nextfile() self.assertEqual(fi.fileno(), -1) line = list(fi) self.assertEqual(fi.fileno(), -1)
def test_fileno(self): try: t1 = writeTmp(1, ["A\nB"]) t2 = writeTmp(2, ["C\nD"]) fi = FileInput(files=(t1, t2)) self.assertEqual(fi.fileno(), -1) line = fi.next() self.assertNotEqual(fi.fileno(), -1) fi.nextfile() self.assertEqual(fi.fileno(), -1) line = list(fi) self.assertEqual(fi.fileno(), -1) finally: remove_tempfiles(t1, t2)
def test_fileno(self): t1 = t2 = None try: t1 = writeTmp(1, ["A\nB"]) t2 = writeTmp(2, ["C\nD"]) fi = FileInput(files=(t1, t2)) self.assertEqual(fi.fileno(), -1) line = next(fi) self.assertNotEqual(fi.fileno(), -1) fi.nextfile() self.assertEqual(fi.fileno(), -1) line = list(fi) self.assertEqual(fi.fileno(), -1) finally: remove_tempfiles(t1, t2)
encoding = sys.getfilesystemencoding() if encoding is None: encoding = 'ascii' fi = FileInput(files=unicode(t1, encoding)) lines = list(fi) verify(lines == ["A\n", "B"]) finally: remove_tempfiles(t1) if verbose: print "16. fileno()" try: t1 = writeTmp(1, ["A\nB"]) t2 = writeTmp(2, ["C\nD"]) fi = FileInput(files=(t1, t2)) verify(fi.fileno() == -1) line = fi.next() verify(fi.fileno() != -1) fi.nextfile() verify(fi.fileno() == -1) line = list(fi) verify(fi.fileno() == -1) finally: remove_tempfiles(t1, t2) if verbose: print "17. Specify opening mode" try: # invalid mode, should raise ValueError fi = FileInput(mode="w") raise TestFailed("FileInput should reject invalid mode argument")
class ManyFile(object): """A generalized version of file input that allows for stopping reading the current file and switching to a different file. Reading the old file is resumed once the current file is complete. Useful for #include or similiar constructs. """ def __init__(self, files=None, hook=None): self.fi = [ ] # an array of file input objects. We push and pop onto this so we can resume. self.files = [] # an array of files that need to be read later. self.hook = None self.current = None # the current file input object. self.line = 0 if files is not None: self.current = FileInput(files, openhook=self.hook) def include(self, files): if self.current is not None: self.fi.push(self.current) self.current = FileInput(files, openhook=self.hook) def fileno(self): if self.current is None: return None return self.current.fileno() def filename(self): if self.current is None: return None return self.current.filename() def lineno(self): return self.line def filelineno(self): if self.current is None: return None return self.current.filelineno() def isfirstline(self): if self.current is None: return None return self.current.isfirstline() def isstdin(self): return False def nextfile(self): if self.current is not None: self.current.nextfile() def close(self): if self.current: self.current.close() self.fi = [] self.files = [] def readline(self): if self.current is not None: l = self.current.readline() if len(l) > 0: self.line = self.line + 1 return l # Advance to the next file if len(self.fi) > 0: self.current = self.fi.pop() return self.readline() # see if there are any files left to open if len(self.files) > 0: self.current = FileInput(self.files, openhook=fileinput.hook_compressed) self.files = [] return self.readline() return "" def __getimem__(self): return self.readline() def __iter__(self): return self def next(self): l = self.__getimem__() if l != '': return l raise StopIteration
class ManyFile(object): """A generalized version of file input that allows for stopping reading the current file and switching to a different file. Reading the old file is resumed once the current file is complete. Useful for #include or similiar constructs. """ def __init__(self, files=None, hook = None): self.fi = [] # an array of file input objects. We push and pop onto this so we can resume. self.files = [] # an array of files that need to be read later. self.hook = None self.current = None # the current file input object. self.line = 0 if files is not None: self.current = FileInput(files, openhook = self.hook) def include(self, files): if self.current is not None: self.fi.push(self.current) self.current = FileInput(files, openhook = self.hook) def fileno(self): if self.current is None: return None return self.current.fileno() def filename(self): if self.current is None: return None return self.current.filename() def lineno(self): return self.line def filelineno(self): if self.current is None: return None return self.current.filelineno() def isfirstline(self): if self.current is None: return None return self.current.isfirstline() def isstdin(self): return False def nextfile(self): if self.current is not None: self.current.nextfile() def close(self): if self.current: self.current.close() self.fi = [] self.files = [] def readline(self): if self.current is not None: l = self.current.readline() if len(l) > 0: self.line = self.line + 1 return l # Advance to the next file if len (self.fi) > 0: self.current = self.fi.pop() return self.readline() # see if there are any files left to open if len (self.files) > 0: self.current = FileInput(self.files, openhook = fileinput.hook_compressed) self.files = [] return self.readline() return "" def __getimem__ (self): return self.readline() def __iter__(self): return self def next(self): l = self.__getimem__() if l != '': return l raise StopIteration