Skip to content

Commit 1a3faf9

Browse files
Matthias Bussonnierserhiy-storchaka
authored andcommitted
bpo-36952: Remove the bufsize parameter in fileinput.input(). (GH-13400)
This parameter is marked as deprecated since 3.6 and for removal in 3.8. It already had no effects.
1 parent 4011d86 commit 1a3faf9

File tree

5 files changed

+41
-47
lines changed

5 files changed

+41
-47
lines changed

Doc/library/fileinput.rst

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ provided by this module.
5454
The following function is the primary interface of this module:
5555

5656

57-
.. function:: input(files=None, inplace=False, backup='', bufsize=0, mode='r', openhook=None)
57+
.. function:: input(files=None, inplace=False, backup='', *, mode='r', openhook=None)
5858

5959
Create an instance of the :class:`FileInput` class. The instance will be used
6060
as global state for the functions of this module, and is also returned to use
@@ -72,8 +72,9 @@ The following function is the primary interface of this module:
7272
.. versionchanged:: 3.2
7373
Can be used as a context manager.
7474

75-
.. deprecated-removed:: 3.6 3.8
76-
The *bufsize* parameter.
75+
.. versionchanged:: 3.8
76+
The keyword parameters *mode* and *openhook* are now keyword-only.
77+
7778

7879
The following functions use the global state created by :func:`fileinput.input`;
7980
if there is no active state, :exc:`RuntimeError` is raised.
@@ -135,7 +136,7 @@ The class which implements the sequence behavior provided by the module is
135136
available for subclassing as well:
136137

137138

138-
.. class:: FileInput(files=None, inplace=False, backup='', bufsize=0, mode='r', openhook=None)
139+
.. class:: FileInput(files=None, inplace=False, backup='', *, mode='r', openhook=None)
139140

140141
Class :class:`FileInput` is the implementation; its methods :meth:`filename`,
141142
:meth:`fileno`, :meth:`lineno`, :meth:`filelineno`, :meth:`isfirstline`,
@@ -160,18 +161,20 @@ available for subclassing as well:
160161
with FileInput(files=('spam.txt', 'eggs.txt')) as input:
161162
process(input)
162163

164+
163165
.. versionchanged:: 3.2
164166
Can be used as a context manager.
165167

166168
.. deprecated:: 3.4
167169
The ``'rU'`` and ``'U'`` modes.
168170

169-
.. deprecated-removed:: 3.6 3.8
170-
The *bufsize* parameter.
171-
172171
.. deprecated:: 3.8
173172
Support for :meth:`__getitem__` method is deprecated.
174173

174+
.. versionchanged:: 3.8
175+
The keyword parameter *mode* and *openhook* are now keyword-only.
176+
177+
175178

176179
**Optional in-place filtering:** if the keyword argument ``inplace=True`` is
177180
passed to :func:`fileinput.input` or to the :class:`FileInput` constructor, the

Doc/whatsnew/3.8.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,10 @@ The following features and APIs have been removed from Python 3.8:
828828
exposed to the user.
829829
(Contributed by Aviv Palivoda in :issue:`30262`.)
830830

831+
* The ``bufsize`` keyword argument of :func:`fileinput.input` and
832+
:func:`fileinput.FileInput` which was ignored and deprecated since Python 3.6
833+
has been removed. :issue:`36952` (Contributed by Matthias Bussonnier)
834+
831835

832836
Porting to Python 3.8
833837
=====================

Lib/fileinput.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@
8080

8181
_state = None
8282

