Skip to content

Commit acff0b3

Browse files
committed
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".
1 parent 3d05c16 commit acff0b3

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

Lib/distutils/text_file.py

+27-9
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,10 @@ def readline (self):
201201
pos = string.find (line, "#")
202202
if pos == -1: # no "#" -- no comments
203203
pass
204-
elif pos == 0 or line[pos-1] != "\\": # it's a comment
205-
204+
205+
# It's definitely a comment -- either "#" is the first
206+
# character, or it's elsewhere and unescaped.
207+
elif pos == 0 or line[pos-1] != "\\":
206208
# Have to preserve the trailing newline, because it's
207209
# the job of a later step (rstrip_ws) to remove it --
208210
# and if rstrip_ws is false, we'd better preserve it!
@@ -212,6 +214,16 @@ def readline (self):
212214
eol = (line[-1] == '\n') and '\n' or ''
213215
line = line[0:pos] + eol
214216

217+
# If all that's left is whitespace, then skip line
218+
# *now*, before we try to join it to 'buildup_line' --
219+
# that way constructs like
220+
# hello \\
221+
# # comment that should be ignored
222+
# there
223+
# result in "hello there".
224+
if string.strip(line) == "":
225+
continue
226+
215227
else: # it's an escaped "#"
216228
line = string.replace (line, "\\#", "#")
217229

@@ -232,7 +244,8 @@ def readline (self):
232244
if type (self.current_line) is ListType:
233245
self.current_line[1] = self.current_line[1] + 1
234246
else:
235-
self.current_line = [self.current_line, self.current_line+1]
247+
self.current_line = [self.current_line,
248+
self.current_line+1]
236249
# just an ordinary line, read it as usual
237250
else:
238251
if line is None: # eof
@@ -271,7 +284,7 @@ def readline (self):
271284
# well, I guess there's some actual content there: return it
272285
return line
273286

274-
# end readline
287+
# readline ()
275288

276289

277290
def readlines (self):
@@ -298,21 +311,26 @@ def unreadline (self, line):
298311
test_data = """# test file
299312
300313
line 3 \\
314+
# intervening comment
301315
continues on next line
302316
"""
303-
304-
305317
# result 1: no fancy options
306318
result1 = map (lambda x: x + "\n", string.split (test_data, "\n")[0:-1])
307319

308320
# result 2: just strip comments
309-
result2 = ["\n", "\n", "line 3 \\\n", " continues on next line\n"]
321+
result2 = ["\n",
322+
"line 3 \\\n",
323+
" continues on next line\n"]
310324

311325
# result 3: just strip blank lines
312-
result3 = ["# test file\n", "line 3 \\\n", " continues on next line\n"]
326+
result3 = ["# test file\n",
327+
"line 3 \\\n",
328+
"# intervening comment\n",
329+
" continues on next line\n"]
313330

314331
# result 4: default, strip comments, blank lines, and trailing whitespace
315-
result4 = ["line 3 \\", " continues on next line"]
332+
result4 = ["line 3 \\",
333+
" continues on next line"]
316334

317335
# result 5: strip comments and blanks, plus join lines (but don't
318336
# "collapse" joined lines

0 commit comments

Comments
 (0)