mirror of
https://github.com/python/cpython.git
synced 2025-10-22 06:32:43 +00:00
Changed so lines that are all comment (or just whitespace + comment)
are completely skipped, rather than being treated as blank lines (and then subject to the 'skip_blanks' flag). This allows us to process old-style Setup files, which rely on hello \\ # boo! there coming out as "hello there".
This commit is contained in:
parent
3d05c16003
commit
acff0b3f3b
1 changed files with 27 additions and 9 deletions
|
@ -201,8 +201,10 @@ class TextFile:
|
||||||
pos = string.find (line, "#")
|
pos = string.find (line, "#")
|
||||||
if pos == -1: # no "#" -- no comments
|
if pos == -1: # no "#" -- no comments
|
||||||
pass
|
pass
|
||||||
elif pos == 0 or line[pos-1] != "\\": # it's a comment
|
|
||||||
|
# It's definitely a comment -- either "#" is the first
|
||||||
|
# character, or it's elsewhere and unescaped.
|
||||||
|
elif pos == 0 or line[pos-1] != "\\":
|
||||||
# Have to preserve the trailing newline, because it's
|
# Have to preserve the trailing newline, because it's
|
||||||
# the job of a later step (rstrip_ws) to remove it --
|
# the job of a later step (rstrip_ws) to remove it --
|
||||||
# and if rstrip_ws is false, we'd better preserve it!
|
# and if rstrip_ws is false, we'd better preserve it!
|
||||||
|
@ -212,6 +214,16 @@ class TextFile:
|
||||||
eol = (line[-1] == '\n') and '\n' or ''
|
eol = (line[-1] == '\n') and '\n' or ''
|
||||||
line = line[0:pos] + eol
|
line = line[0:pos] + eol
|
||||||
|
|
||||||
|
# If all that's left is whitespace, then skip line
|
||||||
|
# *now*, before we try to join it to 'buildup_line' --
|
||||||
|
# that way constructs like
|
||||||
|
# hello \\
|
||||||
|
# # comment that should be ignored
|
||||||
|
# there
|
||||||
|
# result in "hello there".
|
||||||
|
if string.strip(line) == "":
|
||||||
|
continue
|
||||||
|
|
||||||
else: # it's an escaped "#"
|
else: # it's an escaped "#"
|
||||||
line = string.replace (line, "\\#", "#")
|
line = string.replace (line, "\\#", "#")
|
||||||
|
|
||||||
|
@ -232,7 +244,8 @@ class TextFile:
|
||||||
if type (self.current_line) is ListType:
|
if type (self.current_line) is ListType:
|
||||||
self.current_line[1] = self.current_line[1] + 1
|
self.current_line[1] = self.current_line[1] + 1
|
||||||
else:
|
else:
|
||||||
self.current_line = [self.current_line, self.current_line+1]
|
self.current_line = [self.current_line,
|
||||||
|
self.current_line+1]
|
||||||
# just an ordinary line, read it as usual
|
# just an ordinary line, read it as usual
|
||||||
else:
|
else:
|
||||||
if line is None: # eof
|
if line is None: # eof
|
||||||
|
@ -271,7 +284,7 @@ class TextFile:
|
||||||
# well, I guess there's some actual content there: return it
|
# well, I guess there's some actual content there: return it
|
||||||
return line
|
return line
|
||||||
|
|
||||||
# end readline
|
# readline ()
|
||||||
|
|
||||||
|
|
||||||
def readlines (self):
|
def readlines (self):
|
||||||
|
@ -298,21 +311,26 @@ if __name__ == "__main__":
|
||||||
test_data = """# test file
|
test_data = """# test file
|
||||||
|
|
||||||
line 3 \\
|
line 3 \\
|
||||||
|
# intervening comment
|
||||||
continues on next line
|
continues on next line
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
# result 1: no fancy options
|
# result 1: no fancy options
|
||||||
result1 = map (lambda x: x + "\n", string.split (test_data, "\n")[0:-1])
|
result1 = map (lambda x: x + "\n", string.split (test_data, "\n")[0:-1])
|
||||||
|
|
||||||
# result 2: just strip comments
|
# result 2: just strip comments
|
||||||
result2 = ["\n", "\n", "line 3 \\\n", " continues on next line\n"]
|
result2 = ["\n",
|
||||||
|
"line 3 \\\n",
|
||||||
|
" continues on next line\n"]
|
||||||
|
|
||||||
# result 3: just strip blank lines
|
# result 3: just strip blank lines
|
||||||
result3 = ["# test file\n", "line 3 \\\n", " continues on next line\n"]
|
result3 = ["# test file\n",
|
||||||
|
"line 3 \\\n",
|
||||||
|
"# intervening comment\n",
|
||||||
|
" continues on next line\n"]
|
||||||
|
|
||||||
# result 4: default, strip comments, blank lines, and trailing whitespace
|
# result 4: default, strip comments, blank lines, and trailing whitespace
|
||||||
result4 = ["line 3 \\", " continues on next line"]
|
result4 = ["line 3 \\",
|
||||||
|
" continues on next line"]
|
||||||
|
|
||||||
# result 5: strip comments and blanks, plus join lines (but don't
|
# result 5: strip comments and blanks, plus join lines (but don't
|
||||||
# "collapse" joined lines
|
# "collapse" joined lines
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue