Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 02ec92f

Browse files
bpo-29209: Remove old-deprecated features in ElementTree. (GH-6769)
Also make getchildren() and getiterator() emitting a DeprecationWarning instead of PendingDeprecationWarning.
1 parent c573499 commit 02ec92f

7 files changed

Lines changed: 73 additions & 182 deletions

File tree

Doc/library/xml.etree.elementtree.rst

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -772,13 +772,13 @@ Element Objects
772772

773773
.. method:: getchildren()
774774

775-
.. deprecated:: 3.2
775+
.. deprecated-removed:: 3.2 3.9
776776
Use ``list(elem)`` or iteration.
777777

778778

779779
.. method:: getiterator(tag=None)
780780

781-
.. deprecated:: 3.2
781+
.. deprecated-removed:: 3.2 3.9
782782
Use method :meth:`Element.iter` instead.
783783

784784

@@ -888,7 +888,7 @@ ElementTree Objects
888888

889889
.. method:: getiterator(tag=None)
890890

891-
.. deprecated:: 3.2
891+
.. deprecated-removed:: 3.2 3.9
892892
Use method :meth:`ElementTree.iter` instead.
893893

894894

@@ -1050,20 +1050,20 @@ XMLParser Objects
10501050
^^^^^^^^^^^^^^^^^
10511051

10521052

1053-
.. class:: XMLParser(html=0, target=None, encoding=None)
1053+
.. class:: XMLParser(*, target=None, encoding=None)
10541054

10551055
This class is the low-level building block of the module. It uses
10561056
:mod:`xml.parsers.expat` for efficient, event-based parsing of XML. It can
10571057
be fed XML data incrementally with the :meth:`feed` method, and parsing
10581058
events are translated to a push API - by invoking callbacks on the *target*
10591059
object. If *target* is omitted, the standard :class:`TreeBuilder` is used.
1060-
The *html* argument was historically used for backwards compatibility and is
1061-
now deprecated. If *encoding* [1]_ is given, the value overrides the
1060+
If *encoding* [1]_ is given, the value overrides the
10621061
encoding specified in the XML file.
10631062

1064-
.. deprecated:: 3.4
1065-
The *html* argument. The remaining arguments should be passed via
1066-
keyword to prepare for the removal of the *html* argument.
1063+
.. versionchanged:: 3.8
1064+
Parameters are now :ref:`keyword-only <keyword-only_parameter>`.
1065+
The *html* argument no longer supported.
1066+
10671067

10681068
.. method:: close()
10691069

@@ -1072,13 +1072,6 @@ XMLParser Objects
10721072
this is the toplevel document element.
10731073

10741074

1075-
.. method:: doctype(name, pubid, system)
1076-
1077-
.. deprecated:: 3.2
1078-
Define the :meth:`TreeBuilder.doctype` method on a custom TreeBuilder
1079-
target.
1080-
1081-
10821075
.. method:: feed(data)
10831076

10841077
Feeds data to the parser. *data* is encoded data.

Doc/whatsnew/3.8.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ Build and C API Changes
158158
Deprecated
159159
==========
160160

161+
* Deprecated methods ``getchildren()`` and ``getiterator()`` in
162+
the :mod:`~xml.etree.ElementTree` module emit now a
163+
:exc:`DeprecationWarning` instead of :exc:`PendingDeprecationWarning`.
164+
They will be removed in Python 3.9.
165+
(Contributed by Serhiy Storchaka in :issue:`29209`.)
161166

162167

163168
Removed
@@ -173,6 +178,14 @@ Removed
173178
* ``filemode`` function is removed from :mod:`tarfile` module.
174179
It is not documented and deprecated since Python 3.3.
175180

181+
* The :class:`~xml.etree.ElementTree.XMLParser` constructor no longer accepts
182+
the *html* argument. It never had effect and was deprecated in Python 3.4.
183+
All other parameters are now :ref:`keyword-only <keyword-only_parameter>`.
184+
(Contributed by Serhiy Storchaka in :issue:`29209`.)
185+
186+
* Removed the ``doctype()`` method of :class:`~xml.etree.ElementTree.XMLParser`.
187+
(Contributed by Serhiy Storchaka in :issue:`29209`.)
188+
176189

177190
Porting to Python 3.8
178191
=====================
@@ -204,6 +217,13 @@ Changes in the Python API
204217
a database if it does not exist.
205218
(Contributed by Serhiy Storchaka in :issue:`32749`.)
206219

