From 2cc3b4ba9ffa658784da03f14a0a068e2c61d1b3 Mon Sep 17 00:00:00 2001 From: Ezio Melotti Date: Sat, 3 Nov 2012 17:38:43 +0200 Subject: [PATCH] #16152: fix tokenize to ignore whitespace at the end of the code when no newline is found. Patch by Ned Batchelder. --- Lib/test/test_tokenize.py | 5 +++++ Lib/tokenize.py | 4 +++- Misc/ACKS | 1 + Misc/NEWS | 3 +++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_tokenize.py b/Lib/test/test_tokenize.py index b6a9ca1e168..f9652ce7b27 100644 --- a/Lib/test/test_tokenize.py +++ b/Lib/test/test_tokenize.py @@ -552,6 +552,11 @@ Evil tabs DEDENT '' (4, 0) (4, 0) DEDENT '' (4, 0) (4, 0) +Pathological whitespace (http://bugs.python.org/issue16152) + >>> dump_tokens("@ ") + ENCODING 'utf-8' (0, 0) (0, 0) + OP '@' (1, 0) (1, 1) + Non-ascii identifiers >>> dump_tokens("Örter = 'places'\\ngrün = 'green'") diff --git a/Lib/tokenize.py b/Lib/tokenize.py index 59081d35790..29c9e29b30f 100644 --- a/Lib/tokenize.py +++ b/Lib/tokenize.py @@ -108,7 +108,7 @@ ContStr = group(r"[bB]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*" + group("'", r'\\\r?\n'), r'[bB]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*' + group('"', r'\\\r?\n')) -PseudoExtras = group(r'\\\r?\n', Comment, Triple) +PseudoExtras = group(r'\\\r?\n|\Z', Comment, Triple) PseudoToken = Whitespace + group(PseudoExtras, Number, Funny, ContStr, Name) def _compile(expr): @@ -473,6 +473,8 @@ def _tokenize(readline, encoding): if pseudomatch: # scan for tokens start, end = pseudomatch.span(1) spos, epos, pos = (lnum, start), (lnum, end), end + if start == end: + continue token, initial = line[start:end], line[start] if (initial in numchars or # ordinary number diff --git a/Misc/ACKS b/Misc/ACKS index a070be811c1..10957338393 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -71,6 +71,7 @@ Des Barry Ulf Bartelt Don Bashford Nick Bastin +Ned Batchelder Jeff Bauer Mike Bayer Michael R Bax diff --git a/Misc/NEWS b/Misc/NEWS index 30752252337..3ee301f8a89 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -143,6 +143,9 @@ Core and Builtins Library ------- +- Issue #16152: fix tokenize to ignore whitespace at the end of the code when + no newline is found. Patch by Ned Batchelder. + - Issue #1207589: Add Cut/Copy/Paste items to IDLE right click Context Menu Patch by Todd Rovito.