Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 67e9fb9

Browse files
committed
Patch #1215184: fileinput now has a fileno() function for getting the
current file number.
1 parent 602b9ba commit 67e9fb9

4 files changed

Lines changed: 51 additions & 7 deletions

File tree

Doc/lib/libfileinput.tex

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ \section{\module{fileinput} ---
6262
line has been read, returns \code{None}.
6363
\end{funcdesc}
6464

65+
\begin{funcdesc}{fileno}{}
66+
Return the integer ``file descriptor'' for the current file. When no
67+
file is opened (before the first line and between files), returns
68+
\code{-1}.
69+
\end{funcdesc}
70+
6571
\begin{funcdesc}{lineno}{}
6672
Return the cumulative line number of the line that has just been
6773
read. Before the first line has been read, returns \code{0}. After
@@ -107,10 +113,11 @@ \section{\module{fileinput} ---
107113
\begin{classdesc}{FileInput}{\optional{files\optional{,
108114
inplace\optional{, backup}}}}
109115
Class \class{FileInput} is the implementation; its methods
110-
\method{filename()}, \method{lineno()}, \method{fileline()},
111-
\method{isfirstline()}, \method{isstdin()}, \method{nextfile()} and
112-
\method{close()} correspond to the functions of the same name in the
113-
module. In addition it has a \method{readline()} method which
116+
\method{filename()}, \method{fileno()}, \method{lineno()},
117+
\method{fileline()}, \method{isfirstline()}, \method{isstdin()},
118+
\method{nextfile()} and \method{close()} correspond to the functions
119+
of the same name in the module.
120+
In addition it has a \method{readline()} method which
114121
returns the next input line, and a \method{__getitem__()} method
115122
which implements the sequence behavior. The sequence must be
116123
accessed in strictly sequential order; random access and

Lib/fileinput.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373
7474
- optional getopt argument processing
7575
- specify open mode ('r' or 'rb')
76-
- fileno()
7776
- isatty()
7877
- read(), read(size), even readlines()
7978
@@ -153,6 +152,15 @@ def filelineno():
153152
raise RuntimeError, "no active input()"
154153
return _state.filelineno()
155154

155+
def fileno():
156+
"""
157+
Return the file number of the current file. When no file is currently
158+
opened, returns -1.
159+
"""
160+
if not _state:
161+
raise RuntimeError, "no active input()"
162+
return _state.fileno()
163+
156164
def isfirstline():
157165
"""
158166
Returns true the line just read is the first line of its file,
@@ -175,8 +183,9 @@ class FileInput:
175183
"""class FileInput([files[, inplace[, backup]]])
176184
177185
Class FileInput is the implementation of the module; its methods
178-
filename(), lineno(), fileline(), isfirstline(), isstdin(), nextfile()
179-
and close() correspond to the functions of the same name in the module.
186+
filename(), lineno(), fileline(), isfirstline(), isstdin(), fileno(),
187+
nextfile() and close() correspond to the functions of the same name
188+
in the module.
180189
In addition it has a readline() method which returns the next
181190
input line, and a __getitem__() method which implements the
182191
sequence behavior. The sequence must be accessed in strictly
@@ -334,6 +343,15 @@ def lineno(self):
334343
def filelineno(self):
335344
return self._filelineno
336345

346+
def fileno(self):
347+
if self._file:
348+
try:
349+
return self._file.fileno()
350+
except ValueError:
351+
return -1
352+
else:
353+
return -1
354+
337355
def isfirstline(self):
338356
return self._filelineno == 1
339357

Lib/test/test_fileinput.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,19 @@ def writeFiles():
167167
verify(lines == ["A\n", "B"])
168168
finally:
169169
remove_tempfiles(t1)
170+
171+
if verbose:
172+
print "16. fileno()"
173+
try:
174+
t1 = writeTmp(1, ["A\nB"])
175+
t2 = writeTmp(2, ["C\nD"])
176+
fi = FileInput(files=(t1, t2))
177+
verify(fi.fileno() == -1)
178+
line = fi.next()
179+
verify(fi.fileno() != -1)
180+
fi.nextfile()
181+
verify(fi.fileno() == -1)
182+
line = list(fi)
183+
verify(fi.fileno() == -1)
184+
finally:
185+
remove_tempfiles(t1, t2)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,9 @@ Extension Modules
366366
Library
367367
-------
368368

369+
- Patch #1215184: fileinput now has a fileno() function for getting the
370+
current file number.
371+
369372
- Patch #1349274: gettext.install() now optionally installs additional
370373
translation functions other than _() in the builtin namespace.
371374

0 commit comments

Comments
 (0)