83-
def input(files=None, inplace=False, backup="", bufsize=0,
84-
mode="r", openhook=None):
83+
def input(files=None, inplace=False, backup="", *, mode="r", openhook=None):
8584
"""Return an instance of the FileInput class, which can be iterated.
8685
8786
The parameters are passed to the constructor of the FileInput class.
@@ -91,7 +90,7 @@ def input(files=None, inplace=False, backup="", bufsize=0,
9190
global _state
9291
if _state and _state._file:
9392
raise RuntimeError("input() already active")
94-
_state = FileInput(files, inplace, backup, bufsize, mode, openhook)
93+
_state = FileInput(files, inplace, backup, mode=mode, openhook=openhook)
9594
return _state
9695

9796
def close():
@@ -173,7 +172,7 @@ def isstdin():
173172
return _state.isstdin()
174173

175174
class FileInput:
176-
"""FileInput([files[, inplace[, backup[, bufsize, [, mode[, openhook]]]]]])
175+
"""FileInput([files[, inplace[, backup]]], *, mode=None, openhook=None)
177176
178177
Class FileInput is the implementation of the module; its methods
179178
filename(), lineno(), fileline(), isfirstline(), isstdin(), fileno(),
@@ -185,7 +184,7 @@ class FileInput:
185184
sequential order; random access and readline() cannot be mixed.
186185
"""
187186

