*** /python2.3/fileinput.py.old Fri May 28 02:26:10 2004 --- /python2.3/fileinput.py Sat May 29 01:39:06 2004 *************** *** 15,21 **** Functions filename(), lineno() return the filename and cumulative line number of the line that has just been read; filelineno() returns its line number in the current file; isfirstline() returns true iff the ! line just read is the first line of its file; isstdin() returns true iff the line was read from sys.stdin. Function nextfile() closes the current file so that the next iteration will read the first line from the next file (if any); lines not read from the file will not count --- 15,22 ---- Functions filename(), lineno() return the filename and cumulative line number of the line that has just been read; filelineno() returns its line number in the current file; isfirstline() returns true iff the ! line just read is the first line of its file; islastline() returns true ! iff the line just read is the last line of its file; isstdin() returns true iff the line was read from sys.stdin. Function nextfile() closes the current file so that the next iteration will read the first line from the next file (if any); lines not read from the file will not count *************** *** 43,50 **** character; otherwise lines are returned including the trailing newline. ! Class FileInput is the implementation; its methods filename(), ! lineno(), fileline(), isfirstline(), isstdin(), nextfile() and close() correspond to the functions in the module. In addition it has a readline() method which returns the next input line, and a __getitem__() method which implements the sequence behavior. The --- 44,51 ---- character; otherwise lines are returned including the trailing newline. ! Class FileInput is the implementation; its methods filename(), lineno(), ! fileline(), isfirstline(), islastline(), isstdin(), nextfile() and close() correspond to the functions in the module. In addition it has a readline() method which returns the next input line, and a __getitem__() method which implements the sequence behavior. The *************** *** 82,88 **** import sys, os __all__ = ["input","close","nextfile","filename","lineno","filelineno", ! "isfirstline","isstdin","FileInput"] _state = None --- 83,89 ---- import sys, os __all__ = ["input","close","nextfile","filename","lineno","filelineno", ! "isfirstline","islastline","isstdin","FileInput"] _state = None *************** *** 155,167 **** def isfirstline(): """ ! Returns true the line just read is the first line of its file, otherwise returns false. """ if not _state: raise RuntimeError, "no active input()" return _state.isfirstline() def isstdin(): """ Returns true if the last line was read from sys.stdin, --- 156,177 ---- def isfirstline(): """ ! Returns true if the line just read is the first line of its file, otherwise returns false. """ if not _state: raise RuntimeError, "no active input()" return _state.isfirstline() + def islastline(): + """ + Returns true if the line just read is the last line of its file, + otherwise returns false. + """ + if not _state: + raise RuntimeError, "no active input()" + return _state.islastline() + def isstdin(): """ Returns true if the last line was read from sys.stdin, *************** *** 174,181 **** class FileInput: """class FileInput([files[, inplace[, backup]]]) ! Class FileInput is the implementation of the module; its methods ! filename(), lineno(), fileline(), isfirstline(), isstdin(), nextfile() and close() correspond to the functions of the same name in the module. In addition it has a readline() method which returns the next input line, and a __getitem__() method which implements the --- 184,191 ---- class FileInput: """class FileInput([files[, inplace[, backup]]]) ! Class FileInput is the implementation of the module; its methods filename(), ! lineno(), fileline(), isfirstline(), islastline() isstdin(), nextfile() and close() correspond to the functions of the same name in the module. In addition it has a readline() method which returns the next input line, and a __getitem__() method which implements the *************** *** 207,212 **** --- 217,223 ---- self._backupfilename = None self._buffer = [] self._bufindex = 0 + self._lastline = None def __del__(self): self.close() *************** *** 266,271 **** --- 277,283 ---- self._isstdin = False self._buffer = [] self._bufindex = 0 + self._lastline = None def readline(self): try: *************** *** 320,325 **** --- 332,340 ---- self._file = open(self._filename, "r") self._buffer = self._file.readlines(self._bufsize) self._bufindex = 0 + if self._lastline: + self._buffer.insert(0, self._lastline) + self._lastline = self._file.readline() if not self._buffer: self.nextfile() # Recursive call *************** *** 337,342 **** --- 352,360 ---- def isfirstline(self): return self._filelineno == 1 + def islastline(self): + return not self._lastline and len(self._buffer) == self._bufindex + def isstdin(self): return self._isstdin *************** *** 351,358 **** for line in input(args, inplace=inplace, backup=backup): if line[-1:] == '\n': line = line[:-1] if line[-1:] == '\r': line = line[:-1] print "%d: %s[%d]%s %s" % (lineno(), filename(), filelineno(), ! isfirstline() and "*" or "", line) print "%d: %s[%d]" % (lineno(), filename(), filelineno()) if __name__ == '__main__': --- 369,382 ---- for line in input(args, inplace=inplace, backup=backup): if line[-1:] == '\n': line = line[:-1] if line[-1:] == '\r': line = line[:-1] + if isfirstline(): + mark = "*" + elif islastline(): + mark = "%" + else: + mark = "" print "%d: %s[%d]%s %s" % (lineno(), filename(), filelineno(), ! mark, line) print "%d: %s[%d]" % (lineno(), filename(), filelineno()) if __name__ == '__main__': *** /test/test_fileinput.py.old Tue Jul 23 12:03:52 2002 --- /test/test_fileinput.py Fri May 28 17:05:01 2004 *************** *** 48,53 **** --- 48,56 ---- print '%s. Status variables (bs=%s)' % (start+1, bs) fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs) s = "x" + while s and s != 'Line 15 of file 1\n': + s = fi.readline() + verify(fi.islastline()) while s and s != 'Line 6 of file 2\n': s = fi.readline() verify(fi.filename() == t2)