Fix: before() (pyquery/pyquery.py) calls tag.getparent() and, when tag has... - #269
Open
M001N wants to merge 1 commit into
Open
Fix: before() (pyquery/pyquery.py) calls tag.getparent() and, when tag has...#269M001N wants to merge 1 commit into
M001N wants to merge 1 commit into
Conversation
…on parentless elements
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.
Summary
In before(), added an explicit
if parent is None: raise ValueError(...)check both at the point parent.text is accessed and again before parent.index(tag) (the second getparent() call later in the loop). In after(), added the same check before parent.index(tag). Both raise a clear ValueError explaining that the element has no parent, instead of letting the unrelated AttributeError propagate.Problem
gawel/pyquery issue reference: #105
Root Cause
before() (pyquery/pyquery.py) calls tag.getparent() and, when tag has no previous sibling, unconditionally accesses parent.text — if tag is the topmost/root element, getparent() returns None and None.text raises a confusing AttributeError. after() has the analogous unconditional parent.index(tag) call. replace_with() calls before() internally, so it inherits the same crash.
Testing
PASS - full suite: 74 passed, 4 skipped (skips are pre-existing, OS-specific, unrelated to this change); all 4 new tests pass.
Related Issue
#105