Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
9dc3a36c
Commit
9dc3a36c
authored
Feb 23, 2014
by
Terry Jan Reedy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #9974: When untokenizing, use row info to insert backslash+newline.
Original patches by A. Kuchling and G. Rees (#12691).
parent
938ba685
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
1 deletion
+22
-1
Lib/test/test_tokenize.py
Lib/test/test_tokenize.py
+16
-1
Lib/tokenize.py
Lib/tokenize.py
+6
-0
No files found.
Lib/test/test_tokenize.py
View file @
9dc3a36c
...
...
@@ -2,7 +2,7 @@ doctests = """
Tests for the tokenize module.
The tests can be really simple. Given a small fragment of source
code, print out a table with tokens. The ENDMARK is omitted for
code, print out a table with tokens. The ENDMARK
ER
is omitted for
brevity.
>>> dump_tokens("1 + 1")
...
...
@@ -1180,6 +1180,7 @@ class TestTokenize(TestCase):
class
UntokenizeTest
(
TestCase
):
def
test_bad_input_order
(
self
):
# raise if previous row
u
=
Untokenizer
()
u
.
prev_row
=
2
u
.
prev_col
=
2
...
...
@@ -1187,8 +1188,22 @@ class UntokenizeTest(TestCase):
u
.
add_whitespace
((
1
,
3
))
self
.
assertEqual
(
cm
.
exception
.
args
[
0
],
'start (1,3) precedes previous end (2,2)'
)
# raise if previous column in row
self
.
assertRaises
(
ValueError
,
u
.
add_whitespace
,
(
2
,
1
))
def
test_backslash_continuation
(
self
):
# The problem is that <whitespace>\<newline> leaves no token
u
=
Untokenizer
()
u
.
prev_row
=
1
u
.
prev_col
=
1
u
.
tokens
=
[]
u
.
add_whitespace
((
2
,
0
))
self
.
assertEqual
(
u
.
tokens
,
[
'
\
\
\
n
'
])
u
.
prev_row
=
2
u
.
add_whitespace
((
4
,
4
))
self
.
assertEqual
(
u
.
tokens
,
[
'
\
\
\
n
'
,
'
\
\
\
n
\
\
\
n
'
,
' '
])
self
.
assertTrue
(
roundtrip
(
'a
\
n
b
\
n
c
\
n
\
\
\
n
c
\
n
'
))
def
test_iter_compat
(
self
):
u
=
Untokenizer
()
token
=
(
NAME
,
'Hello'
)
...
...
Lib/tokenize.py
View file @
9dc3a36c
...
...
@@ -234,6 +234,10 @@ class Untokenizer:
if row < self.prev_row or row == self.prev_row and col < self.prev_col:
raise ValueError("start ({},{}) precedes previous end ({},{})"
.format(row, col, self.prev_row, self.prev_col))
row_offset = row - self.prev_row
if row_offset:
self.tokens.append("
\
\
\
n
" * row_offset)
self.prev_col = 0
col_offset = col - self.prev_col
if col_offset:
self.tokens.append(" " * col_offset)
...
...
@@ -248,6 +252,8 @@ class Untokenizer:
if tok_type == ENCODING:
self.encoding = token
continue
if tok_type == ENDMARKER:
break
self.add_whitespace(start)
self.tokens.append(token)
self.prev_row, self.prev_col = end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment