Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xml namespace not understood by xml.sax.saxutils.XMLGenerator #49277

Closed
roug mannequin opened this issue Jan 21, 2009 · 12 comments
Closed

xml namespace not understood by xml.sax.saxutils.XMLGenerator #49277

roug mannequin opened this issue Jan 21, 2009 · 12 comments
Labels
easy stdlib Python modules in the Lib dir topic-XML type-bug An unexpected behavior, bug, or error

Comments

@roug
Copy link
Mannequin

roug mannequin commented Jan 21, 2009

BPO 5027
Nosy @loewis, @freddrake, @terryjreedy, @pitrou
Files
  • saxutils.issue5027.patch
  • issue5027.py27.diff: A patch for the issue against the 2.7 maintenance branch
  • issue5027.py3k.diff: A patch for the issue against the 3.2 branch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2010-10-27.18:52:36.157>
    created_at = <Date 2009-01-21.20:59:41.237>
    labels = ['expert-XML', 'easy', 'type-bug', 'library']
    title = 'xml namespace not understood by xml.sax.saxutils.XMLGenerator'
    updated_at = <Date 2010-10-27.18:52:36.156>
    user = 'https://bugs.python.org/roug'

    bugs.python.org fields:

    activity = <Date 2010-10-27.18:52:36.156>
    actor = 'pitrou'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-10-27.18:52:36.157>
    closer = 'pitrou'
    components = ['Library (Lib)', 'XML']
    creation = <Date 2009-01-21.20:59:41.237>
    creator = 'roug'
    dependencies = []
    files = ['15430', '18429', '18430']
    hgrepos = []
    issue_num = 5027
    keywords = ['patch', 'easy']
    message_count = 12.0
    messages = ['80346', '95873', '112726', '113062', '113064', '113070', '113097', '113182', '113231', '114317', '119724', '119726']
    nosy_count = 6.0
    nosy_names = ['loewis', 'fdrake', 'terry.reedy', 'pitrou', 'roug', 'troy']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue5027'
    versions = ['Python 3.1', 'Python 2.7', 'Python 3.2']

    @roug
    Copy link
    Mannequin Author

    roug mannequin commented Jan 21, 2009

    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:

    <?xml version="1.0"?>
    <a:greetings xmlns:a="http://example.com/ns"\>
    <a:greet xml:lang="en">Hello world</a:greet>
    </a:greetings>

    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]

    @roug roug mannequin added stdlib Python modules in the Lib dir topic-XML type-bug An unexpected behavior, bug, or error labels Jan 21, 2009
    @troy
    Copy link
    Mannequin

    troy mannequin commented Dec 1, 2009

    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.)

    @terryjreedy
    Copy link
    Member

    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.

    @troy
    Copy link
    Mannequin

    troy mannequin commented Aug 6, 2010

    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?

    @troy
    Copy link
    Mannequin

    troy mannequin commented Aug 6, 2010

    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.)

    @terryjreedy
    Copy link
    Member

    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.

    @troy
    Copy link
    Mannequin

    troy mannequin commented Aug 6, 2010

    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.

    @terryjreedy
    Copy link
    Member

    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?

    @troy
    Copy link
    Mannequin

    troy mannequin commented Aug 8, 2010

    I'm attaching new patches for 2.7 and 3.2, now with comments. :)

    @troy
    Copy link
    Mannequin

    troy mannequin commented Aug 19, 2010

    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

    @pitrou
    Copy link
    Member

    pitrou commented Oct 27, 2010

    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.

    @pitrou
    Copy link
    Member

    pitrou commented Oct 27, 2010

    Committed in r85858 (3.2), r85859 (3.1) and r85860 (2.7). Thank you!

    @pitrou pitrou closed this as completed Oct 27, 2010
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    easy stdlib Python modules in the Lib dir topic-XML type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants