Skip to content

Commit d003a5b

Browse files
authored
bpo-45132 Remove deprecated __getitem__ methods (GH-28225)
Remove deprecated __getitem__ methods of xml.dom.pulldom.DOMEventStream, wsgiref.util.FileWrapper and fileinput.FileInput, deprecated since Python 3.9.
1 parent cb15afc commit d003a5b

File tree

11 files changed

+34
-139
lines changed

11 files changed

+34
-139
lines changed

Doc/library/fileinput.rst

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,10 @@ available for subclassing as well:
146146
Class :class:`FileInput` is the implementation; its methods :meth:`filename`,
147147
:meth:`fileno`, :meth:`lineno`, :meth:`filelineno`, :meth:`isfirstline`,
148148
:meth:`isstdin`, :meth:`nextfile` and :meth:`close` correspond to the
149-
functions of the same name in the module. In addition it has a
150-
:meth:`~io.TextIOBase.readline` method which returns the next input line,
151-
and a :meth:`__getitem__` method which implements the sequence behavior.
152-
The sequence must be accessed in strictly sequential order; random access
153-
and :meth:`~io.TextIOBase.readline` cannot be mixed.
149+
functions of the same name in the module. In addition it is :term:`iterable`
150+
and has a :meth:`~io.TextIOBase.readline` method which returns the next
151+
input line. The sequence must be accessed in strictly sequential order;
152+
random access and :meth:`~io.TextIOBase.readline` cannot be mixed.
154153

155154
With *mode* you can specify which file mode will be passed to :func:`open`. It
156155
must be one of ``'r'`` and ``'rb'``.
@@ -171,17 +170,15 @@ available for subclassing as well:
171170
.. versionchanged:: 3.2
172171
Can be used as a context manager.
173172

174-
.. deprecated:: 3.8
175-
Support for :meth:`__getitem__` method is deprecated.
176-
177173
.. versionchanged:: 3.8
178174
The keyword parameter *mode* and *openhook* are now keyword-only.
179175

180176
.. versionchanged:: 3.10
181177
The keyword-only parameter *encoding* and *errors* are added.
182178

183179
.. versionchanged:: 3.11
184-
The ``'rU'`` and ``'U'`` modes have been removed.
180+
The ``'rU'`` and ``'U'`` modes and the :meth:`__getitem__` method have
181+
been removed.
185182

186183

187184
**Optional in-place filtering:** if the keyword argument ``inplace=True`` is

Doc/library/wsgiref.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ also provides these miscellaneous utilities:
151151
.. class:: FileWrapper(filelike, blksize=8192)
152152

153153
A wrapper to convert a file-like object to an :term:`iterator`. The resulting objects
154-
support both :meth:`__getitem__` and :meth:`__iter__` iteration styles, for
155-
compatibility with Python 2.1 and Jython. As the object is iterated over, the
154+
are :term`iterable`\ s. As the object is iterated over, the
156155
optional *blksize* parameter will be repeatedly passed to the *filelike*
157156
object's :meth:`read` method to obtain bytestrings to yield. When :meth:`read`
158157
returns an empty bytestring, iteration is ended and is not resumable.
@@ -173,8 +172,8 @@ also provides these miscellaneous utilities:
173172
for chunk in wrapper:
174173
print(chunk)
175174

176-
.. deprecated:: 3.8
177-
Support for :meth:`sequence protocol <__getitem__>` is deprecated.
175+
.. versionchanged:: 3.11
176+
Support for :meth:`__getitem__` method has been removed.
178177

179178

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

Doc/library/xml.dom.pulldom.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ DOMEventStream Objects
114114

115115
.. class:: DOMEventStream(stream, parser, bufsize)
116116

117-
.. deprecated:: 3.8
118-
Support for :meth:`sequence protocol <__getitem__>` is deprecated.
117+
.. versionchanged:: 3.11
118+
Support for :meth:`__getitem__` method has been removed.
119119

120120
.. method:: getEvent()
121121

@@ -144,4 +144,3 @@ DOMEventStream Objects
144144
print(node.toxml())
145145

146146
.. method:: DOMEventStream.reset()
147-

