From: Martin v. Löwis Date: Sun, 2 May 2004 20:37:13 +0000 (+0000) Subject: Do not use the default namespace for attributes. X-Git-Tag: v2.4a1~462 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9f1340b9f231f920fcf70ce2f8ea0c0e3a322dd9;p=python Do not use the default namespace for attributes. Fixes http://bugs.debian.org/229885 Will backport to 2.3. --- diff --git a/Lib/test/test_xmllib.py b/Lib/test/test_xmllib.py index b14ead9aa0..0780bc95f3 100644 --- a/Lib/test/test_xmllib.py +++ b/Lib/test/test_xmllib.py @@ -13,6 +13,8 @@ testdoc = """\ Hello, world! """ +nsdoc = "" + import warnings warnings.filterwarnings("ignore", ".* xmllib .* obsolete.*", DeprecationWarning, r'xmllib$') @@ -29,6 +31,18 @@ class XMLParserTestCase(unittest.TestCase): parser.feed(c) parser.close() + def test_default_namespace(self): + class H(xmllib.XMLParser): + def unknown_starttag(self, name, attr): + self.name, self.attr = name, attr + h=H() + h.feed(nsdoc) + h.close() + # The default namespace applies to elements... + self.assertEquals(h.name, "URI foo") + # but not to attributes + self.assertEquals(h.attr, {'attr':'val'}) + def test_main(): test_support.run_unittest(XMLParserTestCase) diff --git a/Lib/xmllib.py b/Lib/xmllib.py index f1cd2e45aa..2a189cdd8f 100644 --- a/Lib/xmllib.py +++ b/Lib/xmllib.py @@ -6,8 +6,7 @@ import re import string import warnings -warnings.warn("The xmllib module is obsolete. Use xml.sax instead.", - DeprecationWarning) +warnings.warn("The xmllib module is obsolete. Use xml.sax instead.", DeprecationWarning) del warnings version = '0.3' @@ -641,20 +640,17 @@ class XMLParser: aprefix, key = res.group('prefix', 'local') if self.__map_case: key = key.lower() - if aprefix is None: - aprefix = '' - ans = None - for t, d, nst in self.stack: - if aprefix in d: - ans = d[aprefix] - if ans is None and aprefix != '': - ans = self.__namespaces.get(aprefix) - if ans is not None: - key = ans + ' ' + key - elif aprefix != '': - key = aprefix + ':' + key - elif ns is not None: - key = ns + ' ' + key + if aprefix is not None: + ans = None + for t, d, nst in self.stack: + if aprefix in d: + ans = d[aprefix] + if ans is None: + ans = self.__namespaces.get(aprefix) + if ans is not None: + key = ans + ' ' + key + else: + key = aprefix + ':' + key nattrdict[key] = val attrnamemap[key] = okey attrdict = nattrdict