bpo-29204: Emit warnings for already deprecated ElementTree features.… · python/cpython@762ec97
@@ -6,6 +6,7 @@
66# monkey-patched when running the "test_xml_etree_c" test suite.
7788import copy
9+import functools
910import html
1011import io
1112import operator
9091"""
9192929394+def checkwarnings(*filters, quiet=False):
95+def decorator(test):
96+def newtest(*args, **kwargs):
97+with support.check_warnings(*filters, quiet=quiet):
98+test(*args, **kwargs)
99+functools.update_wrapper(newtest, test)
100+return newtest
101+return decorator
102+103+93104class ModuleTest(unittest.TestCase):
94105def test_sanity(self):
95106# Import sanity.
@@ -690,6 +701,10 @@ def comment(self, data):
690701 ])
691702692703704+# Element.getchildren() and ElementTree.getiterator() are deprecated.
705+@checkwarnings(("This method will be removed in future versions. "
706+ "Use .+ instead.",
707+ (DeprecationWarning, PendingDeprecationWarning)))
693708def test_getchildren(self):
694709# Test Element.getchildren()
695710@@ -1558,7 +1573,7 @@ def test_bug_200708_close(self):
15581573class EchoTarget:
15591574def close(self):
15601575return ET.Element("element") # simulate root
1561-parser = ET.XMLParser(EchoTarget())
1576+parser = ET.XMLParser(target=EchoTarget())
15621577parser.feed("<element>some text</element>")
15631578self.assertEqual(parser.close().tag, 'element')
15641579@@ -2225,8 +2240,12 @@ def test_find_through_ElementTree(self):
22252240self.assertEqual(summarize_list(ET.ElementTree(e).findall('tag')),
22262241 ['tag'] * 2)
22272242# this produces a warning
2228-self.assertEqual(summarize_list(ET.ElementTree(e).findall('//tag')),
2229- ['tag'] * 3)
2243+msg = ("This search is broken in 1.3 and earlier, and will be fixed "
2244+"in a future version. If you rely on the current behaviour, "
2245+"change it to '.+'")
2246+with self.assertWarnsRegex(FutureWarning, msg):
2247+it = ET.ElementTree(e).findall('//tag')
2248+self.assertEqual(summarize_list(it), ['tag'] * 3)
223022492231225022322251class ElementIterTest(unittest.TestCase):
@@ -2311,6 +2330,9 @@ def test_iter_by_tag(self):
23112330self.assertEqual(self._ilist(doc), all_tags)
23122331self.assertEqual(self._ilist(doc, '*'), all_tags)
231323322333+# Element.getiterator() is deprecated.
2334+@checkwarnings(("This method will be removed in future versions. "
2335+ "Use .+ instead.", PendingDeprecationWarning))
23142336def test_getiterator(self):
23152337doc = ET.XML('''
23162338 <document>
@@ -2493,13 +2515,13 @@ def _check_sample_element(self, e):
24932515def test_constructor_args(self):
24942516# Positional args. The first (html) is not supported, but should be
24952517# nevertheless correctly accepted.
2496-parser = ET.XMLParser(None, ET.TreeBuilder(), 'utf-8')
2518+with self.assertWarnsRegex(DeprecationWarning, r'\bhtml\b'):
2519+parser = ET.XMLParser(None, ET.TreeBuilder(), 'utf-8')
24972520parser.feed(self.sample1)
24982521self._check_sample_element(parser.close())
2499252225002523# Now as keyword args.
25012524parser2 = ET.XMLParser(encoding='utf-8',
2502-html=[{}],
25032525target=ET.TreeBuilder())
25042526parser2.feed(self.sample1)
25052527self._check_sample_element(parser2.close())
@@ -3016,46 +3038,6 @@ def test_correct_import_pyET(self):
30163038# --------------------------------------------------------------------
30173039301830403019-class CleanContext(object):
3020-"""Provide default namespace mapping and path cache."""
3021-checkwarnings = None
3022-3023-def __init__(self, quiet=False):
3024-if sys.flags.optimize >= 2:
3025-# under -OO, doctests cannot be run and therefore not all warnings
3026-# will be emitted
3027-quiet = True
3028-deprecations = (
3029-# Search behaviour is broken if search path starts with "/".
3030- ("This search is broken in 1.3 and earlier, and will be fixed "
3031-"in a future version. If you rely on the current behaviour, "
3032-"change it to '.+'", FutureWarning),
3033-# Element.getchildren() and Element.getiterator() are deprecated.
3034- ("This method will be removed in future versions. "
3035-"Use .+ instead.", DeprecationWarning),
3036- ("This method will be removed in future versions. "
3037-"Use .+ instead.", PendingDeprecationWarning))
3038-self.checkwarnings = support.check_warnings(*deprecations, quiet=quiet)
3039-3040-def __enter__(self):
3041-from xml.etree import ElementPath
3042-self._nsmap = ET.register_namespace._namespace_map
3043-# Copy the default namespace mapping
3044-self._nsmap_copy = self._nsmap.copy()
3045-# Copy the path cache (should be empty)
3046-self._path_cache = ElementPath._cache
3047-ElementPath._cache = self._path_cache.copy()
3048-self.checkwarnings.__enter__()
3049-3050-def __exit__(self, *args):
3051-from xml.etree import ElementPath
3052-# Restore mapping and path cache
3053-self._nsmap.clear()
3054-self._nsmap.update(self._nsmap_copy)
3055-ElementPath._cache = self._path_cache
3056-self.checkwarnings.__exit__(*args)
3057-3058-30593041def test_main(module=None):
30603042# When invoked without a module, runs the Python ET tests by loading pyET.
30613043# Otherwise, uses the given module as the ET.
@@ -3095,11 +3077,22 @@ def test_main(module=None):
30953077NoAcceleratorTest,
30963078 ])
309730793080+# Provide default namespace mapping and path cache.
3081+from xml.etree import ElementPath
3082+nsmap = ET.register_namespace._namespace_map
3083+# Copy the default namespace mapping
3084+nsmap_copy = nsmap.copy()
3085+# Copy the path cache (should be empty)
3086+path_cache = ElementPath._cache
3087+ElementPath._cache = path_cache.copy()
30983088try:
3099-# XXX the C module should give the same warnings as the Python module
3100-with CleanContext(quiet=(pyET is not ET)):
3101-support.run_unittest(*test_classes)
3089+support.run_unittest(*test_classes)
31023090finally:
3091+from xml.etree import ElementPath
3092+# Restore mapping and path cache
3093+nsmap.clear()
3094+nsmap.update(nsmap_copy)
3095+ElementPath._cache = path_cache
31033096# don't interfere with subsequent tests
31043097ET = pyET = None
31053098