Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/distutils/text_file.py

    r2 r391  
    55lines, and joining lines with backslashes."""
    66
    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
     9import sys
    1110
    1211
     
    138137            line = self.current_line
    139138        outmsg.append(self.filename + ", ")
    140         if type (line) in (ListType, TupleType):
     139        if isinstance(line, (list, tuple)):
    141140            outmsg.append("lines %d-%d: " % tuple (line))
    142141        else:
    143142            outmsg.append("line %d: " % line)
    144143        outmsg.append(str(msg))
    145         return string.join(outmsg, "")
     144        return ''.join(outmsg)
    146145
    147146
     
    197196                # lurking in there) and otherwise leave the line alone.
    198197
    199                 pos = string.find (line, "#")
     198                pos = line.find("#")
    200199                if pos == -1:           # no "#" -- no comments
    201200                    pass
     
    220219                    #   there
    221220                    # result in "hello there".
    222                     if string.strip(line) == "":
     221                    if line.strip() == "":
    223222                        continue
    224223
    225224                else:                   # it's an escaped "#"
    226                     line = string.replace (line, "\\#", "#")
     225                    line = line.replace("\\#", "#")
    227226
    228227
     
    236235
    237236                if self.collapse_join:
    238                     line = string.lstrip (line)
     237                    line = line.lstrip()
    239238                line = buildup_line + line
    240239
    241240                # careful: pay attention to line number when incrementing it
    242                 if type (self.current_line) is ListType:
     241                if isinstance(self.current_line, list):
    243242                    self.current_line[1] = self.current_line[1] + 1
    244243                else:
     
    251250
    252251                # 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):
    254253                    self.current_line = self.current_line[1] + 1
    255254                else:
     
    260259            # trailing, or one or the other, or neither)
    261260            if self.lstrip_ws and self.rstrip_ws:
    262                 line = string.strip (line)
     261                line = line.strip()
    263262            elif self.lstrip_ws:
    264                 line = string.lstrip (line)
     263                line = line.lstrip()
    265264            elif self.rstrip_ws:
    266                 line = string.rstrip (line)
     265                line = line.rstrip()
    267266
    268267            # blank line (whether we rstrip'ed or not)? skip to next line
     
    304303
    305304        self.linebuf.append (line)
    306 
    307 
    308 if __name__ == "__main__":
    309     test_data = """# test file
    310 
    311 line 3 \\
    312 # intervening comment
    313   continues on next line
    314 """
    315     # result 1: no fancy options
    316     result1 = map (lambda x: x + "\n", string.split (test_data, "\n")[0:-1])
    317 
    318     # result 2: just strip comments
    319     result2 = ["\n",
    320                "line 3 \\\n",
    321                "  continues on next line\n"]
    322 
    323     # result 3: just strip blank lines
    324     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 whitespace
    330     result4 = ["line 3 \\",
    331                "  continues on next line"]
    332 
    333     # result 5: strip comments and blanks, plus join lines (but don't
    334     # "collapse" joined lines
    335     result5 = ["line 3   continues on next line"]
    336 
    337     # result 6: strip comments and blanks, plus join lines (and
    338     # "collapse" joined lines
    339     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_result
    350             print "** received:"
    351             print result
    352 
    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.