188-
def __init__(self, files=None, inplace=False, backup="", bufsize=0,
187+
def __init__(self, files=None, inplace=False, backup="", *,
189188
mode="r", openhook=None):
190189
if isinstance(files, str):
191190
files = (files,)
@@ -201,10 +200,6 @@ def __init__(self, files=None, inplace=False, backup="", bufsize=0,
201200
self._files = files
202201
self._inplace = inplace
203202
self._backup = backup
204-
if bufsize:
205-
import warnings
206-
warnings.warn('bufsize is deprecated and ignored',
207-
DeprecationWarning, stacklevel=2)
208203
self._savestdout = None
209204
self._output = None
210205
self._filename = None

Lib/test/test_fileinput.py

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,17 @@ def close(self):
8282

8383
class BufferSizesTests(BaseTests, unittest.TestCase):
8484
def test_buffer_sizes(self):
85-
# First, run the tests with default and teeny buffer size.
86-
for round, bs in (0, 0), (1, 30):
87-
t1 = self.writeTmp(''.join("Line %s of file 1\n" % (i+1) for i in range(15)))
88-
t2 = self.writeTmp(''.join("Line %s of file 2\n" % (i+1) for i in range(10)))
89-
t3 = self.writeTmp(''.join("Line %s of file 3\n" % (i+1) for i in range(5)))
90-
t4 = self.writeTmp(''.join("Line %s of file 4\n" % (i+1) for i in range(1)))
91-
if bs:
92-
with self.assertWarns(DeprecationWarning):
93-
self.buffer_size_test(t1, t2, t3, t4, bs, round)
94-
else:
95-
self.buffer_size_test(t1, t2, t3, t4, bs, round)
96-
97-
def buffer_size_test(self, t1, t2, t3, t4, bs=0, round=0):
85+
86+
t1 = self.writeTmp(''.join("Line %s of file 1\n" % (i+1) for i in range(15)))
87+
t2 = self.writeTmp(''.join("Line %s of file 2\n" % (i+1) for i in range(10)))
88+
t3 = self.writeTmp(''.join("Line %s of file 3\n" % (i+1) for i in range(5)))
89+
t4 = self.writeTmp(''.join("Line %s of file 4\n" % (i+1) for i in range(1)))
90+
9891
pat = re.compile(r'LINE (\d+) OF FILE (\d+)')
9992

100-
start = 1 + round*6
10193
if verbose:
102-
print('%s. Simple iteration (bs=%s)' % (start+0, bs))
103-
fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
94+
print('1. Simple iteration')
95+
fi = FileInput(files=(t1, t2, t3, t4))
10496
lines = list(fi)
10597
fi.close()
10698
self.assertEqual(len(lines), 31)
@@ -110,8 +102,8 @@ def buffer_size_test(self, t1, t2, t3, t4, bs=0, round=0):
110102
self.assertEqual(fi.filename(), t4)
111103

112104
if verbose:
113-
print('%s. Status variables (bs=%s)' % (start+1, bs))
114-
fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
105+
print('2. Status variables')
106+
fi = FileInput(files=(t1, t2, t3, t4))
115107
s = "x"
116108
while s and s != 'Line 6 of file 2\n':
117109
s = fi.readline()
@@ -122,15 +114,15 @@ def buffer_size_test(self, t1, t2, t3, t4, bs=0, round=0):
122114
self.assertFalse(fi.isstdin())
123115

124116
if verbose:
125-
print('%s. Nextfile (bs=%s)' % (start+2, bs))
117+
print('3. Nextfile')
126118
fi.nextfile()
127119
self.assertEqual(fi.readline(), 'Line 1 of file 3\n')
128120
self.assertEqual(fi.lineno(), 22)
129121
fi.close()
130122

131123
if verbose:
132-
print('%s. Stdin (bs=%s)' % (start+3, bs))
133-
fi = FileInput(files=(t1, t2, t3, t4, '-'), bufsize=bs)
124+
print('4. Stdin')
125+
fi = FileInput(files=(t1, t2, t3, t4, '-'))
134126
savestdin = sys.stdin
135127
try:
136128
sys.stdin = StringIO("Line 1 of stdin\nLine 2 of stdin\n")
@@ -143,27 +135,27 @@ def buffer_size_test(self, t1, t2, t3, t4, bs=0, round=0):
143135
sys.stdin = savestdin
144136

145137
if verbose:
146-
print('%s. Boundary conditions (bs=%s)' % (start+4, bs))
147-
fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
138+
print('5. Boundary conditions')
139+
fi = FileInput(files=(t1, t2, t3, t4))
148140
self.assertEqual(fi.lineno(), 0)
149141
self.assertEqual(fi.filename(), None)
150142
fi.nextfile()
151143
self.assertEqual(fi.lineno(), 0)
152144
self.assertEqual(fi.filename(), None)
153145

154146
if verbose:
155-
print('%s. Inplace (bs=%s)' % (start+5, bs))
147+
print('6. Inplace')
156148
savestdout = sys.stdout
157149
try:
158-
fi = FileInput(files=(t1, t2, t3, t4), inplace=1, bufsize=bs)
150+
fi = FileInput(files=(t1, t2, t3, t4), inplace=1)
159151
for line in fi:
160152
line = line[:-1].upper()
161153
print(line)
162154
fi.close()
163155
finally:
164156
sys.stdout = savestdout
165157

166-
fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
158+
fi = FileInput(files=(t1, t2, t3, t4))
167159
for line in fi:
168160
self.assertEqual(line[-1], '\n')
169161
m = pat.match(line[:-1])
@@ -533,12 +525,11 @@ def test_pathlib_file_inplace(self):
533525
class MockFileInput:
534526
"""A class that mocks out fileinput.FileInput for use during unit tests"""
535527

536-
def __init__(self, files=None, inplace=False, backup="", bufsize=0,
528+
def __init__(self, files=None, inplace=False, backup="", *,
537529
mode="r", openhook=None):
538530
self.files = files
539531
self.inplace = inplace
540532
self.backup = backup
541-
self.bufsize = bufsize
542533
self.mode = mode
543534
self.openhook = openhook
544535
self._file = None
@@ -641,13 +632,11 @@ def do_test_call_input(self):
641632
files = object()
642633
inplace = object()
643634
backup = object()
644-
bufsize = object()
645635
mode = object()
646636
openhook = object()
647637

648638
# call fileinput.input() with different values for each argument
649639
result = fileinput.input(files=files, inplace=inplace, backup=backup,
650-
bufsize=bufsize,
651640
mode=mode, openhook=openhook)
652641

653642
# ensure fileinput._state was set to the returned object
@@ -658,7 +647,6 @@ def do_test_call_input(self):
658647
self.assertIs(files, result.files, "files")
659648
self.assertIs(inplace, result.inplace, "inplace")
660649
self.assertIs(backup, result.backup, "backup")
661-
self.assertIs(bufsize, result.bufsize, "bufsize")
662650
self.assertIs(mode, result.mode, "mode")
663651
self.assertIs(openhook, result.openhook, "openhook")
664652

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:func:`fileinput.input` and :class:`fileinput.FileInput` **bufsize**
2+
argument has been removed (was deprecated and ignored since Python 3.6),
3+
and as a result the **mode** and **openhook** arguments have been made
4+
keyword-only.

0 commit comments

Comments
 (0)