Skip to content

Commit 9dc3a36

Browse files
committed
Issue #9974: When untokenizing, use row info to insert backslash+newline.
Original patches by A. Kuchling and G. Rees (#12691).
1 parent 938ba68 commit 9dc3a36

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Lib/test/test_tokenize.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Tests for the tokenize module.
33
44
The tests can be really simple. Given a small fragment of source
5-
code, print out a table with tokens. The ENDMARK is omitted for
5+
code, print out a table with tokens. The ENDMARKER is omitted for
66
brevity.
77
88
>>> dump_tokens("1 + 1")
@@ -1180,15 +1180,30 @@ def test_pathological_trailing_whitespace(self):
11801180
class UntokenizeTest(TestCase):
11811181

11821182
def test_bad_input_order(self):
1183+
# raise if previous row
11831184
u = Untokenizer()
11841185
u.prev_row = 2
11851186
u.prev_col = 2
11861187
with self.assertRaises(ValueError) as cm:
11871188
u.add_whitespace((1,3))
11881189
self.assertEqual(cm.exception.args[0],
11891190
'start (1,3) precedes previous end (2,2)')
1191+
# raise if previous column in row
11901192
self.assertRaises(ValueError, u.add_whitespace, (2,1))
11911193

1194+
def test_backslash_continuation(self):
1195+
# The problem is that <whitespace>\<newline> leaves no token
1196+
u = Untokenizer()
1197+
u.prev_row = 1
1198+
u.prev_col = 1
1199+
u.tokens = []
1200+
u.add_whitespace((2, 0))
1201+
self.assertEqual(u.tokens, ['\\\n'])
1202+
u.prev_row = 2
1203+
u.add_whitespace((4, 4))
1204+
self.assertEqual(u.tokens, ['\\\n', '\\\n\\\n', ' '])
1205+
self.assertTrue(roundtrip('a\n b\n c\n \\\n c\n'))
1206+
11921207
def test_iter_compat(self):
11931208
u = Untokenizer()
11941209
token = (NAME, 'Hello')

Lib/tokenize.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ def add_whitespace(self, start):
234234
if row < self.prev_row or row == self.prev_row and col < self.prev_col:
235235
raise ValueError("start ({},{}) precedes previous end ({},{})"
236236
.format(row, col, self.prev_row, self.prev_col))
237+
row_offset = row - self.prev_row
238+
if row_offset:
239+
self.tokens.append("\\\n" * row_offset)
240+
self.prev_col = 0
237241
col_offset = col - self.prev_col
238242
if col_offset:
239243
self.tokens.append(" " * col_offset)
@@ -248,6 +252,8 @@ def untokenize(self, iterable):
248252
if tok_type == ENCODING:
249253
self.encoding = token
250254
continue
255+
if tok_type == ENDMARKER:
256+
break
251257
self.add_whitespace(start)
252258
self.tokens.append(token)
253259
self.prev_row, self.prev_col = end

0 commit comments

Comments
 (0)