8
8
9
9
import cgi
10
10
import copy
11
+ import functools
11
12
import io
12
13
import pickle
13
14
import StringIO
86
87
"""
87
88
88
89
90
+ def checkwarnings (* filters ):
91
+ def decorator (test ):
92
+ def newtest (* args , ** kwargs ):
93
+ with support .check_warnings (* filters ):
94
+ test (* args , ** kwargs )
95
+ functools .update_wrapper (newtest , test )
96
+ return newtest
97
+ return decorator
98
+
99
+
89
100
class ModuleTest (unittest .TestCase ):
90
101
# TODO: this should be removed once we get rid of the global module vars
91
102
@@ -656,6 +667,10 @@ def comment(self, data):
656
667
])
657
668
658
669
670
+ # Element.getchildren() and ElementTree.getiterator() are deprecated.
671
+ @checkwarnings (("This method will be removed in future versions. "
672
+ "Use .+ instead." ,
673
+ (DeprecationWarning , PendingDeprecationWarning )))
659
674
def test_getchildren (self ):
660
675
# Test Element.getchildren()
661
676
@@ -1356,9 +1371,15 @@ def test_bug_200708_close(self):
1356
1371
1357
1372
# Test custom builder.
1358
1373
class EchoTarget :
1374
+ def start (self , tag , attrib ):
1375
+ pass
1376
+ def end (self , tag ):
1377
+ pass
1378
+ def data (self , text ):
1379
+ pass
1359
1380
def close (self ):
1360
1381
return ET .Element ("element" ) # simulate root
1361
- parser = ET .XMLParser (EchoTarget ())
1382
+ parser = ET .XMLParser (target = EchoTarget ())
1362
1383
parser .feed ("<element>some text</element>" )
1363
1384
self .assertEqual (parser .close ().tag , 'element' )
1364
1385
@@ -1908,7 +1929,12 @@ def test_find_through_ElementTree(self):
1908
1929
e = ET .XML (SAMPLE_XML )
1909
1930
self .assertEqual (ET .ElementTree (e ).find ('tag' ).tag , 'tag' )
1910
1931
self .assertEqual (ET .ElementTree (e ).find ('./tag' ).tag , 'tag' )
1911
- self .assertEqual (ET .ElementTree (e ).find ('/tag' ).tag , 'tag' )
1932
+ # this produces a warning
1933
+ msg = ("This search is broken in 1.3 and earlier, and will be fixed "
1934
+ "in a future version. If you rely on the current behaviour, "
1935
+ "change it to '.+'" )
1936
+ with support .check_warnings ((msg , FutureWarning )):
1937
+ self .assertEqual (ET .ElementTree (e ).find ('/tag' ).tag , 'tag' )
1912
1938
e [2 ] = ET .XML (SAMPLE_SECTION )
1913
1939
self .assertEqual (ET .ElementTree (e ).find ('section/tag' ).tag , 'tag' )
1914
1940
self .assertIsNone (ET .ElementTree (e ).find ('tog' ))
@@ -1919,14 +1945,15 @@ def test_find_through_ElementTree(self):
1919
1945
self .assertEqual (ET .ElementTree (e ).findtext ('tog/foo' , 'default' ),
1920
1946
'default' )
1921
1947
self .assertEqual (ET .ElementTree (e ).findtext ('./tag' ), 'text' )
1922
- self .assertEqual (ET .ElementTree (e ).findtext ('/tag' ), 'text' )
1948
+ with support .check_warnings ((msg , FutureWarning )):
1949
+ self .assertEqual (ET .ElementTree (e ).findtext ('/tag' ), 'text' )
1923
1950
self .assertEqual (ET .ElementTree (e ).findtext ('section/tag' ), 'subtext' )
1924
1951
1925
1952
self .assertEqual (summarize_list (ET .ElementTree (e ).findall ('./tag' )),
1926
1953
['tag' ] * 2 )
1927
- # this produces a warning
1928
- self . assertEqual ( summarize_list ( ET .ElementTree (e ).findall ('/tag' )),
1929
- ['tag' ] * 2 )
1954
+ with support . check_warnings (( msg , FutureWarning )):
1955
+ it = ET .ElementTree (e ).findall ('/tag' )
1956
+ self . assertEqual ( summarize_list ( it ), ['tag' ] * 2 )
1930
1957
1931
1958
1932
1959
class ElementIterTest (unittest .TestCase ):
@@ -2014,6 +2041,15 @@ def test_iter_by_tag(self):
2014
2041
self .assertEqual (self ._ilist (doc , '*' ), all_tags )
2015
2042
2016
2043
def test_getiterator (self ):
2044
+ # Element.getiterator() is deprecated.
2045
+ if sys .py3kwarning or ET is pyET :
2046
+ with support .check_warnings (("This method will be removed in future versions. "
2047
+ "Use .+ instead." , PendingDeprecationWarning )):
2048
+ self ._test_getiterator ()
2049
+ else :
2050
+ self ._test_getiterator ()
2051
+
2052
+ def _test_getiterator (self ):
2017
2053
doc = ET .XML ('''
2018
2054
<document>
2019
2055
<house>
@@ -2178,13 +2214,13 @@ def _check_sample_element(self, e):
2178
2214
def test_constructor_args (self ):
2179
2215
# Positional args. The first (html) is not supported, but should be
2180
2216
# nevertheless correctly accepted.
2181
- parser = ET .XMLParser (None , ET .TreeBuilder (), 'utf-8' )
2217
+ with support .check_py3k_warnings ((r'.*\bhtml\b' , DeprecationWarning )):
2218
+ parser = ET .XMLParser (None , ET .TreeBuilder (), 'utf-8' )
2182
2219
parser .feed (self .sample1 )
2183
2220
self ._check_sample_element (parser .close ())
2184
2221
2185
2222
# Now as keyword args.
2186
2223
parser2 = ET .XMLParser (encoding = 'utf-8' ,
2187
- html = [{}],
2188
2224
target = ET .TreeBuilder ())
2189
2225
parser2 .feed (self .sample1 )
2190
2226
self ._check_sample_element (parser2 .close ())
@@ -2593,44 +2629,6 @@ def test_correct_import_pyET(self):
2593
2629
# --------------------------------------------------------------------
2594
2630
2595
2631
2596
- class CleanContext (object ):
2597
- """Provide default namespace mapping and path cache."""
2598
- checkwarnings = None
2599
-
2600
- def __init__ (self , quiet = False ):
2601
- deprecations = (
2602
- ("This method of XMLParser is deprecated. Define doctype\(\) "
2603
- "method on the TreeBuilder target." , DeprecationWarning ),
2604
- # Search behaviour is broken if search path starts with "/".
2605
- ("This search is broken in 1.3 and earlier, and will be fixed "
2606
- "in a future version. If you rely on the current behaviour, "
2607
- "change it to '.+'" , FutureWarning ),
2608
- # Element.getchildren() and Element.getiterator() are deprecated.
2609
- ("This method will be removed in future versions. "
2610
- "Use .+ instead." , DeprecationWarning ),
2611
- ("This method will be removed in future versions. "
2612
- "Use .+ instead." , PendingDeprecationWarning ))
2613
- self .checkwarnings = support .check_warnings (* deprecations , quiet = quiet )
2614
-
2615
- def __enter__ (self ):
2616
- from xml .etree import ElementPath
2617
- self ._nsmap = pyET ._namespace_map
2618
- # Copy the default namespace mapping
2619
- self ._nsmap_copy = self ._nsmap .copy ()
2620
- # Copy the path cache (should be empty)
2621
- self ._path_cache = ElementPath ._cache
2622
- ElementPath ._cache = self ._path_cache .copy ()
2623
- self .checkwarnings .__enter__ ()
2624
-
2625
- def __exit__ (self , * args ):
2626
- from xml .etree import ElementPath
2627
- # Restore mapping and path cache
2628
- self ._nsmap .clear ()
2629
- self ._nsmap .update (self ._nsmap_copy )
2630
- ElementPath ._cache = self ._path_cache
2631
- self .checkwarnings .__exit__ (* args )
2632
-
2633
-
2634
2632
def test_main (module = None ):
2635
2633
# When invoked without a module, runs the Python ET tests by loading pyET.
2636
2634
# Otherwise, uses the given module as the ET.
@@ -2666,11 +2664,22 @@ def test_main(module=None):
2666
2664
NoAcceleratorTest ,
2667
2665
])
2668
2666
2667
+ # Provide default namespace mapping and path cache.
2668
+ from xml .etree import ElementPath
2669
+ nsmap = pyET ._namespace_map
2670
+ # Copy the default namespace mapping
2671
+ nsmap_copy = nsmap .copy ()
2672
+ # Copy the path cache (should be empty)
2673
+ path_cache = ElementPath ._cache
2674
+ ElementPath ._cache = path_cache .copy ()
2669
2675
try :
2670
- # XXX the C module should give the same warnings as the Python module
2671
- with CleanContext (quiet = (pyET is not ET )):
2672
- support .run_unittest (* test_classes )
2676
+ support .run_unittest (* test_classes )
2673
2677
finally :
2678
+ from xml .etree import ElementPath
2679
+ # Restore mapping and path cache
2680
+ nsmap .clear ()
2681
+ nsmap .update (nsmap_copy )
2682
+ ElementPath ._cache = path_cache
2674
2683
# don't interfere with subsequent tests
2675
2684
ET = None
2676
2685
0 commit comments