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

Skip to content

Commit 4e0f36b

Browse files
committed
#16152: merge with 3.3.
2 parents 76bcff2 + fafa8b7 commit 4e0f36b

4 files changed

Lines changed: 11 additions & 1 deletion

File tree

Lib/test/test_tokenize.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,10 @@ def test_exact_type(self):
11091109
token.NAME, token.AMPER, token.NUMBER,
11101110
token.RPAR)
11111111

1112+
def test_pathological_trailing_whitespace(self):
1113+
# See http://bugs.python.org/issue16152
1114+
self.assertExactTypeEqual('@ ', token.AT)
1115+
11121116
__test__ = {"doctests" : doctests, 'decistmt': decistmt}
11131117

11141118
def test_main():

Lib/tokenize.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def maybe(*choices): return group(*choices) + '?'
162162
group("'", r'\\\r?\n'),
163163
StringPrefix + r'"[^\n"\\]*(?:\\.[^\n"\\]*)*' +
164164
group('"', r'\\\r?\n'))
165-
PseudoExtras = group(r'\\\r?\n', Comment, Triple)
165+
PseudoExtras = group(r'\\\r?\n|\Z', Comment, Triple)
166166
PseudoToken = Whitespace + group(PseudoExtras, Number, Funny, ContStr, Name)
167167

168168
def _compile(expr):
@@ -555,6 +555,8 @@ def _tokenize(readline, encoding):
555555
if pseudomatch: # scan for tokens
556556
start, end = pseudomatch.span(1)
557557
spos, epos, pos = (lnum, start), (lnum, end), end
558+
if start == end:
559+
continue
558560
token, initial = line[start:end], line[start]
559561

560562
if (initial in numchars or # ordinary number

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ Ulf Bartelt
7777
Don Bashford
7878
Pior Bastida
7979
Nick Bastin
80+
Ned Batchelder
8081
Jeff Bauer
8182
Michael R Bax
8283
Anthony Baxter

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ Core and Builtins
9191
Library
9292
-------
9393

94+
- Issue #16152: fix tokenize to ignore whitespace at the end of the code when
95+
no newline is found. Patch by Ned Batchelder.
96+
9497
- Issue #16284: Prevent keeping unnecessary references to worker functions
9598
in concurrent.futures ThreadPoolExecutor.
9699

0 commit comments

Comments
 (0)