Doc/whatsnew/3.11.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,12 @@ Removed
254254
Use ``bdist_wheel`` (wheel packages) instead.
255255
(Contributed by Hugo van Kemenade in :issue:`45124`.)
256256

257+
* Remove :meth:`__getitem__` methods of
258+
:class:`xml.dom.pulldom.DOMEventStream`, :class:`wsgiref.util.FileWrapper`
259+
and :class:`fileinput.FileInput`, deprecated since Python 3.9.
260+
(Contributed by Hugo van Kemenade in :issue:`45132`.)
261+
262+
257263
Optimizations
258264
=============
259265

Lib/fileinput.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -257,21 +257,6 @@ def __next__(self):
257257
self.nextfile()
258258
# repeat with next file
259259

260-
def __getitem__(self, i):
261-
import warnings
262-
warnings.warn(
263-
"Support for indexing FileInput objects is deprecated. "
264-
"Use iterator protocol instead.",
265-
DeprecationWarning,
266-
stacklevel=2
267-
)
268-
if i != self.lineno():
269-
raise RuntimeError("accessing lines out of order")
270-
try:
271-
return self.__next__()
272-
except StopIteration:
273-
raise IndexError("end of input reached")
274-
275260
def nextfile(self):
276261
savestdout = self._savestdout
277262
self._savestdout = None

Lib/test/test_fileinput.py

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from test.support.os_helper import TESTFN
3030
from test.support.os_helper import unlink as safe_unlink
3131
from test.support import os_helper
32-
from test.support import warnings_helper
3332
from test import support
3433
from unittest import mock
3534

@@ -357,44 +356,6 @@ def test_empty_files_list_specified_to_constructor(self):
357356
with FileInput(files=[], encoding="utf-8") as fi:
358357
self.assertEqual(fi._files, ('-',))
359358

360-
@warnings_helper.ignore_warnings(category=DeprecationWarning)
361-
def test__getitem__(self):
362-
"""Tests invoking FileInput.__getitem__() with the current
363-
line number"""
364-
t = self.writeTmp("line1\nline2\n")
365-
with FileInput(files=[t], encoding="utf-8") as fi:
366-
retval1 = fi[0]
367-
self.assertEqual(retval1, "line1\n")
368-
retval2 = fi[1]
369-
self.assertEqual(retval2, "line2\n")
370-
371-
def test__getitem___deprecation(self):
372-
t = self.writeTmp("line1\nline2\n")
373-
with self.assertWarnsRegex(DeprecationWarning,
374-
r'Use iterator protocol instead'):
375-
with FileInput(files=[t]) as fi:
376-
self.assertEqual(fi[0], "line1\n")
377-
378-
@warnings_helper.ignore_warnings(category=DeprecationWarning)
379-
def test__getitem__invalid_key(self):
380-
"""Tests invoking FileInput.__getitem__() with an index unequal to
381-
the line number"""
382-
t = self.writeTmp("line1\nline2\n")
383-
with FileInput(files=[t], encoding="utf-8") as fi:
384-
with self.assertRaises(RuntimeError) as cm:
385-
fi[1]
386-
self.assertEqual(cm.exception.args, ("accessing lines out of order",))
387-
388-
@warnings_helper.ignore_warnings(category=DeprecationWarning)
389-
def test__getitem__eof(self):
390-
"""Tests invoking FileInput.__getitem__() with the line number but at
391-
end-of-input"""
392-
t = self.writeTmp('')
393-
with FileInput(files=[t], encoding="utf-8") as fi:
394-
with self.assertRaises(IndexError) as cm:
395-
fi[0]
396-
self.assertEqual(cm.exception.args, ("end of input reached",))
397-
398359
def test_nextfile_oserror_deleting_backup(self):
399360
"""Tests invoking FileInput.nextfile() when the attempt to delete
400361
the backup file would raise OSError. This error is expected to be

Lib/test/test_pulldom.py

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

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

Lib/test/test_wsgiref.py

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from unittest import mock
22
from test import support
33
from test.support import socket_helper
4-
from test.support import warnings_helper
54
from test.test_httpservers import NoLogRequestHandler
65
from unittest import TestCase
76
from wsgiref.util import setup_testing_defaults
@@ -81,41 +80,26 @@ def run_amock(app=hello_app, data=b"GET / HTTP/1.0\n\n"):
8180

8281
return out.getvalue(), err.getvalue()
8382

84-
def compare_generic_iter(make_it,match):
85-
"""Utility to compare a generic 2.1/2.2+ iterator with an iterable
8683

