(original) (raw)

Index: Lib/fileinput.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/fileinput.py,v retrieving revision 1.16 diff -u -p -c -r1.16 fileinput.py *** Lib/fileinput.py 8 Jan 2003 16:33:16 -0000 1.16 --- Lib/fileinput.py 31 May 2005 21:39:40 -0000 *************** numbers are zero; nextfile() has no effe *** 28,35 **** read, filename() and the line number functions return the values pertaining to the last line read; nextfile() has no effect. ! All files are opened in text mode. If an I/O error occurs during ! opening or reading a file, the IOError exception is raised. If sys.stdin is used more than once, the second and further use will return no lines, except perhaps for interactive use, or if it has been --- 28,37 ---- read, filename() and the line number functions return the values pertaining to the last line read; nextfile() has no effect. ! All files are opened in text mode by default, you can override this by ! setting the mode parameter to input() or FileInput.__init__(). ! If an I/O error occurs during opening or reading a file, the IOError ! exception is raised. If sys.stdin is used more than once, the second and further use will return no lines, except perhaps for interactive use, or if it has been *************** buffer size. *** 72,82 **** XXX Possible additions: - optional getopt argument processing - - specify open mode ('r' or 'rb') - fileno() - isatty() - read(), read(size), even readlines() """ import sys, os --- 74,85 ---- XXX Possible additions: - optional getopt argument processing - fileno() - isatty() - read(), read(size), even readlines() + Added 2005-05-31: mode argument to input() and FileInput.__init__() + """ import sys, os *************** _state = None *** 88,95 **** DEFAULT_BUFSIZE = 8*1024 ! def input(files=None, inplace=0, backup="", bufsize=0): ! """input([files[, inplace[, backup]]]) Create an instance of the FileInput class. The instance will be used as global state for the functions of this module, and is also returned --- 91,98 ---- DEFAULT_BUFSIZE = 8*1024 ! def input(files=None, inplace=0, backup="", bufsize=0, mode='r'): ! """input([files[, inplace[, backup[, mode]]]]) Create an instance of the FileInput class. The instance will be used as global state for the functions of this module, and is also returned *************** def input(files=None, inplace=0, backup= *** 99,105 **** global _state if _state and _state._file: raise RuntimeError, "input() already active" ! _state = FileInput(files, inplace, backup, bufsize) return _state def close(): --- 102,108 ---- global _state if _state and _state._file: raise RuntimeError, "input() already active" ! _state = FileInput(files, inplace, backup, bufsize, mode) return _state def close(): *************** def isstdin(): *** 172,178 **** return _state.isstdin() class FileInput: ! """class FileInput([files[, inplace[, backup]]]) Class FileInput is the implementation of the module; its methods filename(), lineno(), fileline(), isfirstline(), isstdin(), nextfile() --- 175,181 ---- return _state.isstdin() class FileInput: ! """class FileInput([files[, inplace[, backup[, mode]]]]) Class FileInput is the implementation of the module; its methods filename(), lineno(), fileline(), isfirstline(), isstdin(), nextfile() *************** class FileInput: *** 183,189 **** sequential order; random access and readline() cannot be mixed. """ ! def __init__(self, files=None, inplace=0, backup="", bufsize=0): if type(files) == type(''): files = (files,) else: --- 186,192 ---- sequential order; random access and readline() cannot be mixed. """ ! def __init__(self, files=None, inplace=0, backup="", bufsize=0, mode='r'): if type(files) == type(''): files = (files,) else: *************** class FileInput: *** 207,212 **** --- 210,219 ---- self._backupfilename = None self._buffer = [] self._bufindex = 0 + # restrict mode argument accordingly + if mode not in ['r', 'rU', 'U', 'rb']: + raise ValueError("FileInput opening mode must be r, rU, U or rb") + self._mode = mode def __del__(self): self.close() *************** class FileInput: *** 298,304 **** except os.error: pass # The next few lines may raise IOError os.rename(self._filename, self._backupfilename) ! self._file = open(self._backupfilename, "r") try: perm = os.fstat(self._file.fileno()).st_mode except OSError: --- 305,311 ---- except os.error: pass # The next few lines may raise IOError os.rename(self._filename, self._backupfilename) ! self._file = open(self._backupfilename, self._mode) try: perm = os.fstat(self._file.fileno()).st_mode except OSError: *************** class FileInput: *** 317,323 **** sys.stdout = self._output else: # This may raise IOError ! self._file = open(self._filename, "r") self._buffer = self._file.readlines(self._bufsize) self._bufindex = 0 if not self._buffer: --- 324,330 ---- sys.stdout = self._output else: # This may raise IOError ! self._file = open(self._filename, self._mode) self._buffer = self._file.readlines(self._bufsize) self._bufindex = 0 if not self._buffer: Index: Lib/test/test_fileinput.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/test/test_fileinput.py,v retrieving revision 1.2 diff -u -p -c -r1.2 test_fileinput.py *** Lib/test/test_fileinput.py 23 Jul 2002 19:03:52 -0000 1.2 --- Lib/test/test_fileinput.py 31 May 2005 21:40:21 -0000 *************** try: *** 157,159 **** --- 157,176 ---- verify(fi.lineno() == 6) finally: remove_tempfiles(t1, t2) + + if verbose: + print "15. Specifying a mode for opening" + try: + # invalid mode, should raise ValueError + fi = FileInput(mode='w') + print " failed" + except ValueError: + pass + # Try opening in universal newline mode. + try: + t1 = writeTmp(1, ["A\nB\r\nC\rD"]) + fi = FileInput(files=[t1], mode='U') + lines = list(fi) + verify(lines == ["A\n", "B\n", "C\n", "D"]) + finally: + remove_tempfiles(t1, t2) Index: Doc/lib/libfileinput.tex =================================================================== RCS file: /cvsroot/python/python/dist/src/Doc/lib/libfileinput.tex,v retrieving revision 1.7 diff -u -p -c -r1.7 libfileinput.tex *** Doc/lib/libfileinput.tex 10 Nov 2003 14:43:16 -0000 1.7 --- Doc/lib/libfileinput.tex 31 May 2005 21:40:32 -0000 *************** *** 1,3 **** --- 1,4 ---- + \section{\module{fileinput} --- Iterate over lines from multiple input streams} \declaremodule{standard}{fileinput} *************** empty. If a filename is \code{'-'}, it *** 26,33 **** it as the first argument to \function{input()}. A single file name is also allowed. ! All files are opened in text mode. If an I/O error occurs during ! opening or reading a file, \exception{IOError} is raised. If \code{sys.stdin} is used more than once, the second and further use will return no lines, except perhaps for interactive use, or if it has --- 27,36 ---- it as the first argument to \function{input()}. A single file name is also allowed. ! All files are opened in text mode by default, you can override this by ! setting the \code{mode} parameter in the call to \function{input()} ! or the constructor of \class{FileInput}. If an I/O ! error occurs during opening or reading a file, \exception{IOError} is raised. If \code{sys.stdin} is used more than once, the second and further use will return no lines, except perhaps for interactive use, or if it has *************** is present. *** 44,50 **** The following function is the primary interface of this module: \begin{funcdesc}{input}{\optional{files\optional{, ! inplace\optional{, backup}}}} Create an instance of the \class{FileInput} class. The instance will be used as global state for the functions of this module, and is also returned to use during iteration. The parameters to this --- 47,54 ---- The following function is the primary interface of this module: \begin{funcdesc}{input}{\optional{files\optional{, ! inplace\optional{, backup\optional{, ! mode}}}}} Create an instance of the \class{FileInput} class. The instance will be used as global state for the functions of this module, and is also returned to use during iteration. The parameters to this *************** The following functions use the global s *** 67,72 **** --- 71,77 ---- read. Before the first line has been read, returns \code{0}. After the last line of the last file has been read, returns the line number of that line. + \end{funcdesc} \begin{funcdesc}{filelineno}{} *************** The class which implements the sequence *** 105,111 **** module is available for subclassing as well: \begin{classdesc}{FileInput}{\optional{files\optional{, ! inplace\optional{, backup}}}} Class \class{FileInput} is the implementation; its methods \method{filename()}, \method{lineno()}, \method{fileline()}, \method{isfirstline()}, \method{isstdin()}, \method{nextfile()} and --- 110,117 ---- module is available for subclassing as well: \begin{classdesc}{FileInput}{\optional{files\optional{, ! inplace\optional{, backup\optional{, ! mode}}}}} Class \class{FileInput} is the implementation; its methods \method{filename()}, \method{lineno()}, \method{fileline()}, \method{isfirstline()}, \method{isstdin()}, \method{nextfile()} and