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

Skip to content

Commit 6b03ee6

Browse files
committed
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.
1 parent c1a6836 commit 6b03ee6

4 files changed

Lines changed: 66 additions & 0 deletions

File tree

Lib/test/test_sax.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from xml.sax.saxutils import XMLGenerator, escape, unescape, quoteattr, \
1212
XMLFilterBase
1313
from xml.sax.expatreader import create_parser
14+
from xml.sax.handler import feature_namespaces
1415
from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl
1516
from io import StringIO
1617
from test.support import findfile, run_unittest
@@ -385,6 +386,60 @@ def test_1463026_3_empty(self):
385386
self.assertEquals(result.getvalue(),
386387
start+'<my:a xmlns:my="qux" b="c"/>')
387388

389+
def test_5027_1(self):
390+
# The xml prefix (as in xml:lang below) is reserved and bound by
391+
# definition to http://www.w3.org/XML/1998/namespace. XMLGenerator had
392+
# a bug whereby a KeyError is thrown because this namespace is missing
393+
# from a dictionary.
394+
#
395+
# This test demonstrates the bug by parsing a document.
396+
test_xml = StringIO(
397+
'<?xml version="1.0"?>'
398+
'<a:g1 xmlns:a="http://example.com/ns">'
399+
'<a:g2 xml:lang="en">Hello</a:g2>'
400+
'</a:g1>')
401+
402+
parser = make_parser()
403+
parser.setFeature(feature_namespaces, True)
404+
result = StringIO()
405+
gen = XMLGenerator(result)
406+
parser.setContentHandler(gen)
407+
parser.parse(test_xml)
408+
409+
self.assertEquals(result.getvalue(),
410+
start + (
411+
'<a:g1 xmlns:a="http://example.com/ns">'
412+
'<a:g2 xml:lang="en">Hello</a:g2>'
413+
'</a:g1>'))
414+
415+
def test_5027_2(self):
416+
# The xml prefix (as in xml:lang below) is reserved and bound by
417+
# definition to http://www.w3.org/XML/1998/namespace. XMLGenerator had
418+
# a bug whereby a KeyError is thrown because this namespace is missing
419+
# from a dictionary.
420+
#
421+
# This test demonstrates the bug by direct manipulation of the
422+
# XMLGenerator.
423+
result = StringIO()
424+
gen = XMLGenerator(result)
425+
426+
gen.startDocument()
427+
gen.startPrefixMapping('a', 'http://example.com/ns')
428+
gen.startElementNS(('http://example.com/ns', 'g1'), 'g1', {})
429+
lang_attr = {('http://www.w3.org/XML/1998/namespace', 'lang'): 'en'}
430+
gen.startElementNS(('http://example.com/ns', 'g2'), 'g2', lang_attr)
431+
gen.characters('Hello')
432+
gen.endElementNS(('http://example.com/ns', 'g2'), 'g2')
433+
gen.endElementNS(('http://example.com/ns', 'g1'), 'g1')
434+
gen.endPrefixMapping('a')
435+
gen.endDocument()
436+
437+
self.assertEquals(result.getvalue(),
438+
start + (
439+
'<a:g1 xmlns:a="http://example.com/ns">'
440+
'<a:g2 xml:lang="en">Hello</a:g2>'
441+
'</a:g1>'))
442+
388443

389444
class XMLFilterBaseTest(unittest.TestCase):
390445
def test_filter_basic(self):

Lib/xml/sax/saxutils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ def _write(self, text):
100100
def _qname(self, name):
101101
"""Builds a qualified name from a (ns_url, localname) pair"""
102102
if name[0]:
103+
# Per http://www.w3.org/XML/1998/namespace, The 'xml' prefix is
104+
# bound by definition to http://www.w3.org/XML/1998/namespace. It
105+
# does not need to be declared and will not usually be found in
106+
# self._current_context.
107+
if 'http://www.w3.org/XML/1998/namespace' == name[0]:
108+
return 'xml:' + name[1]
103109
# The name is in a non-empty namespace
104110
prefix = self._current_context[name[0]]
105111
if prefix:

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ Greg Ewing
257257
Martijn Faassen
258258
Andreas Faerber
259259
Bill Fancher
260+
Troy J. Farrell
260261
Mark Favas
261262
Niels Ferguson
262263
Sebastian Fernandez

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ Core and Builtins
5151
Library
5252
-------
5353

54+
- Issue #5027: The standard ``xml`` namespace is now understood by
55+
xml.sax.saxutils.XMLGenerator as being bound to
56+
http://www.w3.org/XML/1998/namespace. Patch by Troy J. Farrell.
57+
5458
- Issue #5975: Add csv.unix_dialect class.
5559

5660
- Issue #7761: telnetlib.interact failures on Windows fixed.

0 commit comments

Comments
 (0)