Fix UnboundLocalError in newick read_props when a required value is missing (#799) - #815
Open
youdie006 wants to merge 1 commit into
Open
Fix UnboundLocalError in newick read_props when a required value is missing (#799)#815youdie006 wants to merge 1 commit into
youdie006 wants to merge 1 commit into
Conversation
…issing
In read_props (ete4/parser/newick.pyx), the second try block only binds p1_str
inside its if branch (when a :-separated value is present). When a strict parser
requires that value but it is absent, the elif check_req and p1_req branch raises
AssertionError('missing required value') while p1_str is still unbound. The
except handler then references p1_str, so instead of the intended NewickError the
caller gets an UnboundLocalError. This fires for every strict parser with a
required second field (parsers 2, 3, 5, 6, 7).
Initialize p1_str = '' before the try, mirroring how p0_str is always defined
earlier in the same function. Parsing behavior is unchanged - parser=3 still
rejects (A); - only the exception type on the required-value-missing path
changes, so the parser raises the intended, catchable NewickError instead of
crashing.
Fixes etetoolkit#799.
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 #799.
Problem
crashes with:
in
ete4.parser.newick.read_props, on ete4 4.4.0 and currentete4HEAD.Root cause
In
read_props(ete4/parser/newick.pyx), the secondtryblock only bindsp1_strinside itsifbranch (when a:-separated value is present). When a strict parser requires that value but it is absent, theelif check_req and p1_reqbranch raisesAssertionError('missing required value')whilep1_stris still unbound. Theexcepthandler then referencesp1_str(raise NewickError('parsing %r: %s' % (p1_str, e))), so instead of the intendedNewickErrorthe caller gets anUnboundLocalError. This fires for every strict parser with a required second field (parsers 2, 3, 5, 6, 7) when a required dist/support value is missing.Fix
Initialize
p1_str = ''before thetry, mirroring howp0_stris always defined earlier in the same function. The error message now reads sensibly:parsing '': missing required value.This is deliberately narrow: it does not make the invalid input parse.
parser=3still correctly rejects(A);(as @dengzq1234 notes in the issue) -- the change only ensures the parser raises the intended, catchableNewickErrordescribed in its documented error contract instead of crashing with anUnboundLocalError, which is exactly the reporter's open complaint ("that doesn't return an error, it just blows up").Test
Added
test_newick_missing_required_valuetotests/test_tree.py, mirroring the existing newick error-test style, assertingTree('(A);', parser=3)raisesNewickError. Verified red-green against the rebuilt Cython extension: before the fix the test fails withUnboundLocalError; after it passes. Fulltests/test_tree.pyand adjacent newick-parsing modules pass with no regressions.Thanks to @terrycojones for the clear report.
This change was prepared with AI assistance and reviewed by me before submission.