|
16 | 16 | import unittest |
17 | 17 |
|
18 | 18 | from test import support |
19 | | -from test.support import findfile |
| 19 | +from test.support import findfile, import_fresh_module |
20 | 20 |
|
21 | | -from xml.etree import ElementTree as ET |
| 21 | +pyET = import_fresh_module('xml.etree.ElementTree', blocked=['_elementtree']) |
22 | 22 |
|
23 | 23 | SIMPLE_XMLFILE = findfile("simple.xml", subdir="xmltestdata") |
24 | 24 | try: |
@@ -275,7 +275,7 @@ def simplefind(): |
275 | 275 | """ |
276 | 276 | Test find methods using the elementpath fallback. |
277 | 277 |
|
278 | | - >>> from xml.etree import ElementTree |
| 278 | + >>> ElementTree = pyET |
279 | 279 |
|
280 | 280 | >>> CurrentElementPath = ElementTree.ElementPath |
281 | 281 | >>> ElementTree.ElementPath = ElementTree._SimpleElementPath() |
@@ -460,17 +460,19 @@ def path_cache(): |
460 | 460 | """ |
461 | 461 | Check that the path cache behaves sanely. |
462 | 462 |
|
| 463 | + >>> from xml.etree import ElementPath |
| 464 | +
|
463 | 465 | >>> elem = ET.XML(SAMPLE_XML) |
464 | 466 | >>> for i in range(10): ET.ElementTree(elem).find('./'+str(i)) |
465 | | - >>> cache_len_10 = len(ET.ElementPath._cache) |
| 467 | + >>> cache_len_10 = len(ElementPath._cache) |
466 | 468 | >>> for i in range(10): ET.ElementTree(elem).find('./'+str(i)) |
467 | | - >>> len(ET.ElementPath._cache) == cache_len_10 |
| 469 | + >>> len(ElementPath._cache) == cache_len_10 |
468 | 470 | True |
469 | 471 | >>> for i in range(20): ET.ElementTree(elem).find('./'+str(i)) |
470 | | - >>> len(ET.ElementPath._cache) > cache_len_10 |
| 472 | + >>> len(ElementPath._cache) > cache_len_10 |
471 | 473 | True |
472 | 474 | >>> for i in range(600): ET.ElementTree(elem).find('./'+str(i)) |
473 | | - >>> len(ET.ElementPath._cache) < 500 |
| 475 | + >>> len(ElementPath._cache) < 500 |
474 | 476 | True |
475 | 477 | """ |
476 | 478 |
|
@@ -1879,37 +1881,38 @@ def __init__(self, quiet=False): |
1879 | 1881 | self.checkwarnings = support.check_warnings(*deprecations, quiet=quiet) |
1880 | 1882 |
|
1881 | 1883 | def __enter__(self): |
1882 | | - from xml.etree import ElementTree |
1883 | | - self._nsmap = ElementTree._namespace_map |
1884 | | - self._path_cache = ElementTree.ElementPath._cache |
| 1884 | + from xml.etree import ElementPath |
| 1885 | + if hasattr(ET, '_namespace_map'): |
| 1886 | + self._nsmap = ET._namespace_map |
| 1887 | + else: |
| 1888 | + # when testing the cElementTree alias |
| 1889 | + from xml.etree.ElementTree import _namespace_map |
| 1890 | + self._nsmap = _namespace_map |
1885 | 1891 | # Copy the default namespace mapping |
1886 | | - ElementTree._namespace_map = self._nsmap.copy() |
| 1892 | + self._nsmap_copy = self._nsmap.copy() |
1887 | 1893 | # Copy the path cache (should be empty) |
1888 | | - ElementTree.ElementPath._cache = self._path_cache.copy() |
| 1894 | + self._path_cache = ElementPath._cache |
| 1895 | + ElementPath._cache = self._path_cache.copy() |
1889 | 1896 | self.checkwarnings.__enter__() |
1890 | 1897 |
|
1891 | 1898 | def __exit__(self, *args): |
1892 | | - from xml.etree import ElementTree |
| 1899 | + from xml.etree import ElementPath |
1893 | 1900 | # Restore mapping and path cache |
1894 | | - ElementTree._namespace_map = self._nsmap |
1895 | | - ElementTree.ElementPath._cache = self._path_cache |
| 1901 | + self._nsmap.clear() |
| 1902 | + self._nsmap.update(self._nsmap_copy) |
| 1903 | + ElementPath._cache = self._path_cache |
1896 | 1904 | self.checkwarnings.__exit__(*args) |
1897 | 1905 |
|
1898 | 1906 |
|
1899 | | -def test_main(module_name='xml.etree.ElementTree'): |
| 1907 | +def test_main(module=pyET): |
1900 | 1908 | from test import test_xml_etree |
1901 | 1909 |
|
1902 | | - use_py_module = (module_name == 'xml.etree.ElementTree') |
1903 | | - |
1904 | 1910 | # The same doctests are used for both the Python and the C implementations |
1905 | | - assert test_xml_etree.ET.__name__ == module_name |
| 1911 | + test_xml_etree.ET = module |
1906 | 1912 |
|
1907 | 1913 | # XXX the C module should give the same warnings as the Python module |
1908 | | - with CleanContext(quiet=not use_py_module): |
| 1914 | + with CleanContext(quiet=(module is not pyET)): |
1909 | 1915 | support.run_doctest(test_xml_etree, verbosity=True) |
1910 | 1916 |
|
1911 | | - # The module should not be changed by the tests |
1912 | | - assert test_xml_etree.ET.__name__ == module_name |
1913 | | - |
1914 | 1917 | if __name__ == '__main__': |
1915 | 1918 | test_main() |
0 commit comments