Skip to content

bpo-33563: Fileinput(bufsize=0) does not emit deprecation warning. #6959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions Lib/fileinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@

_state = None

def input(files=None, inplace=False, backup="", bufsize=0,
_sentinel = object()

def input(files=None, inplace=False, backup="", bufsize=_sentinel,
mode="r", openhook=None):
"""Return an instance of the FileInput class, which can be iterated.

Expand All @@ -91,7 +93,13 @@ def input(files=None, inplace=False, backup="", bufsize=0,
global _state
if _state and _state._file:
raise RuntimeError("input() already active")
_state = FileInput(files, inplace, backup, bufsize, mode, openhook)
if bufsize is not _sentinel:
import warnings
warnings.warn('bufsize is deprecated and ignored since Python 3.6'
' and will be removed in 3.8',
DeprecationWarning, stacklevel=2)

_state = FileInput(files, inplace, backup, mode=mode, openhook=openhook)
return _state

def close():
Expand Down Expand Up @@ -185,7 +193,7 @@ class FileInput:
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="", bufsize=_sentinel,
mode="r", openhook=None):
if isinstance(files, str):
files = (files,)
Expand All @@ -201,9 +209,10 @@ def __init__(self, files=None, inplace=False, backup="", bufsize=0,
self._files = files
self._inplace = inplace
self._backup = backup
if bufsize:
if bufsize is not _sentinel:
import warnings
warnings.warn('bufsize is deprecated and ignored',
warnings.warn('bufsize is deprecated and ignored since Python 3.6'
' and will be removed in 3.8',
DeprecationWarning, stacklevel=2)
self._savestdout = None
self._output = None
Expand Down
17 changes: 11 additions & 6 deletions Lib/test/test_fileinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,13 @@ def close(self):
class BufferSizesTests(BaseTests, unittest.TestCase):
def test_buffer_sizes(self):
# First, run the tests with default and teeny buffer size.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests will be not ran with non-default buffer size. Please restore tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, but that is useless as bufsize= is anyway ignored. That is basically running the same exact test twice...

for round, bs in (0, 0), (1, 30):
from fileinput import _sentinel
for round, bs in (0, 0), (1, 30), (2, _sentinel):
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)))
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)))
if bs:
if bs is not _sentinel:
with self.assertWarns(DeprecationWarning):
self.buffer_size_test(t1, t2, t3, t4, bs, round)
else:
Expand Down Expand Up @@ -646,9 +647,10 @@ def do_test_call_input(self):
openhook = object()

# call fileinput.input() with different values for each argument
result = fileinput.input(files=files, inplace=inplace, backup=backup,
bufsize=bufsize,
mode=mode, openhook=openhook)
with self.assertWarns(DeprecationWarning):
result = fileinput.input(files=files, inplace=inplace, backup=backup,
bufsize=bufsize,
mode=mode, openhook=openhook)

# ensure fileinput._state was set to the returned object
self.assertIs(result, fileinput._state, "fileinput._state")
Expand All @@ -658,10 +660,13 @@ def do_test_call_input(self):
self.assertIs(files, result.files, "files")
self.assertIs(inplace, result.inplace, "inplace")
self.assertIs(backup, result.backup, "backup")
self.assertIs(bufsize, result.bufsize, "bufsize")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please restore this assertion.

Copy link
Contributor Author

@Carreau Carreau May 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is testing the Mock, the MockFileInput have a bufsize attribute, the rel FileInput does not. The goal of this test is to explicitly test that fileinput.input is forwrding input(bufsize=...) to FileInput(bufsize...) which this patch does remove in order to print a warnign with the correct stack. I can change to assertIsNot though.

self.assertIs(mode, result.mode, "mode")
self.assertIs(openhook, result.openhook, "openhook")

# FileInput(bufsize=..) is deprecated, check that fileinut.input will
# _not_ its value to the MockFileInput
self.assertIsNot(bufsize, result.bufsize, "bufsize")

class Test_fileinput_close(BaseFileInputGlobalMethodsTest):
"""Unit tests for fileinput.close()"""

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve fileinput's DeprecationWarning and emit them when bufsize=0 is
passed.