summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorBerker Peksag <berker.peksag@gmail.com>2018-08-11 06:05:04 (GMT)
committerGitHub <noreply@github.com>2018-08-11 06:05:04 (GMT)
commit84a13fbda0d79789e3c9efcc9f64752261ce1e8d (patch)
tree5cc443f7bbfde403e0f7a731b5fda5fb4f54676a /Lib/test
parent423d05f6f59b24c91b9ef6b2e4ac130316764382 (diff)
downloadcpython-84a13fbda0d79789e3c9efcc9f64752261ce1e8d.zip
cpython-84a13fbda0d79789e3c9efcc9f64752261ce1e8d.tar.gz
cpython-84a13fbda0d79789e3c9efcc9f64752261ce1e8d.tar.bz2
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.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/support/__init__.py17
-rw-r--r--Lib/test/test_fileinput.py10
-rw-r--r--Lib/test/test_pulldom.py7
-rw-r--r--Lib/test/test_wsgiref.py8
4 files changed, 42 insertions, 0 deletions
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index d8dabd4..13b60f7 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -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.
diff --git a/Lib/test/test_fileinput.py b/Lib/test/test_fileinput.py
index c97bb4c..c5d722e 100644
--- a/Lib/test/test_fileinput.py
+++ b/Lib/test/test_fileinput.py
@@ -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"""
diff --git a/Lib/test/test_pulldom.py b/Lib/test/test_pulldom.py
index 3d89e3a..f454098 100644
--- a/Lib/test/test_pulldom.py
+++ b/Lib/test/test_pulldom.py
@@ -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."""
diff --git a/Lib/test/test_wsgiref.py b/Lib/test/test_wsgiref.py
index 8422b30..737dfed 100644
--- a/Lib/test/test_wsgiref.py
+++ b/Lib/test/test_wsgiref.py
@@ -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', '')