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

Skip to content

Commit 84a13fb

Browse files
authored
bpo-9372: Deprecate several __getitem__ methods (GH-8609)
The __getitem__ methods of DOMEventStream, FileInput, and FileWrapper classes ignore their 'index' parameters and return the next item instead.
1 parent 423d05f commit 84a13fb

12 files changed

Lines changed: 82 additions & 0 deletions

File tree

Doc/library/fileinput.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ available for subclassing as well:
169169
.. deprecated-removed:: 3.6 3.8
170170
The *bufsize* parameter.
171171

172+
.. deprecated:: 3.8
173+
Support for :meth:`__getitem__` method is deprecated.
174+
172175

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

Doc/library/wsgiref.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ also provides these miscellaneous utilities:
173173
for chunk in wrapper:
174174
print(chunk)
175175

176+
.. deprecated:: 3.8
177+
Support for :meth:`sequence protocol <__getitem__>` is deprecated.
176178

177179

178180
:mod:`wsgiref.headers` -- WSGI response header tools

Doc/library/xml.dom.pulldom.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ DOMEventStream Objects
100100

101101
.. class:: DOMEventStream(stream, parser, bufsize)
102102

103+
.. deprecated:: 3.8
104+
Support for :meth:`sequence protocol <__getitem__>` is deprecated.
103105

104106
.. method:: getEvent()
105107

Doc/whatsnew/3.8.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ Build and C API Changes
154154

155155
(Contributed by Antoine Pitrou in :issue:`32430`.)
156156

157+
* The :meth:`__getitem__` methods of :class:`xml.dom.pulldom.DOMEventStream`,
158+
:class:`wsgiref.util.FileWrapper` and :class:`fileinput.FileInput` have been
159+
deprecated.
160+
161+
Implementations of these methods have been ignoring their *index* parameter,
162+
and returning the next item instead.
163+
164+
(Contributed by Berker Peksag in :issue:`9372`.)
165+
157166

158167
Deprecated
159168
==========

Lib/fileinput.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,13 @@ def __next__(self):
259259
# repeat with next file
260260

261261
def __getitem__(self, i):
262+
import warnings
263+
warnings.warn(
264+
"Support for indexing FileInput objects is deprecated. "
265+
"Use iterator protocol instead.",
266+
DeprecationWarning,
267+
stacklevel=2
268+
)
262269
if i != self.lineno():
263270
raise RuntimeError("accessing lines out of order")
264271
try:

Lib/test/support/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"requires_IEEE_754", "skip_unless_xattr", "requires_zlib",
9090
"anticipate_failure", "load_package_tests", "detect_api_mismatch",
9191
"check__all__", "skip_unless_bind_unix_socket",
92+
"ignore_warnings",
9293
# sys
9394
"is_jython", "is_android", "check_impl_detail", "unix_shell",
9495
"setswitchinterval",
@@ -138,6 +139,22 @@ def _ignore_deprecated_imports(ignore=True):
138139
yield
139140

140141

142+
def ignore_warnings(*, category):
143+
"""Decorator to suppress deprecation warnings.
144+
145+
Use of context managers to hide warnings make diffs
146+
more noisy and tools like 'git blame' less useful.
147+
"""
148+
def decorator(test):
149+
@functools.wraps(test)
150+
def wrapper(self, *args, **kwargs):
151+
with warnings.catch_warnings():
152+
warnings.simplefilter('ignore', category=category)
153+
return test(self, *args, **kwargs)
154+
return wrapper
155+
return decorator
156+
157+
141158
def import_module(name, deprecated=False, *, required_on=()):
142159
"""Import and return the module to be tested, raising SkipTest if
143160
it is not available.

Lib/test/test_fileinput.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ def test_empty_files_list_specified_to_constructor(self):
351351
with FileInput(files=[]) as fi:
352352
self.assertEqual(fi._files, ('-',))
353353

354+
@support.ignore_warnings(category=DeprecationWarning)
354355
def test__getitem__(self):
355356
"""Tests invoking FileInput.__getitem__() with the current
356357
line number"""
@@ -361,6 +362,14 @@ def test__getitem__(self):
361362
retval2 = fi[1]
362363
self.assertEqual(retval2, "line2\n")
363364

365+
def test__getitem___deprecation(self):
366+
t = self.writeTmp("line1\nline2\n")
367+
with self.assertWarnsRegex(DeprecationWarning,
368+
r'Use iterator protocol instead'):
369+
with FileInput(files=[t]) as fi:
370+
self.assertEqual(fi[0], "line1\n")
371+
372+
@support.ignore_warnings(category=DeprecationWarning)
364373
def test__getitem__invalid_key(self):
365374
"""Tests invoking FileInput.__getitem__() with an index unequal to
366375
the line number"""
@@ -370,6 +379,7 @@ def test__getitem__invalid_key(self):
370379
fi[1]
371380
self.assertEqual(cm.exception.args, ("accessing lines out of order",))
372381

382+
@support.ignore_warnings(category=DeprecationWarning)
373383
def test__getitem__eof(self):
374384
"""Tests invoking FileInput.__getitem__() with the line number but at
375385
end-of-input"""

Lib/test/test_pulldom.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ def test_end_document(self):
159159
self.fail(
160160
"Ran out of events, but should have received END_DOCUMENT")
161161

162+
def test_getitem_deprecation(self):
163+
parser = pulldom.parseString(SMALL_SAMPLE)
164+
with self.assertWarnsRegex(DeprecationWarning,
165+
r'Use iterator protocol instead'):
166+
# This should have returned 'END_ELEMENT'.
167+
self.assertEqual(parser[-1][0], pulldom.START_DOCUMENT)
168+
162169

163170
class ThoroughTestCase(unittest.TestCase):
164171
"""Test the hard-to-reach parts of pulldom."""

Lib/test/test_wsgiref.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ def checkReqURI(self,uri,query=1,**kw):
338338
util.setup_testing_defaults(kw)
339339
self.assertEqual(util.request_uri(kw,query),uri)
340340

341+
@support.ignore_warnings(category=DeprecationWarning)
341342
def checkFW(self,text,size,match):
342343

343344
def make_it(text=text,size=size):
@@ -356,6 +357,13 @@ def make_it(text=text,size=size):
356357
it.close()
357358
self.assertTrue(it.filelike.closed)
358359

360+
def test_filewrapper_getitem_deprecation(self):
361+
wrapper = util.FileWrapper(StringIO('foobar'), 3)
362+
with self.assertWarnsRegex(DeprecationWarning,
363+
r'Use iterator protocol instead'):
364+
# This should have returned 'bar'.
365+
self.assertEqual(wrapper[1], 'foo')
366+
359367
def testSimpleShifts(self):
360368
self.checkShift('','/', '', '/', '')
361369
self.checkShift('','/x', 'x', '/x', '')

Lib/wsgiref/util.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ def __init__(self, filelike, blksize=8192):
1818
self.close = filelike.close
1919

2020
def __getitem__(self,key):
21+
import warnings
22+
warnings.warn(
23+
"FileWrapper's __getitem__ method ignores 'key' parameter. "
24+
"Use iterator protocol instead.",
25+
DeprecationWarning,
26+
stacklevel=2
27+
)
2128
data = self.filelike.read(self.blksize)
2229
if data:
2330
return data

0 commit comments

Comments
 (0)