Whole file
martinblech/xmltodict
The author described this change as “fix(unparse): serialize None text/attrs as empty values (fixes #401)”. It counts as a record because the checks below fail on the code as it stood at f7d76c96f and pass on aa165113b, with nothing else changed between the two runs.
Projectmartinblech/xmltodict
Fix saved2026-02-15
Sharing licenceMIT · LICENSE
Change size+8 −3
What the code was meant to do, written into the code itself as a save note
fix(unparse): serialize None text/attrs as empty values (fixes #401)
The change
| 493 | 493 | children = [] | |
| 494 | 494 | for ik, iv in v.items(): | |
| 495 | 495 | if ik == cdata_key: | |
| 496 | - | cdata = _convert_value_to_string(iv) | |
| 496 | + | if iv is None: | |
| 497 | + | cdata = None | |
| 498 | + | else: | |
| 499 | + | cdata = _convert_value_to_string(iv) | |
| 497 | 500 | continue | |
| 498 | 501 | if isinstance(ik, str) and ik.startswith(attr_prefix): | |
| 499 | 502 | ik = _process_namespace(ik, namespaces, namespace_separator, | |
| ⋯ | |||
| 502 | 505 | for k, v in iv.items(): | |
| 503 | 506 | _validate_name(k, "attribute") | |
| 504 | 507 | attr = 'xmlns{}'.format(f':{k}' if k else '') | |
| 505 | - | attrs[attr] = str(v) | |
| 508 | + | attrs[attr] = '' if v is None else str(v) | |
| 506 | 509 | continue | |
| 507 | - | if not isinstance(iv, str): | |
| 510 | + | if iv is None: | |
| 511 | + | iv = '' | |
| 512 | + | elif not isinstance(iv, str): | |
| 508 | 513 | iv = str(iv) | |
| 509 | 514 | attr_name = ik[len(attr_prefix) :] | |
| 510 | 515 | _validate_name(attr_name, "attribute") | |
The check that tells the two apart
fail→pass·tests/test_dicttoxml.py::test_none_attribute_serializes_as_empty_string
fail→pass·tests/test_dicttoxml.py::test_none_text_with_short_empty_elements_and_attributes
Check file tests/test_dicttoxml.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 itf7d76c96fc0141238947abcc5fa925d3ffd9eb78
Broken version dated2025-10-27
Modulexmltodict
Units changed_emit
Fingerprintea9a70046e200315
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
- 2025-09-17fix: allow DOCTYPE with disable_entities=True (default)
- 2025-09-16fix: fail closed when entities disabled
- 2025-09-16fix: validate XML comments
- 2025-09-12fix(unparse): handle non-string `#text` with attributes; unify value conversion
- 2025-09-12fix(unparse): skip empty lists to keep pretty/compact outputs consistent
- 2025-09-12fix(streaming): avoid parent accumulation at item_depth; add regression tests