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

Skip to content

Commit b9cd72a

Browse files
committed
#5762: fix handling of empty namespace in minidom, which would result in AttributeError on toxml().
1 parent d4460aa commit b9cd72a

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

Lib/test/test_minidom.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,13 @@ def testSerializeCommentNodeWithDoubleHyphen(self):
14891489
doc.appendChild(doc.createComment("foo--bar"))
14901490
self.assertRaises(ValueError, doc.toxml)
14911491

1492+
def testEmptyXMLNSValue(self):
1493+
doc = parseString("<element xmlns=''>\n"
1494+
"<foo/>\n</element>")
1495+
doc2 = parseString(doc.toxml())
1496+
self.confirm(doc2.namespaceURI == xml.dom.EMPTY_NAMESPACE)
1497+
1498+
14921499
def test_main():
14931500
run_unittest(MinidomTest)
14941501

Lib/xml/dom/minidom.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,10 @@ def _in_document(node):
301301

302302
def _write_data(writer, data):
303303
"Writes datachars to writer."
304-
data = data.replace("&", "&amp;").replace("<", "&lt;")
305-
data = data.replace("\"", "&quot;").replace(">", "&gt;")
306-
writer.write(data)
304+
if data:
305+
data = data.replace("&", "&amp;").replace("<", "&lt;"). \
306+
replace("\"", "&quot;").replace(">", "&gt;")
307+
writer.write(data)
307308

308309
def _get_elements_by_tagName_helper(parent, name, rc):
309310
for node in parent.childNodes:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Core and Builtins
2424
Library
2525
-------
2626

27+
- Issue #5762: Fix AttributeError raised by ``xml.dom.minidom`` when an empty
28+
XML namespace attribute is encountered.
29+
2730
- Issue #2830: Add the ``html.escape()`` function, which quotes all problematic
2831
characters by default. Deprecate ``cgi.escape()``.
2932

0 commit comments

Comments
 (0)