220+
* The ``doctype()`` method defined in a subclass of
221+
:class:`~xml.etree.ElementTree.XMLParser` will no longer be called and will
222+
cause emitting a :exc:`RuntimeWarning` instead of a :exc:`DeprecationWarning`.
223+
Define the :meth:`doctype() <xml.etree.ElementTree.TreeBuilder.doctype>`
224+
method on a target for handling an XML doctype declaration.
225+
(Contributed by Serhiy Storchaka in :issue:`29209`.)
226+
207227
* A :exc:`RuntimeError` is now raised when the custom metaclass doesn't
208228
provide the ``__classcell__`` entry in the namespace passed to
209229
``type.__new__``. A :exc:`DeprecationWarning` was emitted in Python

Lib/test/test_xml_etree.py

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ def comment(self, data):
706706
# Element.getchildren() and ElementTree.getiterator() are deprecated.
707707
@checkwarnings(("This method will be removed in future versions. "
708708
"Use .+ instead.",
709-
(DeprecationWarning, PendingDeprecationWarning)))
709+
DeprecationWarning))
710710
def test_getchildren(self):
711711
# Test Element.getchildren()
712712

@@ -2399,7 +2399,7 @@ def test_iter_by_tag(self):
23992399

24002400
# Element.getiterator() is deprecated.
24012401
@checkwarnings(("This method will be removed in future versions. "
2402-
"Use .+ instead.", PendingDeprecationWarning))
2402+
"Use .+ instead.", DeprecationWarning))
24032403
def test_getiterator(self):
24042404
doc = ET.XML('''
24052405
<document>
@@ -2605,14 +2605,6 @@ def _check_sample_element(self, e):
26052605
self.assertEqual(e[0].text, '22')
26062606

26072607
def test_constructor_args(self):
2608-
# Positional args. The first (html) is not supported, but should be
2609-
# nevertheless correctly accepted.
2610-
with self.assertWarnsRegex(DeprecationWarning, r'\bhtml\b'):
2611-
parser = ET.XMLParser(None, ET.TreeBuilder(), 'utf-8')
2612-
parser.feed(self.sample1)
2613-
self._check_sample_element(parser.close())
2614-
2615-
# Now as keyword args.
26162608
parser2 = ET.XMLParser(encoding='utf-8',
26172609
target=ET.TreeBuilder())
26182610
parser2.feed(self.sample1)
@@ -2626,13 +2618,6 @@ class MyParser(ET.XMLParser):
26262618
self._check_sample_element(parser.close())
26272619

26282620
def test_doctype_warning(self):
2629-
parser = ET.XMLParser()
2630-
with self.assertWarns(DeprecationWarning):
2631-
parser.doctype('html', '-//W3C//DTD XHTML 1.0 Transitional//EN',
2632-
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd')
2633-
parser.feed('<html/>')
2634-
parser.close()
2635-
26362621
with warnings.catch_warnings():
26372622
warnings.simplefilter('error', DeprecationWarning)
26382623
parser = ET.XMLParser()
@@ -2642,21 +2627,20 @@ def test_doctype_warning(self):
26422627
def test_subclass_doctype(self):
26432628
_doctype = None
26442629
class MyParserWithDoctype(ET.XMLParser):
2645-
def doctype(self, name, pubid, system):
2630+
def doctype(self, *args, **kwargs):
26462631
nonlocal _doctype
2647-
_doctype = (name, pubid, system)
2632+
_doctype = (args, kwargs)
26482633

26492634
parser = MyParserWithDoctype()
2650-
with self.assertWarns(DeprecationWarning):
2635+
with self.assertWarnsRegex(RuntimeWarning, 'doctype'):
26512636
parser.feed(self.sample2)
26522637
parser.close()
2653-
self.assertEqual(_doctype,
2654-
('html', '-//W3C//DTD XHTML 1.0 Transitional//EN',
2655-
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'))
2638+
self.assertIsNone(_doctype)
26562639

26572640
_doctype = _doctype2 = None
26582641
with warnings.catch_warnings():
26592642
warnings.simplefilter('error', DeprecationWarning)
2643+
warnings.simplefilter('error', RuntimeWarning)
26602644
class DoctypeParser:
26612645
def doctype(self, name, pubid, system):
26622646
nonlocal _doctype2
@@ -2674,6 +2658,7 @@ def test_inherited_doctype(self):
26742658
'''Ensure that ordinary usage is not deprecated (Issue 19176)'''
26752659
with warnings.catch_warnings():
26762660
warnings.simplefilter('error', DeprecationWarning)
2661+
warnings.simplefilter('error', RuntimeWarning)
26772662
class MyParserWithoutDoctype(ET.XMLParser):
26782663
pass
26792664
parser = MyParserWithoutDoctype()

Lib/xml/etree/ElementTree.py

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,10 @@ def iter(self, tag=None):
412412

413413
# compatibility
414414
def getiterator(self, tag=None):
415-
# Change for a DeprecationWarning in 1.4
416415
warnings.warn(
417416
"This method will be removed in future versions. "
418417
"Use 'elem.iter()' or 'list(elem.iter())' instead.",
419-
PendingDeprecationWarning, stacklevel=2
418+
DeprecationWarning, stacklevel=2
420419
)
421420
return list(self.iter(tag))
422421

@@ -622,11 +621,10 @@ def iter(self, tag=None):
622621

623622
# compatibility
624623
def getiterator(self, tag=None):
625-
# Change for a DeprecationWarning in 1.4
626624
warnings.warn(
627625
"This method will be removed in future versions. "
628626
"Use 'tree.iter()' or 'list(tree.iter())' instead.",
629-
PendingDeprecationWarning, stacklevel=2
627+
DeprecationWarning, stacklevel=2
630628
)
631629
return list(self.iter(tag))
632630

@@ -1431,25 +1429,19 @@ def end(self, tag):
14311429
self._tail = 1
14321430
return self._last
14331431

1434-
_sentinel = ['sentinel']
14351432

14361433
# also see ElementTree and TreeBuilder
14371434
class XMLParser:
14381435
"""Element structure builder for XML source data based on the expat parser.
14391436
1440-
*html* are predefined HTML entities (deprecated and not supported),
14411437
*target* is an optional target object which defaults to an instance of the
14421438
standard TreeBuilder class, *encoding* is an optional encoding string
14431439
which if given, overrides the encoding specified in the XML file:
14441440
http://www.iana.org/assignments/character-sets
14451441
14461442
"""
14471443

1448-
def __init__(self, html=_sentinel, target=None, encoding=None):
1449-
if html is not _sentinel:
1450-
warnings.warn(
1451-
"The html argument of XMLParser() is deprecated",
1452-
DeprecationWarning, stacklevel=2)
1444+
def __init__(self, *, target=None, encoding=None):
14531445
try:
14541446
from xml.parsers import expat
14551447
except ImportError:
@@ -1602,27 +1594,13 @@ def _default(self, text):
16021594
return
16031595
if hasattr(self.target, "doctype"):
16041596
self.target.doctype(name, pubid, system[1:-1])
1605-
elif self.doctype != self._XMLParser__doctype:
1606-
# warn about deprecated call
1607-
self._XMLParser__doctype(name, pubid, system[1:-1])
1608-
self.doctype(name, pubid, system[1:-1])
1609-
self._doctype = None
1610-
1611-
def doctype(self, name, pubid, system):
1612-
"""(Deprecated) Handle doctype declaration
1613-
1614-
*name* is the Doctype name, *pubid* is the public identifier,
1615-
and *system* is the system identifier.
1597+
elif hasattr(self, "doctype"):
1598+
warnings.warn(
1599+
"The doctype() method of XMLParser is ignored. "
1600+
"Define doctype() method on the TreeBuilder target.",
1601+
RuntimeWarning)
16161602

1617-
"""
1618-
warnings.warn(
1619-
"This method of XMLParser is deprecated. Define doctype() "
1620-
"method on the TreeBuilder target.",
1621-
DeprecationWarning,
1622-
)
1623-
1624-
# sentinel, if doctype is redefined in a subclass
1625-
__doctype = doctype
1603+
self._doctype = None
16261604

16271605
def feed(self, data):
16281606
"""Feed encoded data to parser."""
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Removed the ``doctype()`` method and the *html* parameter of the constructor
2+
of :class:`~xml.etree.ElementTree.XMLParser`. The ``doctype()`` method
3+
defined in a subclass will no longer be called. Deprecated methods
4+
``getchildren()`` and ``getiterator()`` in the :mod:`~xml.etree.ElementTree`
5+
module emit now a :exc:`DeprecationWarning` instead of
6+
:exc:`PendingDeprecationWarning`.

0 commit comments

Comments
 (0)