Repository navigation
Conversation
…Filter
_stripws_parenthesis assumed a group laid out as "(" content ")", so
tokens[1] and tokens[-2] existed. A group holding fewer tokens raised
IndexError from inside the formatter:
>>> sqlparse.format("( AS )", strip_whitespace=True)
IndexError: list index out of range
"( AS )" groups the whole thing into a Parenthesis with a single nested
Identifier token, so the assumption did not hold.
Fall back to the default whitespace handling when the group is too short
to have a bracket on either side, and guard the inner trailing-whitespace
loop against a group whose tokens are all whitespace.
Closes andialbrecht#885
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 #885.
_stripws_parenthesisassumed everyParenthesisgroup is laid out as(content), so it indexedtokens[1]andtokens[-2]unconditionally. For"( AS )"the grouping step produces aParenthesisthat holds a single nestedIdentifiertoken, sotokens[1]does not exist:Change
sqlparse/filters/others.py:_stripws_defaultinstead of indexing past the end.The output for the reported input is now the input unchanged (
"( AS )"). I deliberately did not collapse it to"(AS)": the inner content is a single nested group that already went through_stripws_default, and tightening it would need bracket-aware handling that is out of scope for the crash.Behaviour for well-formed groups is unchanged —
SELECT ( a ) FROM tstill formats toSELECT (a) FROM t, and the full suite passes.Tests
Two regression tests in
tests/test_regressions.py(near the other issue-numbered tests):test_issue885_stripws_malformed_parenthesis— the reported reproducer.test_issue885_stripws_short_parenthesis_groups—(),( ),(a)must not raise.A
CHANGELOGentry is added underDevelopment Version.