summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_sax.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-10-27 18:33:30 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2010-10-27 18:33:30 (GMT)
commit6b03ee603315ac9e69332c8f2d064de0a98c3f15 (patch)
tree062ff0d1fa4816dd12137b65901f87a9f22c538d /Lib/test/test_sax.py
parentc1a68363057b7b939fccbe35ab5f9d288e0ef6c1 (diff)
downloadcpython-6b03ee603315ac9e69332c8f2d064de0a98c3f15.zip
cpython-6b03ee603315ac9e69332c8f2d064de0a98c3f15.tar.gz
cpython-6b03ee603315ac9e69332c8f2d064de0a98c3f15.tar.bz2
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.
Diffstat (limited to 'Lib/test/test_sax.py')
-rw-r--r--Lib/test/test_sax.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py
index 143ddf2..c4b211f 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
@@ -385,6 +386,60 @@ class XmlgenTest(unittest.TestCase):
self.assertEquals(result.getvalue(),
start+'<my:a xmlns:my="qux" b="c"/>')
+ 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(
+ '<?xml version="1.0"?>'
+ '<a:g1 xmlns:a="http://example.com/ns">'
+ '<a:g2 xml:lang="en">Hello</a:g2>'
+ '</a:g1>')
+
+ 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 + (
+ '<a:g1 xmlns:a="http://example.com/ns">'
+ '<a:g2 xml:lang="en">Hello</a:g2>'
+ '</a:g1>'))
+
+ 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 + (
+ '<a:g1 xmlns:a="http://example.com/ns">'
+ '<a:g2 xml:lang="en">Hello</a:g2>'
+ '</a:g1>'))
+
class XMLFilterBaseTest(unittest.TestCase):
def test_filter_basic(self):