Commit 7f08102b authored by Antoine Pitrou's avatar Antoine Pitrou

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.
........
parent e1ff2141
...@@ -11,6 +11,7 @@ except SAXReaderNotAvailable: ...@@ -11,6 +11,7 @@ except SAXReaderNotAvailable:
from xml.sax.saxutils import XMLGenerator, escape, unescape, quoteattr, \ from xml.sax.saxutils import XMLGenerator, escape, unescape, quoteattr, \
XMLFilterBase XMLFilterBase
from xml.sax.expatreader import create_parser from xml.sax.expatreader import create_parser
from xml.sax.handler import feature_namespaces
from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl
from cStringIO import StringIO from cStringIO import StringIO
from test.test_support import findfile, run_unittest from test.test_support import findfile, run_unittest
...@@ -290,6 +291,60 @@ class XmlgenTest(unittest.TestCase): ...@@ -290,6 +291,60 @@ class XmlgenTest(unittest.TestCase):
self.assertEquals(result.getvalue(), self.assertEquals(result.getvalue(),
start+'<my:a xmlns:my="qux" b="c"></my:a>') start+'<my:a xmlns:my="qux" b="c"></my:a>')
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): class XMLFilterBaseTest(unittest.TestCase):
def test_filter_basic(self): def test_filter_basic(self):
......
...@@ -103,6 +103,12 @@ class XMLGenerator(handler.ContentHandler): ...@@ -103,6 +103,12 @@ class XMLGenerator(handler.ContentHandler):
def _qname(self, name): def _qname(self, name):
"""Builds a qualified name from a (ns_url, localname) pair""" """Builds a qualified name from a (ns_url, localname) pair"""
if name[0]: 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 # The name is in a non-empty namespace
prefix = self._current_context[name[0]] prefix = self._current_context[name[0]]
if prefix: if prefix:
......
...@@ -242,6 +242,7 @@ Greg Ewing ...@@ -242,6 +242,7 @@ Greg Ewing
Martijn Faassen Martijn Faassen
Andreas Faerber Andreas Faerber
Bill Fancher Bill Fancher
Troy J. Farrell
Mark Favas Mark Favas
Niels Ferguson Niels Ferguson
Sebastian Fernandez Sebastian Fernandez
......
...@@ -63,6 +63,10 @@ Core and Builtins ...@@ -63,6 +63,10 @@ Core and Builtins
Library 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.
- Issue #10163: Skip unreadable registry keys during mimetypes - Issue #10163: Skip unreadable registry keys during mimetypes
initialization. initialization.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment