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() parser.setContentHandler(c) parser.parse('testfile.xml') executed on the testfile.xml with this content: <a:greetings xmlns:a="http://example.com/ns"> <a:greet xml:lang="en">Hello world will produce this error: ... File "/usr/lib/python2.5/xml/sax/saxutils.py", line 149, in startElementNS self._write(' %s=%s' % (self._qname(name), quoteattr(value))) File "/usr/lib/python2.5/xml/sax/saxutils.py", line 107, in _qname prefix = self._current_context[name[0]] KeyError: u'http://www.w3.org/XML/1998/namespace' It can be fixed by making an exception for the xml namespace (as required by W3C - See http://www.w3.org/XML/1998/namespace) in xml/sax/saxutils.py. 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'http://www.w3.org/XML/1998/namespace': 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 saxutils.py.) |
|
|
msg112726 - (view) |
Author: Terry J. Reedy (terry.reedy) *  |
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) *  |
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) *  |
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. :) Thanks, Troy |
|
|
msg119724 - (view) |
Author: Antoine Pitrou (pitrou) *  |
Date: 2010-10-27 18:29 |
According to http://www.w3.org/TR/xml-names/: βThe prefix xml is by definition bound to the namespace name http://www.w3.org/XML/1998/namespace. 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) *  |
Date: 2010-10-27 18:52 |
Committed in r85858 (3.2), r85859 (3.1) and r85860 (2.7). Thank you! |
|
|