From 0619ae798d548109180178eda0ec406d725f7d9e Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Wed, 27 Oct 2010 18:37:51 +0000 Subject: Merged revisions 85858 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r85858 | antoine.pitrou | 2010-10-27 20:33:30 +0200 (mer., 27 oct. 2010) | 5 lines Issue #5027: The standard `xml` namespace is now understood by xml.sax.saxutils.XMLGenerator as being bound to http://www.w3.org/XML/1998/namespace. Patch by Troy J. Farrell. ........ --- Lib/test/test_sax.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ Lib/xml/sax/saxutils.py | 6 ++++++ Misc/ACKS | 1 + Misc/NEWS | 4 ++++ 4 files changed, 66 insertions(+) diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py index a45d1f3..ab54ed5 100644 --- a/Lib/test/test_sax.py +++ b/Lib/test/test_sax.py @@ -11,6 +11,7 @@ except SAXReaderNotAvailable: from xml.sax.saxutils import XMLGenerator, escape, unescape, quoteattr, \ XMLFilterBase from xml.sax.expatreader import create_parser +from xml.sax.handler import feature_namespaces from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl from io import StringIO from test.support import findfile, run_unittest @@ -288,6 +289,60 @@ class XmlgenTest(unittest.TestCase): self.assertEquals(result.getvalue(), start+'') + def test_5027_1(self): + # The xml prefix (as in xml:lang below) is reserved and bound by + # definition to http://www.w3.org/XML/1998/namespace. XMLGenerator had + # a bug whereby a KeyError is thrown because this namespace is missing + # from a dictionary. + # + # This test demonstrates the bug by parsing a document. + test_xml = StringIO( + '' + '' + 'Hello' + '') + + parser = make_parser() + parser.setFeature(feature_namespaces, True) + result = StringIO() + gen = XMLGenerator(result) + parser.setContentHandler(gen) + parser.parse(test_xml) + + self.assertEquals(result.getvalue(), + start + ( + '' + 'Hello' + '')) + + def test_5027_2(self): + # The xml prefix (as in xml:lang below) is reserved and bound by + # definition to http://www.w3.org/XML/1998/namespace. XMLGenerator had + # a bug whereby a KeyError is thrown because this namespace is missing + # from a dictionary. + # + # This test demonstrates the bug by direct manipulation of the + # XMLGenerator. + result = StringIO() + gen = XMLGenerator(result) + + gen.startDocument() + gen.startPrefixMapping('a', 'http://example.com/ns') + gen.startElementNS(('http://example.com/ns', 'g1'), 'g1', {}) + lang_attr = {('http://www.w3.org/XML/1998/namespace', 'lang'): 'en'} + gen.startElementNS(('http://example.com/ns', 'g2'), 'g2', lang_attr) + gen.characters('Hello') + gen.endElementNS(('http://example.com/ns', 'g2'), 'g2') + gen.endElementNS(('http://example.com/ns', 'g1'), 'g1') + gen.endPrefixMapping('a') + gen.endDocument() + + self.assertEquals(result.getvalue(), + start + ( + '' + 'Hello' + '')) + class XMLFilterBaseTest(unittest.TestCase): def test_filter_basic(self): diff --git a/Lib/xml/sax/saxutils.py b/Lib/xml/sax/saxutils.py index e845015..b05e815 100644 --- a/Lib/xml/sax/saxutils.py +++ b/Lib/xml/sax/saxutils.py @@ -98,6 +98,12 @@ class XMLGenerator(handler.ContentHandler): def _qname(self, name): """Builds a qualified name from a (ns_url, localname) pair""" if name[0]: + # Per http://www.w3.org/XML/1998/namespace, The 'xml' prefix is + # bound by definition to http://www.w3.org/XML/1998/namespace. It + # does not need to be declared and will not usually be found in + # self._current_context. + if 'http://www.w3.org/XML/1998/namespace' == name[0]: + return 'xml:' + name[1] # The name is in a non-empty namespace prefix = self._current_context[name[0]] if prefix: diff --git a/Misc/ACKS b/Misc/ACKS index 01d18ec..ea6f1a1 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -237,6 +237,7 @@ Greg Ewing Martijn Faassen Andreas Faerber Bill Fancher +Troy J. Farrell Mark Favas Niels Ferguson Sebastian Fernandez diff --git a/Misc/NEWS b/Misc/NEWS index 0b43d19..b92cdc8 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -137,6 +137,10 @@ C-API Library ------- +- Issue #5027: The standard ``xml`` namespace is now understood by + xml.sax.saxutils.XMLGenerator as being bound to + http://www.w3.org/XML/1998/namespace. Patch by Troy J. Farrell. + - #7761: telnetlib.interact failures on Windows fixed. - Issue #5117: Case normalization was needed on ntpath.relpath(). And -- cgit v0.12