[Python-checkins] r42493 - in python/trunk: Doc/lib/libfileinput.tex Lib/fileinput.py Lib/test/test_fileinput.py Misc/NEWS

georg.brandl python-checkins at python.org
Sun Feb 19 14:56:19 CET 2006


Author: georg.brandl
Date: Sun Feb 19 14:56:17 2006
New Revision: 42493

Modified:
   python/trunk/Doc/lib/libfileinput.tex
   python/trunk/Lib/fileinput.py
   python/trunk/Lib/test/test_fileinput.py
   python/trunk/Misc/NEWS
Log:
Patch #1215184: fileinput now has a fileno() function for getting the
current file number.


Modified: python/trunk/Doc/lib/libfileinput.tex
==============================================================================
--- python/trunk/Doc/lib/libfileinput.tex	(original)
+++ python/trunk/Doc/lib/libfileinput.tex	Sun Feb 19 14:56:17 2006
@@ -62,6 +62,12 @@
   line has been read, returns \code{None}.
 \end{funcdesc}
 
+\begin{funcdesc}{fileno}{}
+  Return the integer ``file descriptor'' for the current file. When no
+  file is opened (before the first line and between files), returns
+  \code{-1}.
+\end{funcdesc}
+
 \begin{funcdesc}{lineno}{}
   Return the cumulative line number of the line that has just been
   read.  Before the first line has been read, returns \code{0}.  After
@@ -107,10 +113,11 @@
 \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
-  \method{close()} correspond to the functions of the same name in the
-  module.  In addition it has a \method{readline()} method which
+  \method{filename()}, \method{fileno()}, \method{lineno()},
+  \method{fileline()}, \method{isfirstline()}, \method{isstdin()},
+  \method{nextfile()} and \method{close()} correspond to the functions
+  of the same name in the module.
+  In addition it has a \method{readline()} method which
   returns the next input line, and a \method{__getitem__()} method
   which implements the sequence behavior.  The sequence must be
   accessed in strictly sequential order; random access and

Modified: python/trunk/Lib/fileinput.py
==============================================================================
--- python/trunk/Lib/fileinput.py	(original)
+++ python/trunk/Lib/fileinput.py	Sun Feb 19 14:56:17 2006
@@ -73,7 +73,6 @@
 
 - optional getopt argument processing
 - specify open mode ('r' or 'rb')
-- fileno()
 - isatty()
 - read(), read(size), even readlines()
 
@@ -153,6 +152,15 @@
         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([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 @@
     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
 

Modified: python/trunk/Lib/test/test_fileinput.py
==============================================================================
--- python/trunk/Lib/test/test_fileinput.py	(original)
+++ python/trunk/Lib/test/test_fileinput.py	Sun Feb 19 14:56:17 2006
@@ -167,3 +167,19 @@
     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)

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sun Feb 19 14:56:17 2006
@@ -366,6 +366,9 @@
 Library
 -------
 
+- Patch #1215184: fileinput now has a fileno() function for getting the
+  current file number.
+
 - Patch #1349274: gettext.install() now optionally installs additional
   translation functions other than _() in the builtin namespace.
 


More information about the Python-checkins mailing list