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
2 changes: 1 addition & 1 deletion Doc/reference/compound_stmts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ The match statement is used for pattern matching. Syntax:
match_stmt: 'match' `subject_expr` ":" NEWLINE INDENT `case_block`+ DEDENT
subject_expr: `flexible_expression` "," [`flexible_expression_list` [',']]
: | `assignment_expression`
case_block: 'case' `patterns` [`guard`] ":" `!block`
case_block: 'case' `patterns` [`guard`] ":" `suite`

.. note::
This section uses single quotes to denote
Expand Down
26 changes: 26 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -3663,6 +3663,32 @@ def test_basic(self):
doc = ET.XML("<root>a&amp;<sub>b&amp;</sub>c&amp;</root>")
self.assertEqual(''.join(doc.itertext()), 'a&b&c&')

def test_comment(self):
e = ET.Element('root')
e.text = 'before'
comment = ET.Comment('comment')
self.assertEqual(comment.text, 'comment')
comment.tail = 'after'
e.append(comment)
self.assertEqual(''.join(e.itertext()), 'beforeafter')
self.assertEqual(list(e.iter()), [e, comment])
self.assertEqual(list(e.iter('root')), [e])
self.assertEqual(''.join(comment.itertext()), '')
self.assertEqual(list(comment.iter()), [comment])

def test_processinginstruction(self):
e = ET.Element('root')
e.text = 'before'
pi = ET.PI('test', 'instruction')
self.assertEqual(pi.text, 'test instruction')
pi.tail = 'after'
e.append(pi)
self.assertEqual(''.join(e.itertext()), 'beforeafter')
self.assertEqual(list(e.iter()), [e, pi])
self.assertEqual(list(e.iter('root')), [e])
self.assertEqual(''.join(pi.itertext()), '')
self.assertEqual(list(pi.iter()), [pi])

def test_corners(self):
# single root, no subelements
a = ET.Element('a')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix the C implementation of :meth:`xml.etree.ElementTree.Element.itertext`:
it no longer emits text for comments and processing instructions.
4 changes: 4 additions & 0 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -2297,6 +2297,10 @@ elementiter_next(PyObject *op)
return NULL;
}
if (it->gettext) {
if (elem->tag != Py_None && !PyUnicode_Check(elem->tag)) {
Py_DECREF(elem);
continue;
}
text = element_get_text(elem);
goto gettext;
}
Expand Down
Loading