87-
If running under Python 2.2+, this tests the iterator using iter()/next(),
88-
as well as __getitem__. 'make_it' must be a function returning a fresh
84+
def compare_generic_iter(make_it, match):
85+
"""Utility to compare a generic iterator with an iterable
86+
87+
This tests the iterator using iter()/next().
88+
'make_it' must be a function returning a fresh
8989
iterator to be tested (since this may test the iterator twice)."""
9090

9191
it = make_it()
92-
n = 0
92+
if not iter(it) is it:
93+
raise AssertionError
9394
for item in match:
94-
if not it[n]==item: raise AssertionError
95-
n+=1
95+
if not next(it) == item:
96+
raise AssertionError
9697
try:
97-
it[n]
98-
except IndexError:
98+
next(it)
99+
except StopIteration:
99100
pass
100101
else:
101-
raise AssertionError("Too many items from __getitem__",it)
102-
103-
try:
104-
iter, StopIteration
105-
except NameError:
106-
pass
107-
else:
108-
# Only test iter mode under 2.2+
109-
it = make_it()
110-
if not iter(it) is it: raise AssertionError
111-
for item in match:
112-
if not next(it) == item: raise AssertionError
113-
try:
114-
next(it)
115-
except StopIteration:
116-
pass
117-
else:
118-
raise AssertionError("Too many items from .__next__()", it)
102+
raise AssertionError("Too many items from .__next__()", it)
119103

120104

121105
class IntegrationTests(TestCase):
@@ -340,7 +324,6 @@ def checkReqURI(self,uri,query=1,**kw):
340324
util.setup_testing_defaults(kw)
341325
self.assertEqual(util.request_uri(kw,query),uri)
342326

343-
@warnings_helper.ignore_warnings(category=DeprecationWarning)
344327
def checkFW(self,text,size,match):
345328

346329
def make_it(text=text,size=size):
@@ -359,13 +342,6 @@ def make_it(text=text,size=size):
359342
it.close()
360343
self.assertTrue(it.filelike.closed)
361344

362-
def test_filewrapper_getitem_deprecation(self):
363-
wrapper = util.FileWrapper(StringIO('foobar'), 3)
364-
with self.assertWarnsRegex(DeprecationWarning,
365-
r'Use iterator protocol instead'):
366-
# This should have returned 'bar'.
367-
self.assertEqual(wrapper[1], 'foo')
368-
369345
def testSimpleShifts(self):
370346
self.checkShift('','/', '', '/', '')
371347
self.checkShift('','/x', 'x', '/x', '')

Lib/wsgiref/util.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,6 @@ def __init__(self, filelike, blksize=8192):
1717
if hasattr(filelike,'close'):
1818
self.close = filelike.close
1919

20-
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-
)
28-
data = self.filelike.read(self.blksize)
29-
if data:
30-
return data
31-
raise IndexError
32-
3320
def __iter__(self):
3421
return self
3522

Lib/xml/dom/pulldom.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -216,19 +216,6 @@ def reset(self):
216216
self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
217217
self.parser.setContentHandler(self.pulldom)
218218

219-
def __getitem__(self, pos):
220-
import warnings
221-
warnings.warn(
222-
"DOMEventStream's __getitem__ method ignores 'pos' parameter. "
223-
"Use iterator protocol instead.",
224-
DeprecationWarning,
225-
stacklevel=2
226-
)
227-
rc = self.getEvent()
228-
if rc:
229-
return rc
230-
raise IndexError
231-
232219
def __next__(self):
233220
rc = self.getEvent()
234221
if rc:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Remove :meth:`__getitem__` methods of
2+
:class:`xml.dom.pulldom.DOMEventStream`, :class:`wsgiref.util.FileWrapper`
3+
and :class:`fileinput.FileInput`, deprecated since Python 3.9.
4+
5+
Patch by Hugo van Kemenade.

0 commit comments

Comments
 (0)