diff options
author | Neil Schemenauer <nascheme@enme.ucalgary.ca> | 2002-03-26 20:28:40 (GMT) |
---|---|---|
committer | Neil Schemenauer <nascheme@enme.ucalgary.ca> | 2002-03-26 20:28:40 (GMT) |
commit | 908632a6a67864aa67fc01b02973d6eb3af1ac6a (patch) | |
tree | 94a23b6f5502c864fd86dee68cb9635b63684df5 /Lib | |
parent | 13e34f7a192d6f262a5bc000a3547c1d5ce59bee (diff) | |
download | cpython-908632a6a67864aa67fc01b02973d6eb3af1ac6a.zip cpython-908632a6a67864aa67fc01b02973d6eb3af1ac6a.tar.gz cpython-908632a6a67864aa67fc01b02973d6eb3af1ac6a.tar.bz2 |
Implement iterator protocol.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/fileinput.py | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/Lib/fileinput.py b/Lib/fileinput.py index c18995b..870322c 100644 --- a/Lib/fileinput.py +++ b/Lib/fileinput.py @@ -166,7 +166,10 @@ class FileInput: self.nextfile() self._files = () - def __getitem__(self, i): + def __iter__(self): + return self + + def next(self): try: line = self._buffer[self._bufindex] except IndexError: @@ -176,12 +179,18 @@ class FileInput: self._lineno += 1 self._filelineno += 1 return line - if i != self._lineno: - raise RuntimeError, "accessing lines out of order" line = self.readline() if not line: - raise IndexError, "end of input reached" + raise StopIteration return line + + def __getitem__(self, i): + if i != self._lineno: + raise RuntimeError, "accessing lines out of order" + try: + return self.next() + except StopIteration: + raise IndexError, "end of input reached" def nextfile(self): savestdout = self._savestdout |