Whole file

martinblech/xmltodict

The author described this change as fix: preserve namespace declaration when parsing. It counts as a record because the checks below fail on the code as it stood at 05c171a9c and pass on 61047021c, with nothing else changed between the two runs.

Fix saved2016-06-05
Sharing licenceMIT · LICENSE
Change size+8 0

What the code was meant to do, written into the code itself as a save note

fix: preserve namespace declaration when parsing

The change

7171 self.strip_whitespace = strip_whitespace
7272 self.namespace_separator = namespace_separator
7373 self.namespaces = namespaces
74+ self.namespace_declarations = OrderedDict()
7475 self.force_list = force_list
7576
7677 def _build_name(self, full_name):
9192 return attrs
9293 return self.dict_constructor(zip(attrs[0::2], attrs[1::2]))
9394
95+ def startNamespaceDecl(self, prefix, uri):
96+ self.namespace_declarations[prefix or ''] = uri
97+
9498 def startElement(self, full_name, attrs):
9599 name = self._build_name(full_name)
96100 attrs = self._attrs_to_dict(attrs)
101+ if attrs and self.namespace_declarations:
102+ attrs['xmlns'] = self.namespace_declarations
103+ self.namespace_declarations = OrderedDict()
97104 self.path.append((name, attrs or None))
98105 if len(self.path) > self.item_depth:
99106 self.stack.append((self.item, self.data))
301308 except AttributeError:
302309 # Jython's expat does not support ordered_attributes
303310 pass
311+ parser.StartNamespaceDeclHandler = handler.startNamespaceDecl
304312 parser.StartElementHandler = handler.startElement
305313 parser.EndElementHandler = handler.endElement
306314 parser.CharacterDataHandler = handler.characters

The check that tells the two apart

failpass·tests/test_xmltodict.py::XMLToDictTestCase::test_namespace_collapse
failpass·tests/test_xmltodict.py::XMLToDictTestCase::test_namespace_support

Check file tests/test_xmltodict.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before it05c171a9ce2406dd914364cdf7a497f4eeb62239
Broken version dated2016-04-05
Modulexmltodict
Units changed_DictSAXHandler, parse
Fingerprint24e5cf3109636495
Checked2026-08-18 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in martinblech/xmltodict