Skip to content

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

Closed

Description

@roug
mannequin
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']

    Activity

    1. roug commented on Jan 21, 2009

      rougmannequin
      MannequinAuthor

      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]
    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Jan 21, 2009
    3. troy commented on Dec 1, 2009

      troymannequin
      Mannequin

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

    4. terryjreedy commented on Aug 4, 2010

      @terryjreedy
      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.

    5. troy commented on Aug 6, 2010

      troymannequin
      Mannequin

      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?

    6. troy commented on Aug 6, 2010

      troymannequin
      Mannequin

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

    7. terryjreedy commented on Aug 6, 2010

      @terryjreedy
      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.

    8. troy commented on Aug 6, 2010

      troymannequin
      Mannequin

      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.

    9. terryjreedy commented on Aug 7, 2010

      @terryjreedy
      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?

    10. troy commented on Aug 8, 2010

      troymannequin
      Mannequin

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

    11. troy commented on Aug 19, 2010

      troymannequin
      Mannequin

      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

    12. pitrou commented on Oct 27, 2010

      @pitrou
      Member

      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.

    13. pitrou commented on Oct 27, 2010

      @pitrou
      Member

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

    14. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      easystdlibStandard Library Python modules in the Lib/ directorytopic-XMLtype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions