]> granicus.if.org Git - python/commitdiff
bpo-9372: Deprecate several __getitem__ methods (GH-8609)
authorBerker Peksag <berker.peksag@gmail.com>
Sat, 11 Aug 2018 06:05:04 +0000 (09:05 +0300)
committerGitHub <noreply@github.com>
Sat, 11 Aug 2018 06:05:04 +0000 (09:05 +0300)
The __getitem__ methods of DOMEventStream, FileInput,
and FileWrapper classes ignore their 'index' parameters
and return the next item instead.

12 files changed:
Doc/library/fileinput.rst
Doc/library/wsgiref.rst
Doc/library/xml.dom.pulldom.rst
Doc/whatsnew/3.8.rst
Lib/fileinput.py
Lib/test/support/__init__.py
Lib/test/test_fileinput.py
Lib/test/test_pulldom.py
Lib/test/test_wsgiref.py
Lib/wsgiref/util.py
Lib/xml/dom/pulldom.py
Misc/NEWS.d/next/Library/2018-08-01-21-26-17.bpo-9372.V8Ou3K.rst [new file with mode: 0644]

index 5881fefe293276c70b3ea3ae86aad5a28c784d98..f1e29a8a7d85ae9adc1ff969e6231671f71dfbd9 100644 (file)
@@ -169,6 +169,9 @@ available for subclassing as well:
    .. deprecated-removed:: 3.6 3.8
       The *bufsize* parameter.
 
+   .. deprecated:: 3.8
+      Support for :meth:`__getitem__` method is deprecated.
+
 
 **Optional in-place filtering:** if the keyword argument ``inplace=True`` is
 passed to :func:`fileinput.input` or to the :class:`FileInput` constructor, the
index 2d9b7b359e8060bb8fbf42da407348149033b22a..f8811c5a36174fc885d45fe84e68017e4f7cdc92 100644 (file)
@@ -173,6 +173,8 @@ also provides these miscellaneous utilities:
       for chunk in wrapper:
           print(chunk)
 
+   .. deprecated:: 3.8
+      Support for :meth:`sequence protocol <__getitem__>` is deprecated.
 
 
 :mod:`wsgiref.headers` -- WSGI response header tools
index 5c0f469ad7a5cf5e32508480100ef91330cf9134..56f545c0e6d83e81eec4859303d937d3b8e9b5cd 100644 (file)
@@ -100,6 +100,8 @@ DOMEventStream Objects
 
 .. class:: DOMEventStream(stream, parser, bufsize)
 
+   .. deprecated:: 3.8
+      Support for :meth:`sequence protocol <__getitem__>` is deprecated.
 
    .. method:: getEvent()
 
index 6e0c8b807c007dfa405e0c3e52ff774a058ea623..ef5455a9f6e2a5bf92ed13984903db86f04c9969 100644 (file)
@@ -154,6 +154,15 @@ Build and C API Changes
 
   (Contributed by Antoine Pitrou in :issue:`32430`.)
 
+* The :meth:`__getitem__` methods of :class:`xml.dom.pulldom.DOMEventStream`,
+  :class:`wsgiref.util.FileWrapper` and :class:`fileinput.FileInput` have been
+  deprecated.
+
+  Implementations of these methods have been ignoring their *index* parameter,
+  and returning the next item instead.
+
+  (Contributed by Berker Peksag in :issue:`9372`.)
+
 
 Deprecated
 ==========
index c6fc9a1981a1fa058b0c9f91f3f58f9cd9d5d6c5..a7f8df37aada84812dc10122adfd389491351fb0 100644 (file)
@@ -259,6 +259,13 @@ class FileInput:
             # repeat with next file
 
     def __getitem__(self, i):
+        import warnings
+        warnings.warn(
+            "Support for indexing FileInput objects is deprecated. "
+            "Use iterator protocol instead.",
+            DeprecationWarning,
+            stacklevel=2
+        )
         if i != self.lineno():
             raise RuntimeError("accessing lines out of order")
         try:
