Kaydet (Commit) 908632a6 authored tarafından Neil Schemenauer's avatar Neil Schemenauer

Implement iterator protocol.

üst 13e34f7a
...@@ -166,7 +166,10 @@ class FileInput: ...@@ -166,7 +166,10 @@ class FileInput:
self.nextfile() self.nextfile()
self._files = () self._files = ()
def __getitem__(self, i): def __iter__(self):
return self
def next(self):
try: try:
line = self._buffer[self._bufindex] line = self._buffer[self._bufindex]
except IndexError: except IndexError:
...@@ -176,12 +179,18 @@ class FileInput: ...@@ -176,12 +179,18 @@ class FileInput:
self._lineno += 1 self._lineno += 1
self._filelineno += 1 self._filelineno += 1
return line return line
if i != self._lineno:
raise RuntimeError, "accessing lines out of order"
line = self.readline() line = self.readline()
if not line: if not line:
raise IndexError, "end of input reached" raise StopIteration
return line 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): def nextfile(self):
savestdout = self._savestdout savestdout = self._savestdout
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment