Changeset 391 for python/trunk/Lib/distutils/text_file.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/distutils/text_file.py
r2 r391 5 5 lines, and joining lines with backslashes.""" 6 6 7 __revision__ = "$Id: text_file.py 60923 2008-02-21 18:18:37Z guido.van.rossum $" 8 9 from types import * 10 import sys, os, string 7 __revision__ = "$Id$" 8 9 import sys 11 10 12 11 … … 138 137 line = self.current_line 139 138 outmsg.append(self.filename + ", ") 140 if type (line) in (ListType, TupleType):139 if isinstance(line, (list, tuple)): 141 140 outmsg.append("lines %d-%d: " % tuple (line)) 142 141 else: 143 142 outmsg.append("line %d: " % line) 144 143 outmsg.append(str(msg)) 145 return string.join(outmsg, "")144 return ''.join(outmsg) 146 145 147 146 … … 197 196 # lurking in there) and otherwise leave the line alone. 198 197 199 pos = string.find (line,"#")198 pos = line.find("#") 200 199 if pos == -1: # no "#" -- no comments 201 200 pass … … 220 219 # there 221 220 # result in "hello there". 222 if string.strip(line) == "":221 if line.strip() == "": 223 222 continue 224 223 225 224 else: # it's an escaped "#" 226 line = string.replace (line,"\\#", "#")225 line = line.replace("\\#", "#") 227 226 228 227 … … 236 235 237 236 if self.collapse_join: 238 line = string.lstrip (line)237 line = line.lstrip() 239 238 line = buildup_line + line 240 239 241 240 # careful: pay attention to line number when incrementing it 242 if type (self.current_line) is ListType:241 if isinstance(self.current_line, list): 243 242 self.current_line[1] = self.current_line[1] + 1 244 243 else: … … 251 250 252 251 # still have to be careful about incrementing the line number! 253 if type (self.current_line) is ListType:252 if isinstance(self.current_line, list): 254 253 self.current_line = self.current_line[1] + 1 255 254 else: … … 260 259 # trailing, or one or the other, or neither) 261 260 if self.lstrip_ws and self.rstrip_ws: 262 line = string.strip (line)261 line = line.strip() 263 262 elif self.lstrip_ws: 264 line = string.lstrip (line)263 line = line.lstrip() 265 264 elif self.rstrip_ws: 266 line = string.rstrip (line)265 line = line.rstrip() 267 266 268 267 # blank line (whether we rstrip'ed or not)? skip to next line … … 304 303 305 304 self.linebuf.append (line) 306 307 308 if __name__ == "__main__":309 test_data = """# test file310 311 line 3 \\312 # intervening comment313 continues on next line314 """315 # result 1: no fancy options316 result1 = map (lambda x: x + "\n", string.split (test_data, "\n")[0:-1])317 318 # result 2: just strip comments319 result2 = ["\n",320 "line 3 \\\n",321 " continues on next line\n"]322 323 # result 3: just strip blank lines324 result3 = ["# test file\n",325 "line 3 \\\n",326 "# intervening comment\n",327 " continues on next line\n"]328 329 # result 4: default, strip comments, blank lines, and trailing whitespace330 result4 = ["line 3 \\",331 " continues on next line"]332 333 # result 5: strip comments and blanks, plus join lines (but don't334 # "collapse" joined lines335 result5 = ["line 3 continues on next line"]336 337 # result 6: strip comments and blanks, plus join lines (and338 # "collapse" joined lines339 result6 = ["line 3 continues on next line"]340 341 def test_input (count, description, file, expected_result):342 result = file.readlines ()343 # result = string.join (result, '')344 if result == expected_result:345 print "ok %d (%s)" % (count, description)346 else:347 print "not ok %d (%s):" % (count, description)348 print "** expected:"349 print expected_result350 print "** received:"351 print result352 353 354 filename = "test.txt"355 out_file = open (filename, "w")356 out_file.write (test_data)357 out_file.close ()358 359 in_file = TextFile (filename, strip_comments=0, skip_blanks=0,360 lstrip_ws=0, rstrip_ws=0)361 test_input (1, "no processing", in_file, result1)362 363 in_file = TextFile (filename, strip_comments=1, skip_blanks=0,364 lstrip_ws=0, rstrip_ws=0)365 test_input (2, "strip comments", in_file, result2)366 367 in_file = TextFile (filename, strip_comments=0, skip_blanks=1,368 lstrip_ws=0, rstrip_ws=0)369 test_input (3, "strip blanks", in_file, result3)370 371 in_file = TextFile (filename)372 test_input (4, "default processing", in_file, result4)373 374 in_file = TextFile (filename, strip_comments=1, skip_blanks=1,375 join_lines=1, rstrip_ws=1)376 test_input (5, "join lines without collapsing", in_file, result5)377 378 in_file = TextFile (filename, strip_comments=1, skip_blanks=1,379 join_lines=1, rstrip_ws=1, collapse_join=1)380 test_input (6, "join lines with collapsing", in_file, result6)381 382 os.remove (filename)
Note:
See TracChangeset
for help on using the changeset viewer.