index d8dabd435895643eb86e67cae7b6c2eefbf2148e..13b60f7e815399f84cc31dd137e85286a778b07e 100644 (file)
@@ -89,6 +89,7 @@ __all__ = [
     "requires_IEEE_754", "skip_unless_xattr", "requires_zlib",
     "anticipate_failure", "load_package_tests", "detect_api_mismatch",
     "check__all__", "skip_unless_bind_unix_socket",
+    "ignore_warnings",
     # sys
     "is_jython", "is_android", "check_impl_detail", "unix_shell",
     "setswitchinterval",
@@ -138,6 +139,22 @@ def _ignore_deprecated_imports(ignore=True):
         yield
 
 
+def ignore_warnings(*, category):
+    """Decorator to suppress deprecation warnings.
+
+    Use of context managers to hide warnings make diffs
+    more noisy and tools like 'git blame' less useful.
+    """
+    def decorator(test):
+        @functools.wraps(test)
+        def wrapper(self, *args, **kwargs):
+            with warnings.catch_warnings():
+                warnings.simplefilter('ignore', category=category)
+                return test(self, *args, **kwargs)
+        return wrapper
+    return decorator
+
+
 def import_module(name, deprecated=False, *, required_on=()):
     """Import and return the module to be tested, raising SkipTest if
     it is not available.
index c97bb4cb3b0265ff471da27d81d9f15787513498..c5d722e58f596082a7d7c185be4a30f08ddcb261 100644 (file)
@@ -351,6 +351,7 @@ class FileInputTests(BaseTests, unittest.TestCase):
         with FileInput(files=[]) as fi:
             self.assertEqual(fi._files, ('-',))
 
+    @support.ignore_warnings(category=DeprecationWarning)
     def test__getitem__(self):
         """Tests invoking FileInput.__getitem__() with the current
            line number"""
@@ -361,6 +362,14 @@ class FileInputTests(BaseTests, unittest.TestCase):
             retval2 = fi[1]
             self.assertEqual(retval2, "line2\n")
 
+    def test__getitem___deprecation(self):
+        t = self.writeTmp("line1\nline2\n")
+        with self.assertWarnsRegex(DeprecationWarning,
+                                   r'Use iterator protocol instead'):
+            with FileInput(files=[t]) as fi:
+                self.assertEqual(fi[0], "line1\n")
+
+    @support.ignore_warnings(category=DeprecationWarning)
     def test__getitem__invalid_key(self):
         """Tests invoking FileInput.__getitem__() with an index unequal to
            the line number"""
@@ -370,6 +379,7 @@ class FileInputTests(BaseTests, unittest.TestCase):
                 fi[1]
         self.assertEqual(cm.exception.args, ("accessing lines out of order",))
 
+    @support.ignore_warnings(category=DeprecationWarning)
     def test__getitem__eof(self):
         """Tests invoking FileInput.__getitem__() with the line number but at
            end-of-input"""
index 3d89e3adda26ce646d3a05eced17aca727e6e779..f454098c65b1e98ccabea814fa988a1d4387c71c 100644 (file)
@@ -159,6 +159,13 @@ class PullDOMTestCase(unittest.TestCase):
             self.fail(
                 "Ran out of events, but should have received END_DOCUMENT")
 
+    def test_getitem_deprecation(self):
+        parser = pulldom.parseString(SMALL_SAMPLE)
+        with self.assertWarnsRegex(DeprecationWarning,
+                                   r'Use iterator protocol instead'):
+            # This should have returned 'END_ELEMENT'.
+            self.assertEqual(parser[-1][0], pulldom.START_DOCUMENT)
+
 
 class ThoroughTestCase(unittest.TestCase):
     """Test the hard-to-reach parts of pulldom."""
index 8422b308d795e9bfdec76b897052f62fc6796c29..737dfed3a51e574ebb91cf28e1bf0821ffab46d0 100644 (file)
@@ -338,6 +338,7 @@ class UtilityTests(TestCase):
         util.setup_testing_defaults(kw)
         self.assertEqual(util.request_uri(kw,query),uri)
 
+    @support.ignore_warnings(category=DeprecationWarning)
     def checkFW(self,text,size,match):
 
         def make_it(text=text,size=size):
@@ -356,6 +357,13 @@ class UtilityTests(TestCase):
         it.close()
         self.assertTrue(it.filelike.closed)
 
+    def test_filewrapper_getitem_deprecation(self):
+        wrapper = util.FileWrapper(StringIO('foobar'), 3)
+        with self.assertWarnsRegex(DeprecationWarning,
+                                   r'Use iterator protocol instead'):
+            # This should have returned 'bar'.
+            self.assertEqual(wrapper[1], 'foo')
+
     def testSimpleShifts(self):
         self.checkShift('','/', '', '/', '')
         self.checkShift('','/x', 'x', '/x', '')
index 516fe898d014df979312bdc5e1d41bdcb53a6eda..1cff7a37452e596e91747f7abc42df1542b77941 100644 (file)
@@ -18,6 +18,13 @@ class FileWrapper:
             self.close = filelike.close
 
     def __getitem__(self,key):
+        import warnings
+        warnings.warn(
+            "FileWrapper's __getitem__ method ignores 'key' parameter. "
+            "Use iterator protocol instead.",
+            DeprecationWarning,
+            stacklevel=2
+        )
         data = self.filelike.read(self.blksize)
         if data:
             return data
index 43504f76561652f3384b8b4ba62c12aea1fc4122..96a8d59519ef45957b101b7295d044c316d4e84c 100644 (file)
@@ -217,6 +217,13 @@ class DOMEventStream:
         self.parser.setContentHandler(self.pulldom)
 
     def __getitem__(self, pos):
+        import warnings
+        warnings.warn(
+            "DOMEventStream's __getitem__ method ignores 'pos' parameter. "
+            "Use iterator protocol instead.",
+            DeprecationWarning,
+            stacklevel=2
+        )
         rc = self.getEvent()
         if rc:
             return rc
diff --git a/Misc/NEWS.d/next/Library/2018-08-01-21-26-17.bpo-9372.V8Ou3K.rst b/Misc/NEWS.d/next/Library/2018-08-01-21-26-17.bpo-9372.V8Ou3K.rst
new file mode 100644 (file)
index 0000000..8ae5ce1
--- /dev/null
@@ -0,0 +1,3 @@
+Deprecate :meth:`__getitem__` methods of
+:class:`xml.dom.pulldom.DOMEventStream`, :class:`wsgiref.util.FileWrapper`
+and :class:`fileinput.FileInput`.