diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2018-07-24 09:03:34 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-24 09:03:34 (GMT) |
commit | 02ec92fa7b1dddc23d479ee0b87dc283793505a8 (patch) | |
tree | 4fc90891da617efbc74239d9dc62eef44f3aa5ff /Lib | |
parent | c5734998d91e9953fd179ba6ed7015b6343e8191 (diff) | |
download | cpython-02ec92fa7b1dddc23d479ee0b87dc283793505a8.zip cpython-02ec92fa7b1dddc23d479ee0b87dc283793505a8.tar.gz cpython-02ec92fa7b1dddc23d479ee0b87dc283793505a8.tar.bz2 |
bpo-29209: Remove old-deprecated features in ElementTree. (GH-6769)
Also make getchildren() and getiterator() emitting
a DeprecationWarning instead of PendingDeprecationWarning.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_xml_etree.py | 31 | ||||
-rw-r--r-- | Lib/xml/etree/ElementTree.py | 40 |
2 files changed, 17 insertions, 54 deletions
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index e113975..a525290 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -706,7 +706,7 @@ class ElementTreeTest(unittest.TestCase): # Element.getchildren() and ElementTree.getiterator() are deprecated. @checkwarnings(("This method will be removed in future versions. " "Use .+ instead.", - (DeprecationWarning, PendingDeprecationWarning))) + DeprecationWarning)) def test_getchildren(self): # Test Element.getchildren() @@ -2399,7 +2399,7 @@ class ElementIterTest(unittest.TestCase): # Element.getiterator() is deprecated. @checkwarnings(("This method will be removed in future versions. " - "Use .+ instead.", PendingDeprecationWarning)) + "Use .+ instead.", DeprecationWarning)) def test_getiterator(self): doc = ET.XML(''' <document> @@ -2605,14 +2605,6 @@ class XMLParserTest(unittest.TestCase): self.assertEqual(e[0].text, '22') def test_constructor_args(self): - # Positional args. The first (html) is not supported, but should be - # nevertheless correctly accepted. - with self.assertWarnsRegex(DeprecationWarning, r'\bhtml\b'): - parser = ET.XMLParser(None, ET.TreeBuilder(), 'utf-8') - parser.feed(self.sample1) - self._check_sample_element(parser.close()) - - # Now as keyword args. parser2 = ET.XMLParser(encoding='utf-8', target=ET.TreeBuilder()) parser2.feed(self.sample1) @@ -2626,13 +2618,6 @@ class XMLParserTest(unittest.TestCase): self._check_sample_element(parser.close()) def test_doctype_warning(self): - parser = ET.XMLParser() - with self.assertWarns(DeprecationWarning): - parser.doctype('html', '-//W3C//DTD XHTML 1.0 Transitional//EN', - 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd') - parser.feed('<html/>') - parser.close() - with warnings.catch_warnings(): warnings.simplefilter('error', DeprecationWarning) parser = ET.XMLParser() @@ -2642,21 +2627,20 @@ class XMLParserTest(unittest.TestCase): def test_subclass_doctype(self): _doctype = None class MyParserWithDoctype(ET.XMLParser): - def doctype(self, name, pubid, system): + def doctype(self, *args, **kwargs): nonlocal _doctype - _doctype = (name, pubid, system) + _doctype = (args, kwargs) parser = MyParserWithDoctype() - with self.assertWarns(DeprecationWarning): + with self.assertWarnsRegex(RuntimeWarning, 'doctype'): parser.feed(self.sample2) parser.close() - self.assertEqual(_doctype, - ('html', '-//W3C//DTD XHTML 1.0 Transitional//EN', - 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd')) + self.assertIsNone(_doctype) _doctype = _doctype2 = None with warnings.catch_warnings(): warnings.simplefilter('error', DeprecationWarning) + warnings.simplefilter('error', RuntimeWarning) class DoctypeParser: def doctype(self, name, pubid, system): nonlocal _doctype2 @@ -2674,6 +2658,7 @@ class XMLParserTest(unittest.TestCase): '''Ensure that ordinary usage is not deprecated (Issue 19176)''' with warnings.catch_warnings(): warnings.simplefilter('error', DeprecationWarning) + warnings.simplefilter('error', RuntimeWarning) class MyParserWithoutDoctype(ET.XMLParser): pass parser = MyParserWithoutDoctype() diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index 8727704..371b371 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -412,11 +412,10 @@ class Element: # compatibility def getiterator(self, tag=None): - # Change for a DeprecationWarning in 1.4 warnings.warn( "This method will be removed in future versions. " "Use 'elem.iter()' or 'list(elem.iter())' instead.", - PendingDeprecationWarning, stacklevel=2 + DeprecationWarning, stacklevel=2 ) return list(self.iter(tag)) @@ -622,11 +621,10 @@ class ElementTree: # compatibility def getiterator(self, tag=None): - # Change for a DeprecationWarning in 1.4 warnings.warn( "This method will be removed in future versions. " "Use 'tree.iter()' or 'list(tree.iter())' instead.", - PendingDeprecationWarning, stacklevel=2 + DeprecationWarning, stacklevel=2 ) return list(self.iter(tag)) @@ -1431,13 +1429,11 @@ class TreeBuilder: self._tail = 1 return self._last -_sentinel = ['sentinel'] # also see ElementTree and TreeBuilder class XMLParser: """Element structure builder for XML source data based on the expat parser. - *html* are predefined HTML entities (deprecated and not supported), *target* is an optional target object which defaults to an instance of the standard TreeBuilder class, *encoding* is an optional encoding string which if given, overrides the encoding specified in the XML file: @@ -1445,11 +1441,7 @@ class XMLParser: """ - def __init__(self, html=_sentinel, target=None, encoding=None): - if html is not _sentinel: - warnings.warn( - "The html argument of XMLParser() is deprecated", - DeprecationWarning, stacklevel=2) + def __init__(self, *, target=None, encoding=None): try: from xml.parsers import expat except ImportError: @@ -1602,27 +1594,13 @@ class XMLParser: return if hasattr(self.target, "doctype"): self.target.doctype(name, pubid, system[1:-1]) - elif self.doctype != self._XMLParser__doctype: - # warn about deprecated call - self._XMLParser__doctype(name, pubid, system[1:-1]) - self.doctype(name, pubid, system[1:-1]) - self._doctype = None - - def doctype(self, name, pubid, system): - """(Deprecated) Handle doctype declaration - - *name* is the Doctype name, *pubid* is the public identifier, - and *system* is the system identifier. + elif hasattr(self, "doctype"): + warnings.warn( + "The doctype() method of XMLParser is ignored. " + "Define doctype() method on the TreeBuilder target.", + RuntimeWarning) - """ - warnings.warn( - "This method of XMLParser is deprecated. Define doctype() " - "method on the TreeBuilder target.", - DeprecationWarning, - ) - - # sentinel, if doctype is redefined in a subclass - __doctype = doctype + self._doctype = None def feed(self, data): """Feed encoded data to parser.""" |