Title: xml namespace not understood by xml.sax.saxutils.XMLGenerator
Type: behavior Stage: resolved
Components: Library (Lib), XML Versions: Python 3.1, Python 3.2, Python 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: fdrake, loewis, pitrou, roug, terry.reedy, troy
Priority: normal Keywords: easy, patch

Created on 2009-01-21 20:59 by roug, last changed 2010-10-27 18:52 by pitrou. This issue is now closed.

File name Uploaded Description Edit
saxutils.issue5027.patch troy, 2009-12-01 20:50
issue5027.py27.diff troy, 2010-08-08 01:56 A patch for the issue against the 2.7 maintenance branch
issue5027.py3k.diff troy, 2010-08-08 01:57 A patch for the issue against the 3.2 branch
Messages (12)
msg80346 - (view) Author: Soren Roug (roug) Date: 2009-01-21 20:59
The 'xml' namespace in XML files is special in that it need not be
declared. When xml.sax.saxutils.XMLGenerator is used with the namespace
feature it does not know how to handle it.

Example. The code:

import xml.sax, xml.sax.saxutils
parser = xml.sax.make_parser()
parser.setFeature(xml.sax.handler.feature_namespaces, 1)
c = xml.sax.saxutils.XMLGenerator()

executed on the testfile.xml with this content:

<?xml version="1.0"?>
<a:greetings xmlns:a="">
  <a:greet xml:lang="en">Hello world</a:greet>

will produce this error:
  File "/usr/lib/python2.5/xml/sax/", line 149, in startElementNS
    self._write(' %s=%s' % (self._qname(name), quoteattr(value)))
  File "/usr/lib/python2.5/xml/sax/", line 107, in _qname
    prefix = self._current_context[name[0]]
KeyError: u''

It can be fixed by making an exception for the xml namespace (as
required by W3C - See in
xml/sax/ The _qname method could then look like this:

    def _qname(self, name):
        """Builds a qualified name from a (ns_url, localname) pair"""
        if name[0]:
            if name[0] == u'':
                return u'xml' + ":" + name[1]
            # The name is in a non-empty namespace
            prefix = self._current_context[name[0]]
            if prefix:
                # If it is not the default namespace, prepend the prefix
                return prefix + ":" + name[1]
        # Return the unqualified name
        return name[1]
msg95873 - (view) Author: Troy J. Farrell (troy) Date: 2009-12-01 20:50
I've duplicated the issue and the fix using Python 2.6.2.  I'm attaching 
Soren Roug's fix in patch form.  (I created the patch against r53754 of
msg112726 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-08-04 00:00
Can you add a unittest, based on the example, that fails before and passes after the patch?

Assuming this applies to Py3, make patch against py3k branch (or at least 3.2a1 release), which is now 'trunk'.

That aside, the patch is a simple 2-line addition.
msg113062 - (view) Author: Troy J. Farrell (troy) Date: 2010-08-06 02:46
I've attached a patch against branches/py3k that tests and fixes the issue.

I don't suppose this fix (if I backport it) could make it into 2.6.6, could it?
msg113064 - (view) Author: Troy J. Farrell (troy) Date: 2010-08-06 03:17
I've created tests and patches for the trunk and branches/py3k.  The only difference between the two is the use of u'' for a Unicode string in the trunk.  (IIRC, Py3k treats all strings as Unicode.)
msg113070 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-08-06 03:56
It is about a week too late for 2.6.6. rc1 is just out and only critically needed fixes before the final.

For future reference, 'trunk' is frozen. 2.7 patches should be against '2.7maintenace' (or however spelled) but I assume this should apply. 'py3k' is the defacto development trunk. I am not sure what will happen after the switch to hg.

Given that you both agree on the fix, I suspect that this is ready for commit review, but I cannot properly review it. I am trying to get information on who to add to nosy.
msg113097 - (view) Author: Troy J. Farrell (troy) Date: 2010-08-06 11:48
I figured it was probably too late, but one can always hope. :)

While you sort out who gets to review this, I'll see if I can't work out a patch for 2.7.  It also occurred to me last night that I should probably add a comment to it.  Look for new patches with a day.
msg113182 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-08-07 16:02
There are no specific maintainers for xml.sax.utils. As per RDM's suggestion, Fred or Martin, do either of you have any comments on this or who it might be referred to?
msg113231 - (view) Author: Troy J. Farrell (troy) Date: 2010-08-08 01:56
I'm attaching new patches for 2.7 and 3.2, now with comments. :)
msg114317 - (view) Author: Troy J. Farrell (troy) Date: 2010-08-19 02:39
Hi guys.
I'd like to take a moment to remind everyone that this issue has a small patch with two tests and comments.  Please don't let it get lost. :)

msg119724 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-10-27 18:29
According to

“The prefix xml is by definition bound to the namespace name It MAY, but need not, be declared, and MUST NOT be bound to any other namespace name. Other prefixes MUST NOT be bound to this namespace name, and it MUST NOT be declared as the default namespace.”

The patch looks good to me.
msg119726 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-10-27 18:52
Committed in r85858 (3.2), r85859 (3.1) and r85860 (2.7). Thank you!
Date User Action Args
2010-10-27 18:52:36pitrousetstatus: open -> closed
resolution: accepted -> fixed
messages: + msg119726

stage: commit review -> resolved
2010-10-27 18:29:00pitrousetnosy: + pitrou
messages: + msg119724

resolution: accepted
stage: patch review -> commit review
2010-08-19 02:39:54troysetmessages: + msg114317
2010-08-08 01:57:10troysetfiles: + issue5027.py3k.diff
2010-08-08 01:56:16troysetfiles: + issue5027.py27.diff

messages: + msg113231
2010-08-08 01:55:19troysetfiles: - issue5027.py3k.diff
2010-08-08 01:55:10troysetfiles: - issue5027.trunk.diff
2010-08-07 16:02:38terry.reedysetnosy: + loewis, fdrake
messages: + msg113182
2010-08-06 11:48:46troysetmessages: + msg113097
2010-08-06 03:56:25terry.reedysetmessages: + msg113070
stage: test needed -> patch review
2010-08-06 03:18:07troysetfiles: - issue5027.diff
2010-08-06 03:17:55troysetfiles: + issue5027.py3k.diff

messages: + msg113064
2010-08-06 03:15:31troysetfiles: + issue5027.trunk.diff
2010-08-06 02:46:32troysetfiles: + issue5027.diff

messages: + msg113062
2010-08-04 00:00:34terry.reedysetversions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.6, Python 2.5
nosy: + terry.reedy

messages: + msg112726

keywords: + easy
stage: test needed
2009-12-01 20:50:30troysetfiles: + saxutils.issue5027.patch
versions: + Python 2.6
nosy: + troy

messages: + msg95873

keywords: + patch
2009-01-21 20:59:41rougcreate