Skip to content

Fix lxml treewalker for empty fragments - #609

Open
shkyyy18 wants to merge 1 commit into
html5lib:masterfrom
shkyyy18:fix/lxml-empty-fragment
Open

shkyyy18 wants to merge 1 commit into
html5lib:masterfrom
shkyyy18:fix/lxml-empty-fragment

Conversation

@shkyyy18

@shkyyy18 shkyyy18 commented Oct 1, 2026 •

Copy link
Copy Markdown

Summary

Fixes #343. The lxml fragment builder returns an empty list for empty input (and for a fragment containing only a doctype). Its walker wraps that list in Root, whose length was always one, so walking an otherwise valid empty fragment attempted to read a nonexistent child.

  • Report the actual number of root children and return None when a document has neither text nor children.
  • Exercise empty and doctype-only fragments across the existing treebuilder matrix, including serialization, plus a direct empty-list lxml regression.
  • Add the requested changelog and author entries.

Verification

On Windows, Python 3.12.10, pytest 7.4.4, lxml 6.1.3, chardet 7.6.0:

  • New regressions before the fix: 3 failed, 6 passed, 2 skipped (the failures are the lxml empty cases).
  • New regressions after the fix: 9 passed, 2 skipped (the optional cElementTree builder is not available).
  • Full suite after: 23,571 passed, 6,040 skipped, 1,017 xfailed, 1 failed.
  • Full original-walker baseline, excluding the 11 added cases: 23,562 passed, 6,038 skipped, 1,017 xfailed, 1 failed.
  • Both full runs have the same existing test_stream.py::test_char_win1252 failure: installed chardet guesses big5 rather than windows-1252. This is unrelated to the walker change and has not been modified or hidden.
  • Repository flake8 passes (flake8 . --extend-exclude=.venv, excluding only the local virtual environment).

The full tox/platform/Python matrix and documentation build have not been run locally. Setuptools was kept below 81 locally for this checkout's existing pkg_resources dependency; no dependency files were changed. All inputs are synthetic or existing public fixtures.

Remote CI

AppVeyor build 1.0.449: Python 2.7 base passed; the optional Python 2.7 job fails installing lxml because Microsoft Visual C++ 9.0 is missing, and both Python 3.7 jobs fail before installation because the runner has no matching Python runtime. The preceding build 1.0.448 on PR #608 has the same three infrastructure failures. These jobs did not run this PR's tests, and the overall CI status is failure, not success. GitHub Actions is awaiting maintainer approval.

AI assistance (OpenAI Codex) was used for investigation, implementation, and regression testing. The diff and before/after results were checked against the current upstream master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

lxml walker raises IndexError on empty list

1 participant