Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -4002,6 +4002,21 @@ def close(self):
('html', '-//W3C//DTD XHTML 1.0 Transitional//EN',
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'))

for doctype, expected in [
('<!DOCTYPE html>', ('html', None, None)),
('<!DOCTYPE html [<!ENTITY e "v">]>', ('html', None, None)),
('<!DOCTYPE html SYSTEM "a.dtd">', ('html', None, 'a.dtd')),
('<!DOCTYPE html SYSTEM "a.dtd" [<!ENTITY e "v">]>',
('html', None, 'a.dtd')),
('<!DOCTYPE html PUBLIC "-//P" "a.dtd">', ('html', '-//P', 'a.dtd')),
("<!DOCTYPE\nhtml\nPUBLIC\n'-//P'\n'a.dtd'\n>",
('html', '-//P', 'a.dtd')),
]:
with self.subTest(doctype=doctype):
parser = ET.XMLParser(target=DoctypeParser())
parser.feed(doctype + '<html/>')
self.assertEqual(parser.close(), expected)

def test_builder_lookup_errors(self):
class RaisingBuilder:
def __init__(self, raise_in=None, what=ValueError):
Expand Down
43 changes: 10 additions & 33 deletions Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -1572,10 +1572,10 @@ def __init__(self, *, target=None, encoding=None):
parser.CommentHandler = target.comment
if hasattr(target, 'pi'):
parser.ProcessingInstructionHandler = target.pi
parser.StartDoctypeDeclHandler = self._start_doctype
# Configure pyexpat: buffering, new-style attribute handling.
parser.buffer_text = 1
parser.ordered_attributes = 1
self._doctype = None
self.entity = {}
try:
self.version = "Expat %d.%d.%d" % expat.version_info
Expand Down Expand Up @@ -1694,38 +1694,15 @@ def _default(self, text):
err.lineno = self.parser.ErrorLineNumber
err.offset = self.parser.ErrorColumnNumber
raise err
elif prefix == "<" and text[:9] == "<!DOCTYPE":
self._doctype = [] # inside a doctype declaration
elif self._doctype is not None:
# parse doctype contents
if prefix == ">":
self._doctype = None
return
text = text.strip(_XML_WHITESPACE)
if not text:
return
self._doctype.append(text)
n = len(self._doctype)
if n > 2:
type = self._doctype[1]
if type == "PUBLIC" and n == 4:
name, type, pubid, system = self._doctype
if pubid:
pubid = pubid[1:-1]
elif type == "SYSTEM" and n == 3:
name, type, system = self._doctype
pubid = None
else:
return
if hasattr(self.target, "doctype"):
self.target.doctype(name, pubid, system[1:-1])
elif hasattr(self, "doctype"):
warnings.warn(
"The doctype() method of XMLParser is ignored. "
"Define doctype() method on the TreeBuilder target.",
RuntimeWarning)

self._doctype = None

def _start_doctype(self, name, system, pubid, has_internal_subset):
if hasattr(self.target, "doctype"):
self.target.doctype(name, pubid, system)
elif hasattr(self, "doctype"):
warnings.warn(
"The doctype() method of XMLParser is ignored. "
"Define doctype() method on the TreeBuilder target.",
RuntimeWarning)

def feed(self, data):
"""Feed encoded data to parser."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix the Python implementation of :class:`xml.etree.ElementTree.XMLParser`:
the ``doctype()`` method of the target is now called for a document type
declaration without an external identifier, like ``<!DOCTYPE html>``,
as in the C implementation.
Loading