diff options
author | Greg Ward <gward@python.net> | 2000-09-16 18:33:36 (GMT) |
---|---|---|
committer | Greg Ward <gward@python.net> | 2000-09-16 18:33:36 (GMT) |
commit | acff0b3f3b97584ab9e2da923eb0041f6cc99ed3 (patch) | |
tree | fd9eb1e971165fe6d7433dc519b36717f598248c /Lib/distutils/text_file.py | |
parent | 3d05c1600336ad3f09b28cbedc7b31c17dcf51f0 (diff) | |
download | cpython-acff0b3f3b97584ab9e2da923eb0041f6cc99ed3.zip cpython-acff0b3f3b97584ab9e2da923eb0041f6cc99ed3.tar.gz cpython-acff0b3f3b97584ab9e2da923eb0041f6cc99ed3.tar.bz2 |
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".
Diffstat (limited to 'Lib/distutils/text_file.py')
-rw-r--r-- | Lib/distutils/text_file.py | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/Lib/distutils/text_file.py b/Lib/distutils/text_file.py index c983afa..37bffe6 100644 --- a/Lib/distutils/text_file.py +++ b/Lib/distutils/text_file.py @@ -201,8 +201,10 @@ class TextFile: pos = string.find (line, "#") if pos == -1: # no "#" -- no comments 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 # the job of a later step (rstrip_ws) to remove 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 '' 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 "#" line = string.replace (line, "\\#", "#") @@ -232,7 +244,8 @@ class TextFile: if type (self.current_line) is ListType: self.current_line[1] = self.current_line[1] + 1 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 else: if line is None: # eof @@ -271,7 +284,7 @@ class TextFile: # well, I guess there's some actual content there: return it return line - # end readline + # readline () def readlines (self): @@ -298,21 +311,26 @@ if __name__ == "__main__": test_data = """# test file line 3 \\ +# intervening comment continues on next line """ - - # result 1: no fancy options result1 = map (lambda x: x + "\n", string.split (test_data, "\n")[0:-1]) # 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 - 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 - 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 # "collapse" joined lines |