Mercurial > cpython-lru_cache-weakref
annotate Lib/fileinput.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 694e2708b4a8 |
| children |
| rev | line source |
|---|---|
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 """Helper class to quickly write a loop over all standard input files. |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 Typical use is: |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 import fileinput |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 for line in fileinput.input(): |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 process(line) |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 This iterates over the lines of all files listed in sys.argv[1:], |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 defaulting to sys.stdin if the list is empty. If a filename is '-' it |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 is also replaced by sys.stdin. To specify an alternative list of |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 filenames, pass it as the argument to input(). A single file name is |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 also allowed. |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 Functions filename(), lineno() return the filename and cumulative line |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 number of the line that has just been read; filelineno() returns its |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 line number in the current file; isfirstline() returns true iff the |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 line just read is the first line of its file; isstdin() returns true |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 iff the line was read from sys.stdin. Function nextfile() closes the |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 current file so that the next iteration will read the first line from |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 the next file (if any); lines not read from the file will not count |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 towards the cumulative line count; the filename is not changed until |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 after the first line of the next file has been read. Function close() |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 closes the sequence. |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 Before any lines have been read, filename() returns None and both line |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 numbers are zero; nextfile() has no effect. After all lines have been |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 read, filename() and the line number functions return the values |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 pertaining to the last line read; nextfile() has no effect. |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 |
|
36595
c6fba456227b
Patch #1212287: fileinput.input() now has a mode parameter for
Georg Brandl <georg@python.org>
parents:
36594
diff
changeset
|
31 All files are opened in text mode by default, you can override this by |
|
c6fba456227b
Patch #1212287: fileinput.input() now has a mode parameter for
Georg Brandl <georg@python.org>
parents:
36594
diff
changeset
|
32 setting the mode parameter to input() or FileInput.__init__(). |
|
81039
7d69d04522e3
Replace IOError with OSError (#16715)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
80932
diff
changeset
|
33 If an I/O error occurs during opening or reading a file, the OSError |
|
36595
c6fba456227b
Patch #1212287: fileinput.input() now has a mode parameter for
Georg Brandl <georg@python.org>
parents:
36594
diff
changeset
|
34 exception is raised. |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 If sys.stdin is used more than once, the second and further use will |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 return no lines, except perhaps for interactive use, or if it has been |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 explicitly reset (e.g. using sys.stdin.seek(0)). |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 Empty files are opened and immediately closed; the only time their |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 presence in the list of filenames is noticeable at all is when the |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 last file opened is empty. |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 It is possible that the last line of a file doesn't end in a newline |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 character; otherwise lines are returned including the trailing |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 newline. |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 Class FileInput is the implementation; its methods filename(), |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 lineno(), fileline(), isfirstline(), isstdin(), nextfile() and close() |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 correspond to the functions in the module. In addition it has a |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 readline() method which returns the next input line, and a |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 __getitem__() method which implements the sequence behavior. The |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 sequence must be accessed in strictly sequential order; sequence |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 access and readline() cannot be mixed. |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 Optional in-place filtering: if the keyword argument inplace=1 is |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 passed to input() or to the FileInput constructor, the file is moved |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 to a backup file and standard output is directed to the input file. |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 This makes it possible to write a filter that rewrites its input file |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 in place. If the keyword argument backup=".<some extension>" is also |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 given, it specifies the extension for the backup file, and the backup |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 file remains around; by default, the extension is ".bak" and it is |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 deleted when the output file is closed. In-place filtering is |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 disabled when standard input is read. XXX The current implementation |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 does not work for MS-DOS 8+3 filesystems. |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 |
|
15746
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
67 Performance: this module is unfortunately one of the slower ways of |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
68 processing large numbers of input lines. Nevertheless, a significant |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
69 speed-up has been obtained by using readlines(bufsize) instead of |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
70 readline(). A new keyword argument, bufsize=N, is present on the |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
71 input() function and the FileInput() class to override the default |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
72 buffer size. |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
73 |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 XXX Possible additions: |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 - optional getopt argument processing |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 - isatty() |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 - read(), read(size), even readlines() |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 """ |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 |
|
23697
c0e8c07ae1d6
Replace obsolete stat module constants with
Walter Dörwald <walter@livinglogic.de>
parents:
23521
diff
changeset
|
82 import sys, os |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 |
|
54169
904a1bd35e00
Use PEP 8 and true booleans.
Georg Brandl <georg@python.org>
parents:
43707
diff
changeset
|
84 __all__ = ["input", "close", "nextfile", "filename", "lineno", "filelineno", |
|
904a1bd35e00
Use PEP 8 and true booleans.
Georg Brandl <georg@python.org>
parents:
43707
diff
changeset
|
85 "isfirstline", "isstdin", "FileInput"] |
| 16072 | 86 |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 _state = None |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 |
|
15746
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
89 DEFAULT_BUFSIZE = 8*1024 |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
90 |
|
54169
904a1bd35e00
Use PEP 8 and true booleans.
Georg Brandl <georg@python.org>
parents:
43707
diff
changeset
|
91 def input(files=None, inplace=False, backup="", bufsize=0, |
|
36596
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
92 mode="r", openhook=None): |
|
84362
0760b58526ba
Issue #18315: Improve fileinput docs by adding 'bufsize' where missing and
Terry Jan Reedy <tjreedy@udel.edu>
parents:
73434
diff
changeset
|
93 """Return an instance of the FileInput class, which can be iterated. |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
94 |
|
84362
0760b58526ba
Issue #18315: Improve fileinput docs by adding 'bufsize' where missing and
Terry Jan Reedy <tjreedy@udel.edu>
parents:
73434
diff
changeset
|
95 The parameters are passed to the constructor of the FileInput class. |
|
0760b58526ba
Issue #18315: Improve fileinput docs by adding 'bufsize' where missing and
Terry Jan Reedy <tjreedy@udel.edu>
parents:
73434
diff
changeset
|
96 The returned instance, in addition to being an iterator, |
|
0760b58526ba
Issue #18315: Improve fileinput docs by adding 'bufsize' where missing and
Terry Jan Reedy <tjreedy@udel.edu>
parents:
73434
diff
changeset
|
97 keeps global state for the functions of this module,. |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
98 """ |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 global _state |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 if _state and _state._file: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42622
diff
changeset
|
101 raise RuntimeError("input() already active") |
|
36596
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
102 _state = FileInput(files, inplace, backup, bufsize, mode, openhook) |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 return _state |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 def close(): |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
106 """Close the sequence.""" |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 global _state |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 state = _state |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 _state = None |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 if state: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
111 state.close() |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 def nextfile(): |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
114 """ |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
115 Close the current file so that the next iteration will read the first |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
116 line from the next file (if any); lines not read from the file will |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
117 not count towards the cumulative line count. The filename is not |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
118 changed until after the first line of the next file has been read. |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
119 Before the first line has been read, this function has no effect; |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
120 it cannot be used to skip the first file. After the last line of the |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
121 last file has been read, this function has no effect. |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
122 """ |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 if not _state: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42622
diff
changeset
|
124 raise RuntimeError("no active input()") |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 return _state.nextfile() |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 def filename(): |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
128 """ |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
129 Return the name of the file currently being read. |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
130 Before the first line has been read, returns None. |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
131 """ |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 if not _state: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42622
diff
changeset
|
133 raise RuntimeError("no active input()") |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 return _state.filename() |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 def lineno(): |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
137 """ |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
138 Return the cumulative line number of the line that has just been read. |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
139 Before the first line has been read, returns 0. After the last line |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
140 of the last file has been read, returns the line number of that line. |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
141 """ |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 if not _state: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42622
diff
changeset
|
143 raise RuntimeError("no active input()") |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 return _state.lineno() |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 def filelineno(): |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
147 """ |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
148 Return the line number in the current file. Before the first line |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
149 has been read, returns 0. After the last line of the last file has |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
150 been read, returns the line number of that line within the file. |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
151 """ |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 if not _state: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42622
diff
changeset
|
153 raise RuntimeError("no active input()") |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 return _state.filelineno() |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 |
|
36594
781654be55ef
Patch #1215184: fileinput now has a fileno() function for getting the
Georg Brandl <georg@python.org>
parents:
36590
diff
changeset
|
156 def fileno(): |
|
781654be55ef
Patch #1215184: fileinput now has a fileno() function for getting the
Georg Brandl <georg@python.org>
parents:
36590
diff
changeset
|
157 """ |
|
781654be55ef
Patch #1215184: fileinput now has a fileno() function for getting the
Georg Brandl <georg@python.org>
parents:
36590
diff
changeset
|
158 Return the file number of the current file. When no file is currently |
|
781654be55ef
Patch #1215184: fileinput now has a fileno() function for getting the
Georg Brandl <georg@python.org>
parents:
36590
diff
changeset
|
159 opened, returns -1. |
|
781654be55ef
Patch #1215184: fileinput now has a fileno() function for getting the
Georg Brandl <georg@python.org>
parents:
36590
diff
changeset
|
160 """ |
|
781654be55ef
Patch #1215184: fileinput now has a fileno() function for getting the
Georg Brandl <georg@python.org>
parents:
36590
diff
changeset
|
161 if not _state: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42622
diff
changeset
|
162 raise RuntimeError("no active input()") |
|
36594
781654be55ef
Patch #1215184: fileinput now has a fileno() function for getting the
Georg Brandl <georg@python.org>
parents:
36590
diff
changeset
|
163 return _state.fileno() |
|
781654be55ef
Patch #1215184: fileinput now has a fileno() function for getting the
Georg Brandl <georg@python.org>
parents:
36590
diff
changeset
|
164 |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 def isfirstline(): |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
166 """ |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
167 Returns true the line just read is the first line of its file, |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
168 otherwise returns false. |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
169 """ |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 if not _state: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42622
diff
changeset
|
171 raise RuntimeError("no active input()") |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 return _state.isfirstline() |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 def isstdin(): |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
175 """ |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
176 Returns true if the last line was read from sys.stdin, |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
177 otherwise returns false. |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
178 """ |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 if not _state: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42622
diff
changeset
|
180 raise RuntimeError("no active input()") |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 return _state.isstdin() |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 class FileInput: |
|
84362
0760b58526ba
Issue #18315: Improve fileinput docs by adding 'bufsize' where missing and
Terry Jan Reedy <tjreedy@udel.edu>
parents:
73434
diff
changeset
|
184 """FileInput([files[, inplace[, backup[, bufsize, [, mode[, openhook]]]]]]) |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
185 |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
186 Class FileInput is the implementation of the module; its methods |
|
36594
781654be55ef
Patch #1215184: fileinput now has a fileno() function for getting the
Georg Brandl <georg@python.org>
parents:
36590
diff
changeset
|
187 filename(), lineno(), fileline(), isfirstline(), isstdin(), fileno(), |
|
781654be55ef
Patch #1215184: fileinput now has a fileno() function for getting the
Georg Brandl <georg@python.org>
parents:
36590
diff
changeset
|
188 nextfile() and close() correspond to the functions of the same name |
|
781654be55ef
Patch #1215184: fileinput now has a fileno() function for getting the
Georg Brandl <georg@python.org>
parents:
36590
diff
changeset
|
189 in the module. |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
190 In addition it has a readline() method which returns the next |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
191 input line, and a __getitem__() method which implements the |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
192 sequence behavior. The sequence must be accessed in strictly |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
193 sequential order; random access and readline() cannot be mixed. |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
194 """ |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 |
|
54169
904a1bd35e00
Use PEP 8 and true booleans.
Georg Brandl <georg@python.org>
parents:
43707
diff
changeset
|
196 def __init__(self, files=None, inplace=False, backup="", bufsize=0, |
|
36596
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
197 mode="r", openhook=None): |
|
43577
ace45b361e87
Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents:
43020
diff
changeset
|
198 if isinstance(files, str): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
199 files = (files,) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
200 else: |
|
12340
e2620aab9f2d
Implement suggestion from Lawrence Kesteloot in PR#280, to change the
Guido van Rossum <guido@python.org>
parents:
11387
diff
changeset
|
201 if files is None: |
|
e2620aab9f2d
Implement suggestion from Lawrence Kesteloot in PR#280, to change the
Guido van Rossum <guido@python.org>
parents:
11387
diff
changeset
|
202 files = sys.argv[1:] |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
203 if not files: |
|
12340
e2620aab9f2d
Implement suggestion from Lawrence Kesteloot in PR#280, to change the
Guido van Rossum <guido@python.org>
parents:
11387
diff
changeset
|
204 files = ('-',) |
|
e2620aab9f2d
Implement suggestion from Lawrence Kesteloot in PR#280, to change the
Guido van Rossum <guido@python.org>
parents:
11387
diff
changeset
|
205 else: |
|
e2620aab9f2d
Implement suggestion from Lawrence Kesteloot in PR#280, to change the
Guido van Rossum <guido@python.org>
parents:
11387
diff
changeset
|
206 files = tuple(files) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
207 self._files = files |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
208 self._inplace = inplace |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
209 self._backup = backup |
|
15746
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
210 self._bufsize = bufsize or DEFAULT_BUFSIZE |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
211 self._savestdout = None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
212 self._output = None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
213 self._filename = None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
214 self._lineno = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
215 self._filelineno = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
216 self._file = None |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22595
diff
changeset
|
217 self._isstdin = False |
|
8687
fd49ed65cd20
Add missing initializer for self._backupfilename. Due to Ralph Butler.
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
218 self._backupfilename = None |
|
15746
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
219 self._buffer = [] |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
220 self._bufindex = 0 |
|
36595
c6fba456227b
Patch #1212287: fileinput.input() now has a mode parameter for
Georg Brandl <georg@python.org>
parents:
36594
diff
changeset
|
221 # restrict mode argument to reading modes |
|
c6fba456227b
Patch #1212287: fileinput.input() now has a mode parameter for
Georg Brandl <georg@python.org>
parents:
36594
diff
changeset
|
222 if mode not in ('r', 'rU', 'U', 'rb'): |
|
c6fba456227b
Patch #1212287: fileinput.input() now has a mode parameter for
Georg Brandl <georg@python.org>
parents:
36594
diff
changeset
|
223 raise ValueError("FileInput opening mode must be one of " |
|
c6fba456227b
Patch #1212287: fileinput.input() now has a mode parameter for
Georg Brandl <georg@python.org>
parents:
36594
diff
changeset
|
224 "'r', 'rU', 'U' and 'rb'") |
|
87448
70bd6f7e013b
Issue #15204: Deprecated the 'U' mode in file-like objects.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84363
diff
changeset
|
225 if 'U' in mode: |
|
70bd6f7e013b
Issue #15204: Deprecated the 'U' mode in file-like objects.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84363
diff
changeset
|
226 import warnings |
|
87520
694e2708b4a8
Issue #15204: Silence and check the 'U' mode deprecation warnings in tests.
Serhiy Storchaka <storchaka@gmail.com>
parents:
87448
diff
changeset
|
227 warnings.warn("'U' mode is deprecated", |
|
87448
70bd6f7e013b
Issue #15204: Deprecated the 'U' mode in file-like objects.
Serhiy Storchaka <storchaka@gmail.com>
parents:
84363
diff
changeset
|
228 DeprecationWarning, 2) |
|
36595
c6fba456227b
Patch #1212287: fileinput.input() now has a mode parameter for
Georg Brandl <georg@python.org>
parents:
36594
diff
changeset
|
229 self._mode = mode |
|
73165
8e57b5d8f58f
Closes #13258: Use callable() built-in in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
63379
diff
changeset
|
230 if openhook: |
|
8e57b5d8f58f
Closes #13258: Use callable() built-in in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
63379
diff
changeset
|
231 if inplace: |
|
8e57b5d8f58f
Closes #13258: Use callable() built-in in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
63379
diff
changeset
|
232 raise ValueError("FileInput cannot use an opening hook in inplace mode") |
|
8e57b5d8f58f
Closes #13258: Use callable() built-in in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
63379
diff
changeset
|
233 if not callable(openhook): |
|
8e57b5d8f58f
Closes #13258: Use callable() built-in in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
63379
diff
changeset
|
234 raise ValueError("FileInput openhook must be callable") |
|
36596
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
235 self._openhook = openhook |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 def __del__(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
238 self.close() |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 def close(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
241 self.nextfile() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
242 self._files = () |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 |
|
63379
ab89d0f52a7f
#1286: allow using fileinput.FileInput as context manager.
Georg Brandl <georg@python.org>
parents:
54169
diff
changeset
|
244 def __enter__(self): |
|
ab89d0f52a7f
#1286: allow using fileinput.FileInput as context manager.
Georg Brandl <georg@python.org>
parents:
54169
diff
changeset
|
245 return self |
|
ab89d0f52a7f
#1286: allow using fileinput.FileInput as context manager.
Georg Brandl <georg@python.org>
parents:
54169
diff
changeset
|
246 |
|
ab89d0f52a7f
#1286: allow using fileinput.FileInput as context manager.
Georg Brandl <georg@python.org>
parents:
54169
diff
changeset
|
247 def __exit__(self, type, value, traceback): |
|
ab89d0f52a7f
#1286: allow using fileinput.FileInput as context manager.
Georg Brandl <georg@python.org>
parents:
54169
diff
changeset
|
248 self.close() |
|
ab89d0f52a7f
#1286: allow using fileinput.FileInput as context manager.
Georg Brandl <georg@python.org>
parents:
54169
diff
changeset
|
249 |
|
22595
204d46fc54f6
Implement iterator protocol.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
20654
diff
changeset
|
250 def __iter__(self): |
|
204d46fc54f6
Implement iterator protocol.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
20654
diff
changeset
|
251 return self |
|
204d46fc54f6
Implement iterator protocol.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
20654
diff
changeset
|
252 |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40775
diff
changeset
|
253 def __next__(self): |
|
15746
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
254 try: |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
255 line = self._buffer[self._bufindex] |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
256 except IndexError: |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
257 pass |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
258 else: |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
259 self._bufindex += 1 |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
260 self._lineno += 1 |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
261 self._filelineno += 1 |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
262 return line |
|
22595
204d46fc54f6
Implement iterator protocol.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
20654
diff
changeset
|
263 line = self.readline() |
|
204d46fc54f6
Implement iterator protocol.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
20654
diff
changeset
|
264 if not line: |
|
204d46fc54f6
Implement iterator protocol.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
20654
diff
changeset
|
265 raise StopIteration |
|
204d46fc54f6
Implement iterator protocol.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
20654
diff
changeset
|
266 return line |
|
23075
b7e87732f6ab
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
22867
diff
changeset
|
267 |
|
22595
204d46fc54f6
Implement iterator protocol.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
20654
diff
changeset
|
268 def __getitem__(self, i): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
269 if i != self._lineno: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42622
diff
changeset
|
270 raise RuntimeError("accessing lines out of order") |
|
22595
204d46fc54f6
Implement iterator protocol.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
20654
diff
changeset
|
271 try: |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40775
diff
changeset
|
272 return self.__next__() |
|
22595
204d46fc54f6
Implement iterator protocol.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
20654
diff
changeset
|
273 except StopIteration: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42622
diff
changeset
|
274 raise IndexError("end of input reached") |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 def nextfile(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
277 savestdout = self._savestdout |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
278 self._savestdout = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
279 if savestdout: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
280 sys.stdout = savestdout |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
281 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
282 output = self._output |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
283 self._output = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
284 if output: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
285 output.close() |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
287 file = self._file |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
288 self._file = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
289 if file and not self._isstdin: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
290 file.close() |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
291 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
292 backupfilename = self._backupfilename |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
293 self._backupfilename = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
294 if backupfilename and not self._backup: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
295 try: os.unlink(backupfilename) |
|
24807
685cd52c4c43
tightening up a few except: clauses
Skip Montanaro <skip@pobox.com>
parents:
23697
diff
changeset
|
296 except OSError: pass |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22595
diff
changeset
|
298 self._isstdin = False |
|
15746
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
299 self._buffer = [] |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
300 self._bufindex = 0 |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 def readline(self): |
|
15746
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
303 try: |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
304 line = self._buffer[self._bufindex] |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
305 except IndexError: |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
306 pass |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
307 else: |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
308 self._bufindex += 1 |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
309 self._lineno += 1 |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
310 self._filelineno += 1 |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
311 return line |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
312 if not self._file: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
313 if not self._files: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
314 return "" |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
315 self._filename = self._files[0] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
316 self._files = self._files[1:] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
317 self._filelineno = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
318 self._file = None |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22595
diff
changeset
|
319 self._isstdin = False |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
320 self._backupfilename = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
321 if self._filename == '-': |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
322 self._filename = '<stdin>' |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
323 self._file = sys.stdin |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22595
diff
changeset
|
324 self._isstdin = True |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
325 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
326 if self._inplace: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
327 self._backupfilename = ( |
| 42622 | 328 self._filename + (self._backup or ".bak")) |
|
80932
47f98a550d42
Issue #16706: get rid of os.error
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
73434
diff
changeset
|
329 try: |
|
47f98a550d42
Issue #16706: get rid of os.error
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
73434
diff
changeset
|
330 os.unlink(self._backupfilename) |
|
47f98a550d42
Issue #16706: get rid of os.error
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
73434
diff
changeset
|
331 except OSError: |
|
47f98a550d42
Issue #16706: get rid of os.error
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
73434
diff
changeset
|
332 pass |
|
81039
7d69d04522e3
Replace IOError with OSError (#16715)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
80932
diff
changeset
|
333 # The next few lines may raise OSError |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
334 os.rename(self._filename, self._backupfilename) |
|
36595
c6fba456227b
Patch #1212287: fileinput.input() now has a mode parameter for
Georg Brandl <georg@python.org>
parents:
36594
diff
changeset
|
335 self._file = open(self._backupfilename, self._mode) |
|
11387
019cd21c8c9f
Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents:
8687
diff
changeset
|
336 try: |
|
23697
c0e8c07ae1d6
Replace obsolete stat module constants with
Walter Dörwald <walter@livinglogic.de>
parents:
23521
diff
changeset
|
337 perm = os.fstat(self._file.fileno()).st_mode |
|
24807
685cd52c4c43
tightening up a few except: clauses
Skip Montanaro <skip@pobox.com>
parents:
23697
diff
changeset
|
338 except OSError: |
|
11387
019cd21c8c9f
Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents:
8687
diff
changeset
|
339 self._output = open(self._filename, "w") |
|
019cd21c8c9f
Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents:
8687
diff
changeset
|
340 else: |
|
43707
d8f7442b8cae
Patch 1341 by Amaury Forgeot d'Arc.
Guido van Rossum <guido@python.org>
parents:
43577
diff
changeset
|
341 mode = os.O_CREAT | os.O_WRONLY | os.O_TRUNC |
|
d8f7442b8cae
Patch 1341 by Amaury Forgeot d'Arc.
Guido van Rossum <guido@python.org>
parents:
43577
diff
changeset
|
342 if hasattr(os, 'O_BINARY'): |
|
d8f7442b8cae
Patch 1341 by Amaury Forgeot d'Arc.
Guido van Rossum <guido@python.org>
parents:
43577
diff
changeset
|
343 mode |= os.O_BINARY |
|
d8f7442b8cae
Patch 1341 by Amaury Forgeot d'Arc.
Guido van Rossum <guido@python.org>
parents:
43577
diff
changeset
|
344 |
|
d8f7442b8cae
Patch 1341 by Amaury Forgeot d'Arc.
Guido van Rossum <guido@python.org>
parents:
43577
diff
changeset
|
345 fd = os.open(self._filename, mode, perm) |
|
11387
019cd21c8c9f
Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents:
8687
diff
changeset
|
346 self._output = os.fdopen(fd, "w") |
|
019cd21c8c9f
Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents:
8687
diff
changeset
|
347 try: |
|
27166
0cadc29180e6
Test that chmod() actually exists before calling it (it doesn't on MacOS9).
Jack Jansen <jack.jansen@cwi.nl>
parents:
24807
diff
changeset
|
348 if hasattr(os, 'chmod'): |
|
0cadc29180e6
Test that chmod() actually exists before calling it (it doesn't on MacOS9).
Jack Jansen <jack.jansen@cwi.nl>
parents:
24807
diff
changeset
|
349 os.chmod(self._filename, perm) |
|
24807
685cd52c4c43
tightening up a few except: clauses
Skip Montanaro <skip@pobox.com>
parents:
23697
diff
changeset
|
350 except OSError: |
|
11387
019cd21c8c9f
Fix for PR#111: when using the inplace option, give the new file the
Guido van Rossum <guido@python.org>
parents:
8687
diff
changeset
|
351 pass |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
352 self._savestdout = sys.stdout |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
353 sys.stdout = self._output |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
354 else: |
|
81039
7d69d04522e3
Replace IOError with OSError (#16715)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
80932
diff
changeset
|
355 # This may raise OSError |
|
36596
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
356 if self._openhook: |
|
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
357 self._file = self._openhook(self._filename, self._mode) |
|
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
358 else: |
|
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
359 self._file = open(self._filename, self._mode) |
|
15746
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
360 self._buffer = self._file.readlines(self._bufsize) |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
361 self._bufindex = 0 |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
362 if not self._buffer: |
|
4dbbf322a9df
Speed it up by using readlines(sizehint). It's still slower than
Guido van Rossum <guido@python.org>
parents:
12340
diff
changeset
|
363 self.nextfile() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
364 # Recursive call |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
365 return self.readline() |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
366 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
367 def filename(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
368 return self._filename |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
369 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
370 def lineno(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
371 return self._lineno |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
372 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
373 def filelineno(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
374 return self._filelineno |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
375 |
|
36594
781654be55ef
Patch #1215184: fileinput now has a fileno() function for getting the
Georg Brandl <georg@python.org>
parents:
36590
diff
changeset
|
376 def fileno(self): |
|
781654be55ef
Patch #1215184: fileinput now has a fileno() function for getting the
Georg Brandl <georg@python.org>
parents:
36590
diff
changeset
|
377 if self._file: |
|
781654be55ef
Patch #1215184: fileinput now has a fileno() function for getting the
Georg Brandl <georg@python.org>
parents:
36590
diff
changeset
|
378 try: |
|
781654be55ef
Patch #1215184: fileinput now has a fileno() function for getting the
Georg Brandl <georg@python.org>
parents:
36590
diff
changeset
|
379 return self._file.fileno() |
|
781654be55ef
Patch #1215184: fileinput now has a fileno() function for getting the
Georg Brandl <georg@python.org>
parents:
36590
diff
changeset
|
380 except ValueError: |
|
781654be55ef
Patch #1215184: fileinput now has a fileno() function for getting the
Georg Brandl <georg@python.org>
parents:
36590
diff
changeset
|
381 return -1 |
|
781654be55ef
Patch #1215184: fileinput now has a fileno() function for getting the
Georg Brandl <georg@python.org>
parents:
36590
diff
changeset
|
382 else: |
|
781654be55ef
Patch #1215184: fileinput now has a fileno() function for getting the
Georg Brandl <georg@python.org>
parents:
36590
diff
changeset
|
383 return -1 |
|
781654be55ef
Patch #1215184: fileinput now has a fileno() function for getting the
Georg Brandl <georg@python.org>
parents:
36590
diff
changeset
|
384 |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
385 def isfirstline(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
386 return self._filelineno == 1 |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
387 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
388 def isstdin(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
389 return self._isstdin |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
390 |
|
36596
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
391 |
|
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
392 def hook_compressed(filename, mode): |
|
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
393 ext = os.path.splitext(filename)[1] |
|
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
394 if ext == '.gz': |
|
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
395 import gzip |
|
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
396 return gzip.open(filename, mode) |
|
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
397 elif ext == '.bz2': |
|
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
398 import bz2 |
|
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
399 return bz2.BZ2File(filename, mode) |
|
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
400 else: |
|
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
401 return open(filename, mode) |
|
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
402 |
|
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
403 |
|
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
404 def hook_encoded(encoding): |
|
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
405 def openhook(filename, mode): |
|
73434
c2b3e8b437a4
fileinput: replace this last occurence of codecs.open with builtins.open.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
73165
diff
changeset
|
406 return open(filename, mode, encoding=encoding) |
|
36596
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
407 return openhook |
|
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
408 |
|
e57377f0a167
Patch #1215184: FileInput now can be given an opening hook which can
Georg Brandl <georg@python.org>
parents:
36595
diff
changeset
|
409 |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
410 def _test(): |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
411 import getopt |
|
54169
904a1bd35e00
Use PEP 8 and true booleans.
Georg Brandl <georg@python.org>
parents:
43707
diff
changeset
|
412 inplace = False |
|
904a1bd35e00
Use PEP 8 and true booleans.
Georg Brandl <georg@python.org>
parents:
43707
diff
changeset
|
413 backup = False |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
414 opts, args = getopt.getopt(sys.argv[1:], "ib:") |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
415 for o, a in opts: |
|
54169
904a1bd35e00
Use PEP 8 and true booleans.
Georg Brandl <georg@python.org>
parents:
43707
diff
changeset
|
416 if o == '-i': inplace = True |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
417 if o == '-b': backup = a |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
418 for line in input(args, inplace=inplace, backup=backup): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
419 if line[-1:] == '\n': line = line[:-1] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6620
diff
changeset
|
420 if line[-1:] == '\r': line = line[:-1] |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
36608
diff
changeset
|
421 print("%d: %s[%d]%s %s" % (lineno(), filename(), filelineno(), |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
36608
diff
changeset
|
422 isfirstline() and "*" or "", line)) |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
36608
diff
changeset
|
423 print("%d: %s[%d]" % (lineno(), filename(), filelineno())) |
|
6620
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
424 |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
425 if __name__ == '__main__': |
|
00b1b77c4177
A new standard module, as discussed on comp.lang.python, to simplify
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
426 _test() |
