@@ -201,8 +201,10 @@ def readline (self):
201
201
pos = string .find (line , "#" )
202
202
if pos == - 1 : # no "#" -- no comments
203
203
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 ] != "\\ " :
206
208
# Have to preserve the trailing newline, because it's
207
209
# the job of a later step (rstrip_ws) to remove it --
208
210
# and if rstrip_ws is false, we'd better preserve it!
@@ -212,6 +214,16 @@ def readline (self):
212
214
eol = (line [- 1 ] == '\n ' ) and '\n ' or ''
213
215
line = line [0 :pos ] + eol
214
216
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
+
215
227
else : # it's an escaped "#"
216
228
line = string .replace (line , "\\ #" , "#" )
217
229
@@ -232,7 +244,8 @@ def readline (self):
232
244
if type (self .current_line ) is ListType :
233
245
self .current_line [1 ] = self .current_line [1 ] + 1
234
246
else :
235
- self .current_line = [self .current_line , self .current_line + 1 ]
247
+ self .current_line = [self .current_line ,
248
+ self .current_line + 1 ]
236
249
# just an ordinary line, read it as usual
237
250
else :
238
251
if line is None : # eof
@@ -271,7 +284,7 @@ def readline (self):
271
284
# well, I guess there's some actual content there: return it
272
285
return line
273
286
274
- # end readline
287
+ # readline ()
275
288
276
289
277
290
def readlines (self ):
@@ -298,21 +311,26 @@ def unreadline (self, line):
298
311
test_data = """# test file
299
312
300
313
line 3 \\
314
+ # intervening comment
301
315
continues on next line
302
316
"""
303
-
304
-
305
317
# result 1: no fancy options
306
318
result1 = map (lambda x : x + "\n " , string .split (test_data , "\n " )[0 :- 1 ])
307
319
308
320
# 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 " ]
310
324
311
325
# 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 " ]
313
330
314
331
# 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" ]
316
334
317
335
# result 5: strip comments and blanks, plus join lines (but don't
318
336
# "collapse" joined lines
0 commit comments