Whole file
martinblech/xmltodict
The author described this change as “fix(unparse): handle non-string `#text` with attributes; unify value conversion”. It counts as a record because the check below fails on the code as it stood at ab4c86fed and passes on 927a025ae, with nothing else changed between the two runs.
Projectmartinblech/xmltodict
Fix saved2025-09-12
Sharing licenceMIT · LICENSE
Change size+14 −4
What the code was meant to do, written into the code itself as a save note
fix(unparse): handle non-string `#text` with attributes; unify value conversion
The change
| 374 | 374 | return handler.item | |
| 375 | 375 | ||
| 376 | 376 | ||
| 377 | + | def _convert_value_to_string(value): | |
| 378 | + | """Convert a value to its string representation for XML output. | |
| 379 | + | ||
| 380 | + | Handles boolean values consistently by converting them to lowercase. | |
| 381 | + | """ | |
| 382 | + | if isinstance(value, (str, bytes)): | |
| 383 | + | return value | |
| 384 | + | if isinstance(value, bool): | |
| 385 | + | return "true" if value else "false" | |
| 386 | + | return str(value) | |
| 387 | + | ||
| 388 | + | ||
| 377 | 389 | def _has_angle_brackets(value): | |
| 378 | 390 | """Return True if value (a str) contains '<' or '>'. | |
| 379 | 391 | ||
| ⋯ | |||
| 463 | 475 | raise ValueError('document with multiple roots') | |
| 464 | 476 | if v is None: | |
| 465 | 477 | v = _dict() | |
| 466 | - | elif isinstance(v, bool): | |
| 467 | - | v = 'true' if v else 'false' | |
| 468 | 478 | elif not isinstance(v, (dict, str)): | |
| 469 | 479 | if expand_iter and hasattr(v, '__iter__'): | |
| 470 | 480 | v = _dict(((expand_iter, v),)) | |
| 471 | 481 | else: | |
| 472 | - | v = str(v) | |
| 482 | + | v = _convert_value_to_string(v) | |
| 473 | 483 | if isinstance(v, str): | |
| 474 | 484 | v = _dict(((cdata_key, v),)) | |
| 475 | 485 | cdata = None | |
| ⋯ | |||
| 477 | 487 | children = [] | |
| 478 | 488 | for ik, iv in v.items(): | |
| 479 | 489 | if ik == cdata_key: | |
| 480 | - | cdata = iv | |
| 490 | + | cdata = _convert_value_to_string(iv) | |
| 481 | 491 | continue | |
| 482 | 492 | if isinstance(ik, str) and ik.startswith(attr_prefix): | |
| 483 | 493 | ik = _process_namespace(ik, namespaces, namespace_separator, | |
The check that tells the two apart
fail→pass·tests/test_dicttoxml.py::DictToXMLTestCase::test_non_string_text_with_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 itab4c86fed24dc8ef0e932a524edfb01c6453ecf6
Broken version dated2025-09-12
Modulexmltodict
Units changed_emit
Fingerprint7f4a10241a28ca3a
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
- 2026-02-15fix(unparse): serialize None text/attrs as empty values (fixes #401)
- 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): skip empty lists to keep pretty/compact outputs consistent
- 2025-09-12fix(streaming): avoid parent accumulation at item_depth; add regression tests