Skip to content

Commit a1f45ec

Browse files
taleinatbenjaminp
authored andcommitted
bpo-33899: Revert tokenize module adding an implicit final NEWLINE (pythonGH-10072)
This reverts commit 7829bba.
1 parent 56a4a3a commit a1f45ec

File tree

3 files changed

+12
-47
lines changed

3 files changed

+12
-47
lines changed

Lib/test/test_tokenize.py

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,32 @@
11
from test import test_support
2-
from tokenize import (untokenize, generate_tokens, NUMBER, NAME, OP, NEWLINE,
2+
from tokenize import (untokenize, generate_tokens, NUMBER, NAME, OP,
33
STRING, ENDMARKER, tok_name, Untokenizer, tokenize)
44
from StringIO import StringIO
55
import os
66
from unittest import TestCase
77

88

9-
# Converts a source string into a list of textual representation
10-
# of the tokens such as:
11-
# ` NAME 'if' (1, 0) (1, 2)`
12-
# to make writing tests easier.
13-
def stringify_tokens_from_source(token_generator, source_string):
14-
result = []
15-
num_lines = len(source_string.splitlines())
16-
missing_trailing_nl = source_string[-1] not in '\r\n'
17-
18-
for type, token, start, end, line in token_generator:
19-
if type == ENDMARKER:
20-
break
21-
# Ignore the new line on the last line if the input lacks one
22-
if missing_trailing_nl and type == NEWLINE and end[0] == num_lines:
23-
continue
24-
type = tok_name[type]
25-
result.append(" %(type)-10.10s %(token)-13.13r %(start)s %(end)s" %
26-
locals())
27-
28-
return result
29-
309
class TokenizeTest(TestCase):
3110
# Tests for the tokenize module.
3211

3312
# The tests can be really simple. Given a small fragment of source
34-
# code, print out a table with tokens. The ENDMARKER, ENCODING and
35-
# final NEWLINE are omitted for brevity.
13+
# code, print out a table with tokens. The ENDMARKER is omitted for
14+
# brevity.
3615

3716
def check_tokenize(self, s, expected):
3817
# Format the tokens in s in a table format.
18+
# The ENDMARKER is omitted.
19+
result = []
3920
f = StringIO(s)
40-
result = stringify_tokens_from_source(generate_tokens(f.readline), s)
41-
21+
for type, token, start, end, line in generate_tokens(f.readline):
22+
if type == ENDMARKER:
23+
break
24+
type = tok_name[type]
25+
result.append(" %(type)-10.10s %(token)-13.13r %(start)s %(end)s" %
26+
locals())
4227
self.assertEqual(result,
4328
expected.rstrip().splitlines())
4429

45-
def test_implicit_newline(self):
46-
# Make sure that the tokenizer puts in an implicit NEWLINE
47-
# when the input lacks a trailing new line.
48-
f = StringIO("x")
49-
tokens = list(generate_tokens(f.readline))
50-
self.assertEqual(tokens[-2][0], NEWLINE)
51-
self.assertEqual(tokens[-1][0], ENDMARKER)
5230

5331
def test_basic(self):
5432
self.check_tokenize("1 + 1", """\
@@ -638,7 +616,7 @@ def test_roundtrip(self):
638616
self.check_roundtrip("if x == 1:\n"
639617
" print x\n")
640618
self.check_roundtrip("# This is a comment\n"
641-
"# This also\n")
619+
"# This also")
642620

643621
# Some people use different formatting conventions, which makes
644622
# untokenize a little trickier. Note that this test involves trailing

Lib/tokenize.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -306,15 +306,8 @@ def generate_tokens(readline):
306306
contline = None
307307
indents = [0]
308308

309-
last_line = b''
310-
line = b''
311309
while 1: # loop over lines in stream
312310
try:
313-
# We capture the value of the line variable here because
314-
# readline uses the empty string '' to signal end of input,
315-
# hence `line` itself will always be overwritten at the end
316-
# of this loop.
317-
last_line = line
318311
line = readline()
319312
except StopIteration:
320313
line = ''
@@ -444,9 +437,6 @@ def generate_tokens(readline):
444437
(lnum, pos), (lnum, pos+1), line)
445438
pos += 1
446439

447-
# Add an implicit NEWLINE if the input doesn't end in one
448-
if last_line and last_line[-1] not in '\r\n':
449-
yield (NEWLINE, '', (lnum - 1, len(last_line)), (lnum - 1, len(last_line) + 1), '')
450440
for indent in indents[1:]: # pop remaining indent levels
451441
yield (DEDENT, '', (lnum, 0), (lnum, 0), '')
452442
yield (ENDMARKER, '', (lnum, 0), (lnum, 0), '')

Misc/NEWS.d/next/Library/2018-06-24-01-57-14.bpo-33899.IaOcAr.rst

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)