summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2006-02-19 13:56:17 (GMT)
committerGeorg Brandl <georg@python.org>2006-02-19 13:56:17 (GMT)
commit67e9fb9d7afbd9935322420a7cadd4cb6538dcdf (patch)
tree2d6905f185cfca51ba3d9462882935c35a01b4fb /Lib
parent602b9ba6b37c4ac4ed445f8c9e9dccd68d631899 (diff)
downloadcpython-67e9fb9d7afbd9935322420a7cadd4cb6538dcdf.zip
cpython-67e9fb9d7afbd9935322420a7cadd4cb6538dcdf.tar.gz
cpython-67e9fb9d7afbd9935322420a7cadd4cb6538dcdf.tar.bz2
Patch #1215184: fileinput now has a fileno() function for getting the
current file number.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/fileinput.py24
-rw-r--r--Lib/test/test_fileinput.py16
2 files changed, 37 insertions, 3 deletions
diff --git a/Lib/fileinput.py b/Lib/fileinput.py
index 5c06627..692eeea 100644
--- a/Lib/fileinput.py
+++ b/Lib/fileinput.py
@@ -73,7 +73,6 @@ XXX Possible additions:
- optional getopt argument processing
- specify open mode ('r' or 'rb')
-- fileno()
- isatty()
- read(), read(size), even readlines()
@@ -153,6 +152,15 @@ def filelineno():
raise RuntimeError, "no active input()"
return _state.filelineno()
+def fileno():
+ """
+ Return the file number of the current file. When no file is currently
+ opened, returns -1.
+ """
+ if not _state:
+ raise RuntimeError, "no active input()"
+ return _state.fileno()
+
def isfirstline():
"""
Returns true the line just read is the first line of its file,
@@ -175,8 +183,9 @@ 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.
+ filename(), lineno(), fileline(), isfirstline(), isstdin(), fileno(),
+ 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
sequence behavior. The sequence must be accessed in strictly
@@ -334,6 +343,15 @@ class FileInput:
def filelineno(self):
return self._filelineno
+ def fileno(self):
+ if self._file:
+ try:
+ return self._file.fileno()
+ except ValueError:
+ return -1
+ else:
+ return -1
+
def isfirstline(self):
return self._filelineno == 1
diff --git a/Lib/test/test_fileinput.py b/Lib/test/test_fileinput.py
index 285573c..be4cb8e 100644
--- a/Lib/test/test_fileinput.py
+++ b/Lib/test/test_fileinput.py
@@ -167,3 +167,19 @@ try:
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)