Skip to content

Commit

Permalink
Don't crash and burn on empty lines with trailing whitespace
Browse files Browse the repository at this point in the history
Fixes #80
  • Loading branch information
ambv committed Mar 27, 2018
1 parent 611737f commit fc86903
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,12 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md).

## Change Log

### 18.3a5 (unreleased)

* fixed 18.3a4 regression: don't crash and burn on empty lines with
trailing whitespace (#80)


### 18.3a4

* `# fmt: off` and `# fmt: on` are implemented (#5)
Expand Down
24 changes: 12 additions & 12 deletions blib2to3/pgen2/tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,24 +430,24 @@ def generate_tokens(readline):
yield stashed
stashed = None

if line[pos] in '\r\n': # skip blank lines
yield (NL, line[pos:], (lnum, pos), (lnum, len(line)), line)
continue

if column > indents[-1]: # count indents
indents.append(column)
yield (INDENT, line[:pos], (lnum, 0), (lnum, pos), line)

if line[pos] in '#\r\n': # skip comments or blank lines
if line[pos] == '#':
comment_token = line[pos:].rstrip('\r\n')
nl_pos = pos + len(comment_token)
yield (COMMENT, comment_token,
(lnum, pos), (lnum, pos + len(comment_token)), line)
yield (NL, line[nl_pos:],
(lnum, nl_pos), (lnum, len(line)), line)
else:
yield ((NL, COMMENT)[line[pos] == '#'], line[pos:],
(lnum, pos), (lnum, len(line)), line)
if line[pos] == '#': # skip comments
comment_token = line[pos:].rstrip('\r\n')
nl_pos = pos + len(comment_token)
yield (COMMENT, comment_token,
(lnum, pos), (lnum, pos + len(comment_token)), line)
yield (NL, line[nl_pos:],
(lnum, nl_pos), (lnum, len(line)), line)
continue

while column < indents[-1]: # count dedents
while column < indents[-1]: # count dedents
if column not in indents:
raise IndentationError(
"unindent does not match any outer indentation level",
Expand Down
1 change: 1 addition & 0 deletions tests/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r''
def spaces_types(a: int = 1, b: tuple = (), c: list = [], d: dict = {}, e: bool = True, f: int = -1, g: int = 1 if False else 2, h: str = "", i: str = r''): ...
def spaces2(result= _core.Value(None)):
...
# EMPTY LINE WITH WHITESPACE (this comment will be removed)
def example(session):
result = session.query(models.Customer.id).filter(
models.Customer.account_id == account_id,
Expand Down
2 changes: 2 additions & 0 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
fs = partial(black.format_str, line_length=ll)
THIS_FILE = Path(__file__)
THIS_DIR = THIS_FILE.parent
EMPTY_LINE = '# EMPTY LINE WITH WHITESPACE' + ' (this comment will be removed)'


def dump_to_stderr(*output: str) -> str:
Expand All @@ -33,6 +34,7 @@ def read_data(name: str) -> Tuple[str, str]:
lines = test.readlines()
result = _input
for line in lines:
line = line.replace(EMPTY_LINE, '')
if line.rstrip() == '# output':
result = _output
continue
Expand Down

0 comments on commit fc86903

Please sign in to comment.