Commit 1a3faf9d authored by Matthias Bussonnier's avatar Matthias Bussonnier Committed by Serhiy Storchaka

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.
parent 4011d865
...@@ -54,7 +54,7 @@ provided by this module. ...@@ -54,7 +54,7 @@ provided by this module.
The following function is the primary interface of this module: The following function is the primary interface of this module:
.. function:: input(files=None, inplace=False, backup='', bufsize=0, mode='r', openhook=None) .. function:: input(files=None, inplace=False, backup='', *, mode='r', openhook=None)
Create an instance of the :class:`FileInput` class. The instance will be used Create an instance of the :class:`FileInput` class. The instance will be used
as global state for the functions of this module, and is also returned to use 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: ...@@ -72,8 +72,9 @@ The following function is the primary interface of this module:
.. versionchanged:: 3.2 .. versionchanged:: 3.2
Can be used as a context manager. Can be used as a context manager.
.. deprecated-removed:: 3.6 3.8 .. versionchanged:: 3.8
The *bufsize* parameter. The keyword parameters *mode* and *openhook* are now keyword-only.
The following functions use the global state created by :func:`fileinput.input`; The following functions use the global state created by :func:`fileinput.input`;
if there is no active state, :exc:`RuntimeError` is raised. 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 ...@@ -135,7 +136,7 @@ The class which implements the sequence behavior provided by the module is
available for subclassing as well: available for subclassing as well:
.. class:: FileInput(files=None, inplace=False, backup='', bufsize=0, mode='r', openhook=None) .. class:: FileInput(files=None, inplace=False, backup='', *, mode='r', openhook=None)
Class :class:`FileInput` is the implementation; its methods :meth:`filename`, Class :class:`FileInput` is the implementation; its methods :meth:`filename`,
:meth:`fileno`, :meth:`lineno`, :meth:`filelineno`, :meth:`isfirstline`, :meth:`fileno`, :meth:`lineno`, :meth:`filelineno`, :meth:`isfirstline`,
...@@ -160,18 +161,20 @@ available for subclassing as well: ...@@ -160,18 +161,20 @@ available for subclassing as well:
with FileInput(files=('spam.txt', 'eggs.txt')) as input: with FileInput(files=('spam.txt', 'eggs.txt')) as input:
process(input) process(input)
.. versionchanged:: 3.2 .. versionchanged:: 3.2
Can be used as a context manager. Can be used as a context manager.
.. deprecated:: 3.4 .. deprecated:: 3.4
The ``'rU'`` and ``'U'`` modes. The ``'rU'`` and ``'U'`` modes.
.. deprecated-removed:: 3.6 3.8
The *bufsize* parameter.
.. deprecated:: 3.8 .. deprecated:: 3.8
Support for :meth:`__getitem__` method is deprecated. Support for :meth:`__getitem__` method is deprecated.
.. versionchanged:: 3.8
The keyword parameter *mode* and *openhook* are now keyword-only.
**Optional in-place filtering:** if the keyword argument ``inplace=True`` is **Optional in-place filtering:** if the keyword argument ``inplace=True`` is
passed to :func:`fileinput.input` or to the :class:`FileInput` constructor, the passed to :func:`fileinput.input` or to the :class:`FileInput` constructor, the
......
...@@ -828,6 +828,10 @@ The following features and APIs have been removed from Python 3.8: ...@@ -828,6 +828,10 @@ The following features and APIs have been removed from Python 3.8:
exposed to the user. exposed to the user.
(Contributed by Aviv Palivoda in :issue:`30262`.) (Contributed by Aviv Palivoda in :issue:`30262`.)
* The ``bufsize`` keyword argument of :func:`fileinput.input` and
:func:`fileinput.FileInput` which was ignored and deprecated since Python 3.6
has been removed. :issue:`36952` (Contributed by Matthias Bussonnier)
Porting to Python 3.8 Porting to Python 3.8
===================== =====================
......
...@@ -80,8 +80,7 @@ __all__ = ["input", "close", "nextfile", "filename", "lineno", "filelineno", ...@@ -80,8 +80,7 @@ __all__ = ["input", "close", "nextfile", "filename", "lineno", "filelineno",
_state = None _state = None
def input(files=None, inplace=False, backup="", bufsize=0, def input(files=None, inplace=False, backup="", *, mode="r", openhook=None):
mode="r", openhook=None):
"""Return an instance of the FileInput class, which can be iterated. """Return an instance of the FileInput class, which can be iterated.
The parameters are passed to the constructor of the FileInput class. The parameters are passed to the constructor of the FileInput class.
...@@ -91,7 +90,7 @@ def input(files=None, inplace=False, backup="", bufsize=0, ...@@ -91,7 +90,7 @@ def input(files=None, inplace=False, backup="", bufsize=0,
global _state global _state
if _state and _state._file: if _state and _state._file:
raise RuntimeError("input() already active") raise RuntimeError("input() already active")
_state = FileInput(files, inplace, backup, bufsize, mode, openhook) _state = FileInput(files, inplace, backup, mode=mode, openhook=openhook)
return _state return _state
def close(): def close():
...@@ -173,7 +172,7 @@ def isstdin(): ...@@ -173,7 +172,7 @@ def isstdin():
return _state.isstdin() return _state.isstdin()
class FileInput: class FileInput:
"""FileInput([files[, inplace[, backup[, bufsize, [, mode[, openhook]]]]]]) """FileInput([files[, inplace[, backup]]], *, mode=None, openhook=None)
Class FileInput is the implementation of the module; its methods Class FileInput is the implementation of the module; its methods
filename(), lineno(), fileline(), isfirstline(), isstdin(), fileno(), filename(), lineno(), fileline(), isfirstline(), isstdin(), fileno(),
...@@ -185,7 +184,7 @@ class FileInput: ...@@ -185,7 +184,7 @@ class FileInput:
sequential order; random access and readline() cannot be mixed. sequential order; random access and readline() cannot be mixed.
""" """
def __init__(self, files=None, inplace=False, backup="", bufsize=0, def __init__(self, files=None, inplace=False, backup="", *,
mode="r", openhook=None): mode="r", openhook=None):
if isinstance(files, str): if isinstance(files, str):
files = (files,) files = (files,)
...@@ -201,10 +200,6 @@ class FileInput: ...@@ -201,10 +200,6 @@ class FileInput:
self._files = files self._files = files
self._inplace = inplace self._inplace = inplace
self._backup = backup self._backup = backup
if bufsize:
import warnings
warnings.warn('bufsize is deprecated and ignored',
DeprecationWarning, stacklevel=2)
self._savestdout = None self._savestdout = None
self._output = None self._output = None
self._filename = None self._filename = None
......
...@@ -82,25 +82,17 @@ class LineReader: ...@@ -82,25 +82,17 @@ class LineReader:
class BufferSizesTests(BaseTests, unittest.TestCase): class BufferSizesTests(BaseTests, unittest.TestCase):
def test_buffer_sizes(self): def test_buffer_sizes(self):
# First, run the tests with default and teeny buffer size.
for round, bs in (0, 0), (1, 30):
t1 = self.writeTmp(''.join("Line %s of file 1\n" % (i+1) for i in range(15))) t1 = self.writeTmp(''.join("Line %s of file 1\n" % (i+1) for i in range(15)))
t2 = self.writeTmp(''.join("Line %s of file 2\n" % (i+1) for i in range(10))) t2 = self.writeTmp(''.join("Line %s of file 2\n" % (i+1) for i in range(10)))
t3 = self.writeTmp(''.join("Line %s of file 3\n" % (i+1) for i in range(5))) t3 = self.writeTmp(''.join("Line %s of file 3\n" % (i+1) for i in range(5)))
t4 = self.writeTmp(''.join("Line %s of file 4\n" % (i+1) for i in range(1))) t4 = self.writeTmp(''.join("Line %s of file 4\n" % (i+1) for i in range(1)))
if bs:
with self.assertWarns(DeprecationWarning):
self.buffer_size_test(t1, t2, t3, t4, bs, round)
else:
self.buffer_size_test(t1, t2, t3, t4, bs, round)
def buffer_size_test(self, t1, t2, t3, t4, bs=0, round=0):
pat = re.compile(r'LINE (\d+) OF FILE (\d+)') pat = re.compile(r'LINE (\d+) OF FILE (\d+)')
start = 1 + round*6
if verbose: if verbose:
print('%s. Simple iteration (bs=%s)' % (start+0, bs)) print('1. Simple iteration')
fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs) fi = FileInput(files=(t1, t2, t3, t4))
lines = list(fi) lines = list(fi)
fi.close() fi.close()
self.assertEqual(len(lines), 31) self.assertEqual(len(lines), 31)
...@@ -110,8 +102,8 @@ class BufferSizesTests(BaseTests, unittest.TestCase): ...@@ -110,8 +102,8 @@ class BufferSizesTests(BaseTests, unittest.TestCase):
self.assertEqual(fi.filename(), t4) self.assertEqual(fi.filename(), t4)
if verbose: if verbose:
print('%s. Status variables (bs=%s)' % (start+1, bs)) print('2. Status variables')
fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs) fi = FileInput(files=(t1, t2, t3, t4))
s = "x" s = "x"
while s and s != 'Line 6 of file 2\n': while s and s != 'Line 6 of file 2\n':
s = fi.readline() s = fi.readline()
...@@ -122,15 +114,15 @@ class BufferSizesTests(BaseTests, unittest.TestCase): ...@@ -122,15 +114,15 @@ class BufferSizesTests(BaseTests, unittest.TestCase):
self.assertFalse(fi.isstdin()) self.assertFalse(fi.isstdin())
if verbose: if verbose:
print('%s. Nextfile (bs=%s)' % (start+2, bs)) print('3. Nextfile')
fi.nextfile() fi.nextfile()
self.assertEqual(fi.readline(), 'Line 1 of file 3\n') self.assertEqual(fi.readline(), 'Line 1 of file 3\n')
self.assertEqual(fi.lineno(), 22) self.assertEqual(fi.lineno(), 22)
fi.close() fi.close()
if verbose: if verbose:
print('%s. Stdin (bs=%s)' % (start+3, bs)) print('4. Stdin')
fi = FileInput(files=(t1, t2, t3, t4, '-'), bufsize=bs) fi = FileInput(files=(t1, t2, t3, t4, '-'))
savestdin = sys.stdin savestdin = sys.stdin
try: try:
sys.stdin = StringIO("Line 1 of stdin\nLine 2 of stdin\n") sys.stdin = StringIO("Line 1 of stdin\nLine 2 of stdin\n")
...@@ -143,8 +135,8 @@ class BufferSizesTests(BaseTests, unittest.TestCase): ...@@ -143,8 +135,8 @@ class BufferSizesTests(BaseTests, unittest.TestCase):
sys.stdin = savestdin sys.stdin = savestdin
if verbose: if verbose:
print('%s. Boundary conditions (bs=%s)' % (start+4, bs)) print('5. Boundary conditions')
fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs) fi = FileInput(files=(t1, t2, t3, t4))
self.assertEqual(fi.lineno(), 0) self.assertEqual(fi.lineno(), 0)
self.assertEqual(fi.filename(), None) self.assertEqual(fi.filename(), None)
fi.nextfile() fi.nextfile()
...@@ -152,10 +144,10 @@ class BufferSizesTests(BaseTests, unittest.TestCase): ...@@ -152,10 +144,10 @@ class BufferSizesTests(BaseTests, unittest.TestCase):
self.assertEqual(fi.filename(), None) self.assertEqual(fi.filename(), None)
if verbose: if verbose:
print('%s. Inplace (bs=%s)' % (start+5, bs)) print('6. Inplace')
savestdout = sys.stdout savestdout = sys.stdout
try: try:
fi = FileInput(files=(t1, t2, t3, t4), inplace=1, bufsize=bs) fi = FileInput(files=(t1, t2, t3, t4), inplace=1)
for line in fi: for line in fi:
line = line[:-1].upper() line = line[:-1].upper()
print(line) print(line)
...@@ -163,7 +155,7 @@ class BufferSizesTests(BaseTests, unittest.TestCase): ...@@ -163,7 +155,7 @@ class BufferSizesTests(BaseTests, unittest.TestCase):
finally: finally:
sys.stdout = savestdout sys.stdout = savestdout
fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs) fi = FileInput(files=(t1, t2, t3, t4))
for line in fi: for line in fi:
self.assertEqual(line[-1], '\n') self.assertEqual(line[-1], '\n')
m = pat.match(line[:-1]) m = pat.match(line[:-1])
...@@ -533,12 +525,11 @@ class FileInputTests(BaseTests, unittest.TestCase): ...@@ -533,12 +525,11 @@ class FileInputTests(BaseTests, unittest.TestCase):
class MockFileInput: class MockFileInput:
"""A class that mocks out fileinput.FileInput for use during unit tests""" """A class that mocks out fileinput.FileInput for use during unit tests"""
def __init__(self, files=None, inplace=False, backup="", bufsize=0, def __init__(self, files=None, inplace=False, backup="", *,
mode="r", openhook=None): mode="r", openhook=None):
self.files = files self.files = files
self.inplace = inplace self.inplace = inplace
self.backup = backup self.backup = backup
self.bufsize = bufsize
self.mode = mode self.mode = mode
self.openhook = openhook self.openhook = openhook
self._file = None self._file = None
...@@ -641,13 +632,11 @@ class Test_fileinput_input(BaseFileInputGlobalMethodsTest): ...@@ -641,13 +632,11 @@ class Test_fileinput_input(BaseFileInputGlobalMethodsTest):
files = object() files = object()
inplace = object() inplace = object()
backup = object() backup = object()
bufsize = object()
mode = object() mode = object()
openhook = object() openhook = object()
# call fileinput.input() with different values for each argument # call fileinput.input() with different values for each argument
result = fileinput.input(files=files, inplace=inplace, backup=backup, result = fileinput.input(files=files, inplace=inplace, backup=backup,
bufsize=bufsize,
mode=mode, openhook=openhook) mode=mode, openhook=openhook)
# ensure fileinput._state was set to the returned object # ensure fileinput._state was set to the returned object
...@@ -658,7 +647,6 @@ class Test_fileinput_input(BaseFileInputGlobalMethodsTest): ...@@ -658,7 +647,6 @@ class Test_fileinput_input(BaseFileInputGlobalMethodsTest):
self.assertIs(files, result.files, "files") self.assertIs(files, result.files, "files")
self.assertIs(inplace, result.inplace, "inplace") self.assertIs(inplace, result.inplace, "inplace")
self.assertIs(backup, result.backup, "backup") self.assertIs(backup, result.backup, "backup")
self.assertIs(bufsize, result.bufsize, "bufsize")
self.assertIs(mode, result.mode, "mode") self.assertIs(mode, result.mode, "mode")
self.assertIs(openhook, result.openhook, "openhook") self.assertIs(openhook, result.openhook, "openhook")
......
:func:`fileinput.input` and :class:`fileinput.FileInput` **bufsize**
argument has been removed (was deprecated and ignored since Python 3.6),
and as a result the **mode** and **openhook** arguments have been made
keyword-only.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment