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

XMLGenerator.startElementNS dies on EMPTY_NAMESPACE attribut #39586

Closed
zenzen mannequin opened this issue Nov 23, 2003 · 7 comments
Closed

XMLGenerator.startElementNS dies on EMPTY_NAMESPACE attribut #39586

zenzen mannequin opened this issue Nov 23, 2003 · 7 comments

Comments

@zenzen
Copy link
Mannequin

zenzen mannequin commented Nov 23, 2003

BPO 847665
Nosy @loewis, @akuchling
Files
  • startElementNS.py: Replacement startElementNS and endElementNS
  • 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 2007-02-12.12:23:30.000>
    created_at = <Date 2003-11-23.15:21:36.000>
    labels = ['expert-XML']
    title = 'XMLGenerator.startElementNS dies on EMPTY_NAMESPACE attribut'
    updated_at = <Date 2007-02-12.12:23:30.000>
    user = 'https://bugs.python.org/zenzen'

    bugs.python.org fields:

    activity = <Date 2007-02-12.12:23:30.000>
    actor = 'loewis'
    assignee = 'none'
    closed = True
    closed_date = None
    closer = None
    components = ['XML']
    creation = <Date 2003-11-23.15:21:36.000>
    creator = 'zenzen'
    dependencies = []
    files = ['1109']
    hgrepos = []
    issue_num = 847665
    keywords = []
    message_count = 7.0
    messages = ['19095', '19096', '19097', '19098', '19099', '19100', '19101']
    nosy_count = 4.0
    nosy_names = ['loewis', 'akuchling', 'zenzen', 'ngrig']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue847665'
    versions = ['Python 2.3']

    @zenzen
    Copy link
    Mannequin Author

    zenzen mannequin commented Nov 23, 2003

    from xml.sax.saxutils import XMLGenerator
    
    g = XMLGenerator(encoding='utf8')
    STREAM_NAMESPACE = 'http://etherx.jabber.org/streams'
    CLIENT_NAMESPACE = 'jabber:client'
    g.startDocument()
    g.startPrefixMapping('stream', STREAM_NAMESPACE)
    g.startPrefixMapping('client', CLIENT_NAMESPACE)
    g.startElementNS( 
        (STREAM_NAMESPACE, 'stream'), 'stream',
        {(None,'xmlns'): CLIENT_NAMESPACE}
        )
    Dies with:
    Traceback (most recent call last):
      File "tst.py", line 11, in ?
        g.startElementNS( 
      File "/System/Library/Frameworks/Python.framework/
    Versions/2.3/lib/python2.3/xml/sax/saxutils.py", line 124, in 
    startElementNS
        name = self._current_context[name[0]] + ":" + name[1]
    KeyError: 'x'

    Changing the end of XMLGenerator.startElementNS to the
    following makes it work the way I expect:

            for (name, value) in attrs.items():
                if name[0] is None:
                    name = name[1]
                else:
                    name = self._current_context[name[0]] + ":" + 
    name[1]
                self._out.write(' %s=%s' % (name, 
    quoteattr(value)))
            self._out.write('>')

    @zenzen zenzen mannequin closed this as completed Nov 23, 2003
    @zenzen zenzen mannequin added the topic-XML label Nov 23, 2003
    @zenzen zenzen mannequin closed this as completed Nov 23, 2003
    @zenzen zenzen mannequin added the topic-XML label Nov 23, 2003
    @zenzen
    Copy link
    Mannequin Author

    zenzen mannequin commented Nov 24, 2003

    Logged In: YES
    user_id=46639

    This method also appears to have trouble emmitting tags without a
    namespace prefix:

    import xml.sax
    import xml.sax.handler
    import xml.sax.saxutils
    x = '''<?xml version="1.0"?><mechanisms xmlns='urn:ietf:
    params:xml:ns:xmpp-sasl'
    >'''
    parser = xml.sax.make_parser()
    parser.setFeature(xml.sax.handler.feature_namespaces, True)
    parser.setContentHandler(xml.sax.saxutils.XMLGenerator())
    parser.feed(x)
    dies with:
    Traceback (most recent call last):
      File "tst.py", line 30, in ?
        parser.feed(x)
      File "/System/Library/Frameworks/Python.framework/Versions/
    2.3/lib/python2.3/xml/sax/expatreader.py", line 207, in feed
        self._parser.Parse(data, isFinal)
      File "/System/Library/Frameworks/Python.framework/Versions/
    2.3/lib/python2.3/xml/sax/expatreader.py", line 337, in 
    start_element_ns
        AttributesNSImpl(newattrs, qnames))
      File "/System/Library/Frameworks/Python.framework/Versions/
    2.3/lib/python2.3/xml/sax/saxutils.py", line 116, in startElementNS
        name = self._current_context[name[0]] + ":" + name[1]
    TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

    I've attached my current replacement startElementNS method
    which appears to fix both this and the previous issue.

    @akuchling
    Copy link
    Member

    Logged In: YES
    user_id=11375

    I don't understand your second example; it uses saxutils to
    parse a file. What's it got to do with XMLGenerator?

    @akuchling
    Copy link
    Member

    Logged In: YES
    user_id=11375

    Never mind my previous comment; I just noticed the
    setContentHandler call.

    @akuchling
    Copy link
    Member

    Logged In: YES
    user_id=11375

    Please provide your changes in the form of a patch.

    @ngrig
    Copy link
    Mannequin

    ngrig mannequin commented Mar 26, 2006

    Logged In: YES
    user_id=195108

    This issue lasts for 2.5 years already. I feel like it is
    somewhat stalled.

    The processing of XML namespaces in saxutils.XMLGenerator is
    so blatantly broken that I wonder if it ever has been tested
    at all. I wrote down a simple smoke test for it:

    import sys
    from xml import sax
    from xml.sax.saxutils import XMLGenerator
    from StringIO import StringIO
    
    tests = [
    ("No namespace, no attributes",     "<a/>"),
    ("No namespace, with attributes",   "<a b='c'/>"),
    ("Empty prefix, no attributes",     "<a xmlns='qux'/>"),
    ("Empty prefix, unprefixed attributes", "<a xmlns='qux'
    b='c'/>"),
    ("Prefixed, no attributes",         "<my:a xmlns:my='qux'/>"),
    ("Prefixed, unprefixed attributes", "<my:a xmlns:my='qux'
    b='c'/>"),               
    ("Prefixed, prefixed attributes",   "<my:a xmlns:my='qux'
    my:b='c'/>"),
    ]
              
    for (caption, testdoc) in tests:
        try:
            parser = sax.make_parser()
            parser.setFeature(sax.handler.feature_namespaces, 1)
            parser.setContentHandler(XMLGenerator(StringIO()))
            parser.parse(StringIO(testdoc))
            print caption, "- OK"
        except StandardError, e:
            print caption, "- FAILED:", e.__class__.__name__

    With the current version of saxutils (as of Python 2.4.1), I
    get 4 failures out of 7. Stuart Bishop's patch fixes the
    issue; could someone commit it please?

    @loewis
    Copy link
    Mannequin

    loewis mannequin commented Feb 12, 2007

    This is now fixed in r53754 and r53755.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant