Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2cc3b4b

Browse files
committed
#16152: fix tokenize to ignore whitespace at the end of the code when no newline is found. Patch by Ned Batchelder.
1 parent 4552e3f commit 2cc3b4b

4 files changed

Lines changed: 12 additions & 1 deletion

File tree

Lib/test/test_tokenize.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,11 @@
552552
DEDENT '' (4, 0) (4, 0)
553553
DEDENT '' (4, 0) (4, 0)
554554
555+
Pathological whitespace (http://bugs.python.org/issue16152)
556+
>>> dump_tokens("@ ")
557+
ENCODING 'utf-8' (0, 0) (0, 0)
558+
OP '@' (1, 0) (1, 1)
559+
555560
Non-ascii identifiers
556561
557562
>>> dump_tokens("Örter = 'places'\\ngrün = 'green'")

Lib/tokenize.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def maybe(*choices): return group(*choices) + '?'
108108
group("'", r'\\\r?\n'),
109109
r'[bB]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*' +
110110
group('"', r'\\\r?\n'))
111-
PseudoExtras = group(r'\\\r?\n', Comment, Triple)
111+
PseudoExtras = group(r'\\\r?\n|\Z', Comment, Triple)
112112
PseudoToken = Whitespace + group(PseudoExtras, Number, Funny, ContStr, Name)
113113

114114
def _compile(expr):
@@ -473,6 +473,8 @@ def _tokenize(readline, encoding):
473473
if pseudomatch: # scan for tokens
474474
start, end = pseudomatch.span(1)
475475
spos, epos, pos = (lnum, start), (lnum, end), end
476+
if start == end:
477+
continue
476478
token, initial = line[start:end], line[start]
477479

478480
if (initial in numchars or # ordinary number

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Des Barry
7171
Ulf Bartelt
7272
Don Bashford
7373
Nick Bastin
74+
Ned Batchelder
7475
Jeff Bauer
7576
Mike Bayer
7677
Michael R Bax

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ Core and Builtins
143143
Library
144144
-------
145145

146+
- Issue #16152: fix tokenize to ignore whitespace at the end of the code when
147+
no newline is found. Patch by Ned Batchelder.
148+
146149
- Issue #1207589: Add Cut/Copy/Paste items to IDLE right click Context Menu
147150
Patch by Todd Rovito.
148151

0 commit comments

Comments
 (0)