Raise ValueError for truncated path commands instead of a bare IndexError - #246
Open
eeshsaxena wants to merge 1 commit into
Open
Raise ValueError for truncated path commands instead of a bare IndexError#246eeshsaxena wants to merge 1 commit into
eeshsaxena wants to merge 1 commit into
Conversation
A path string that ends mid-command (e.g. 'M 0 0 L', 'C 1 2', a bare 'H') popped past the end of the token list in _parse_path, surfacing a bare IndexError: pop from empty list. Add a per-command argument-count check so these raise a descriptive ValueError like other malformed paths. Also fix a leading smooth command (a path starting with S/s or T/t): the reflection check compared last_command (None) with 'in "CS"', raising TypeError. Compare against a tuple so a missing previous command is handled.
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.
I was feeding some machine-generated d-strings through parse_path and hit a couple of confusing crashes when a string happened to be cut off mid-command.
Anything that ends before a command has all its numbers, like
M 0 0 L,C 1 2, or a loneH, pops past the end of the token list in_parse_pathand comes back asIndexError: pop from empty list, which doesn't tell you much about what was wrong with the path. I added a small per-command argument-count check right after a command is read, so a short command now raises a ValueError naming the command and how many values it expected, matching how the parser already reports other malformed input (theZ 100 200case).While testing that I also ran into a second one: a path that starts with a smooth command, e.g.
S 1 1 2 2orT 1 1, raisesTypeError: 'in <string>' requires string as left operand, not NoneType. On the first commandlast_commandis still None and the reflection check doeslast_command not in 'CS', andNone in 'CS'throws. Per the SVG spec, when there's no preceding curve the first control point should just coincide with the current point, so I switched the two membership tests to tuples (('C', 'S')/('Q', 'T')), which handles None and parses these as you'd expect.Added tests for both under test_parsing.py; they fail on master (IndexError / TypeError) and pass with the change, and the rest of the parsing suite still passes.