Convert deeply nested documents without recursion (#276) - #277
Open
HardMax71 wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes GH-276. process_tag recursed for every nesting level, so an ordinary acyclic tree hit the default recursion limit at about 330 levels. Mail clients produce that shape easily, Outlook wraps every quoted reply in another div.
process_tag now walks the subtree with an explicit stack. The per-tag work moved into _open_tag (pick the children, build the parent context) and _close_tag (join, collapse newlines, run the convert function), unchanged otherwise. Traversal order is the same, so the output is identical and the existing suite passes untouched. bs4 did the same for tree output in 4.12.1 (bug 1471755).
Subclasses that override process_tag or process_element keep their per-child calls, so per-tag filters found in the wild still fire for nested tags (covered by a test). A cyclic tree (#256) would now spin instead of raising, so a tag already on the current path is skipped, same outcome as #274.
Checked on 3.8 with pytest, flake8, mypy and rst-lint as in CI. A 1000 level document converts in about 0.1 s.