|
6 | 6 | # monkey-patched when running the "test_xml_etree_c" test suite.
|
7 | 7 |
|
8 | 8 | import copy
|
| 9 | +import functools |
9 | 10 | import html
|
10 | 11 | import io
|
11 | 12 | import operator
|
|
90 | 91 | """
|
91 | 92 |
|
92 | 93 |
|
| 94 | +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 | + |
93 | 104 | class ModuleTest(unittest.TestCase):
|
94 | 105 | def test_sanity(self):
|
95 | 106 | # Import sanity.
|
@@ -690,6 +701,10 @@ def comment(self, data):
|
690 | 701 | ])
|
691 | 702 |
|
692 | 703 |
|
| 704 | + # Element.getchildren() and ElementTree.getiterator() are deprecated. |
| 705 | + @checkwarnings(("This method will be removed in future versions. " |
| 706 | + "Use .+ instead.", |
| 707 | + (DeprecationWarning, PendingDeprecationWarning))) |
693 | 708 | def test_getchildren(self):
|
694 | 709 | # Test Element.getchildren()
|
695 | 710 |
|
@@ -1558,7 +1573,7 @@ def test_bug_200708_close(self):
|
1558 | 1573 | class EchoTarget:
|
1559 | 1574 | def close(self):
|
1560 | 1575 | return ET.Element("element") # simulate root
|
1561 |
| - parser = ET.XMLParser(EchoTarget()) |
| 1576 | + parser = ET.XMLParser(target=EchoTarget()) |
1562 | 1577 | parser.feed("<element>some text</element>")
|
1563 | 1578 | self.assertEqual(parser.close().tag, 'element')
|
1564 | 1579 |
|
@@ -2225,8 +2240,12 @@ def test_find_through_ElementTree(self):
|
2225 | 2240 | self.assertEqual(summarize_list(ET.ElementTree(e).findall('tag')),
|
2226 | 2241 | ['tag'] * 2)
|
2227 | 2242 | # 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) |
2230 | 2249 |
|
2231 | 2250 |
|
2232 | 2251 | class ElementIterTest(unittest.TestCase):
|
@@ -2311,6 +2330,9 @@ def test_iter_by_tag(self):
|
2311 | 2330 | self.assertEqual(self._ilist(doc), all_tags)
|
2312 | 2331 | self.assertEqual(self._ilist(doc, '*'), all_tags)
|
2313 | 2332 |
|
| 2333 | + # Element.getiterator() is deprecated. |
| 2334 | + @checkwarnings(("This method will be removed in future versions. " |
| 2335 | + "Use .+ instead.", PendingDeprecationWarning)) |
2314 | 2336 | def test_getiterator(self):
|
2315 | 2337 | doc = ET.XML('''
|
2316 | 2338 | <document>
|
@@ -2493,13 +2515,13 @@ def _check_sample_element(self, e):
|
2493 | 2515 | def test_constructor_args(self):
|
2494 | 2516 | # Positional args. The first (html) is not supported, but should be
|
2495 | 2517 | # 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') |
2497 | 2520 | parser.feed(self.sample1)
|
2498 | 2521 | self._check_sample_element(parser.close())
|
2499 | 2522 |
|
2500 | 2523 | # Now as keyword args.
|
2501 | 2524 | parser2 = ET.XMLParser(encoding='utf-8',
|
2502 |
| - html=[{}], |
2503 | 2525 | target=ET.TreeBuilder())
|
2504 | 2526 | parser2.feed(self.sample1)
|
2505 | 2527 | self._check_sample_element(parser2.close())
|
@@ -3016,46 +3038,6 @@ def test_correct_import_pyET(self):
|
3016 | 3038 | # --------------------------------------------------------------------
|
3017 | 3039 |
|
3018 | 3040 |
|
3019 |
| -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 |
| - |
3059 | 3041 | def test_main(module=None):
|
3060 | 3042 | # When invoked without a module, runs the Python ET tests by loading pyET.
|
3061 | 3043 | # Otherwise, uses the given module as the ET.
|
@@ -3095,11 +3077,22 @@ def test_main(module=None):
|
3095 | 3077 | NoAcceleratorTest,
|
3096 | 3078 | ])
|
3097 | 3079 |
|
| 3080 | + # 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() |
3098 | 3088 | try:
|
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) |
3102 | 3090 | finally:
|
| 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 |
3103 | 3096 | # don't interfere with subsequent tests
|
3104 | 3097 | ET = pyET = None
|
3105 | 3098 |
|
|
0 commit comments