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/Tools/scripts/pindent.py

    r2 r391  
    7777# - optionally do much more thorough reformatting, a la C indent
    7878
     79from __future__ import print_function
     80
    7981# Defaults
    8082STEPSIZE = 8
    8183TABSIZE = 8
    82 EXPANDTABS = 0
    83 
     84EXPANDTABS = False
     85
     86import io
    8487import re
    8588import sys
     
    9093next['try'] = 'except', 'finally'
    9194next['except'] = 'except', 'else', 'finally', 'end'
    92 next['else'] = next['finally'] = next['def'] = next['class'] = 'end'
     95next['else'] = next['finally'] = next['with'] = \
     96    next['def'] = next['class'] = 'end'
    9397next['end'] = ()
    9498start = 'if', 'while', 'for', 'try', 'with', 'def', 'class'
     
    106110        self._write = fpo.write
    107111        self.kwprog = re.compile(
    108                 r'^\s*(?P<kw>[a-z]+)'
    109                 r'(\s+(?P<id>[a-zA-Z_]\w*))?'
     112                r'^(?:\s|\\\n)*(?P<kw>[a-z]+)'
     113                r'((?:\s|\\\n)+(?P<id>[a-zA-Z_]\w*))?'
    110114                r'[^\w]')
    111115        self.endprog = re.compile(
    112                 r'^\s*#?\s*end\s+(?P<kw>[a-z]+)'
     116                r'^(?:\s|\\\n)*#?\s*end\s+(?P<kw>[a-z]+)'
    113117                r'(\s+(?P<id>[a-zA-Z_]\w*))?'
    114118                r'[^\w]')
     
    126130    def readline(self):
    127131        line = self.fpi.readline()
    128         if line: self.lineno = self.lineno + 1
     132        if line: self.lineno += 1
    129133        # end if
    130134        return line
     
    144148            if not line2: break
    145149            # end if
    146             line = line + line2
     150            line += line2
    147151        # end while
    148152        return line
    149153    # end def getline
    150154
    151     def putline(self, line, indent = None):
    152         if indent is None:
    153             self.write(line)
    154             return
    155         # end if
     155    def putline(self, line, indent):
    156156        tabs, spaces = divmod(indent*self.indentsize, self.tabsize)
    157         i = 0
    158         m = self.wsprog.match(line)
    159         if m: i = m.end()
    160         # end if
    161         self.write('\t'*tabs + ' '*spaces + line[i:])
     157        i = self.wsprog.match(line).end()
     158        line = line[i:]
     159        if line[:1] not in ('\n', '\r', ''):
     160            line = '\t'*tabs + ' '*spaces + line
     161        # end if
     162        self.write(line)
    162163    # end def putline
    163164
    164165    def reformat(self):
    165166        stack = []
    166         while 1:
     167        while True:
    167168            line = self.getline()
    168169            if not line: break      # EOF
     
    174175                if not stack:
    175176                    self.error('unexpected end')
    176                 elif stack[-1][0] != kw2:
     177                elif stack.pop()[0] != kw2:
    177178                    self.error('unmatched end')
    178179                # end if
    179                 del stack[-1:]
    180180                self.putline(line, len(stack))
    181181                continue
     
    209209        begin_counter = 0
    210210        end_counter = 0
    211         while 1:
     211        while True:
    212212            line = self.getline()
    213213            if not line: break      # EOF
     
    215215            m = self.endprog.match(line)
    216216            if m:
    217                 end_counter = end_counter + 1
     217                end_counter += 1
    218218                continue
    219219            # end if
     
    222222                kw = m.group('kw')
    223223                if kw in start:
    224                     begin_counter = begin_counter + 1
    225                 # end if
    226             # end if
    227             self.putline(line)
     224                    begin_counter += 1
     225                # end if
     226            # end if
     227            self.write(line)
    228228        # end while
    229229        if begin_counter - end_counter < 0:
     
    235235
    236236    def complete(self):
    237         self.indentsize = 1
    238237        stack = []
    239238        todo = []
    240         thisid = ''
    241         current, firstkw, lastkw, topid = 0, '', '', ''
    242         while 1:
     239        currentws = thisid = firstkw = lastkw = topid = ''
     240        while True:
    243241            line = self.getline()
    244             i = 0
    245             m = self.wsprog.match(line)
    246             if m: i = m.end()
    247             # end if
     242            i = self.wsprog.match(line).end()
    248243            m = self.endprog.match(line)
    249244            if m:
     
    270265                # end if
    271266            # end if
    272             indent = len(line[:i].expandtabs(self.tabsize))
     267            indentws = line[:i]
     268            indent = len(indentws.expandtabs(self.tabsize))
     269            current = len(currentws.expandtabs(self.tabsize))
    273270            while indent < current:
    274271                if firstkw:
     
    279276                        s = '# end %s\n' % firstkw
    280277                    # end if
    281                     self.putline(s, current)
     278                    self.write(currentws + s)
    282279                    firstkw = lastkw = ''
    283280                # end if
    284                 current, firstkw, lastkw, topid = stack[-1]
    285                 del stack[-1]
     281                currentws, firstkw, lastkw, topid = stack.pop()
     282                current = len(currentws.expandtabs(self.tabsize))
    286283            # end while
    287284            if indent == current and firstkw:
     
    298295                        s = '# end %s\n' % firstkw
    299296                    # end if
    300                     self.putline(s, current)
     297                    self.write(currentws + s)
    301298                    firstkw = lastkw = topid = ''
    302299                # end if
    303300            # end if
    304301            if indent > current:
    305                 stack.append((current, firstkw, lastkw, topid))
     302                stack.append((currentws, firstkw, lastkw, topid))
    306303                if thiskw and thiskw not in start:
    307304                    # error
    308305                    thiskw = ''
    309306                # end if
    310                 current, firstkw, lastkw, topid = \
    311                          indent, thiskw, thiskw, thisid
     307                currentws, firstkw, lastkw, topid = \
     308                          indentws, thiskw, thiskw, thisid
    312309            # end if
    313310            if thiskw:
     
    327324        # end while
    328325    # end def complete
    329 
    330326# end class PythonIndenter
    331327
     
    353349# end def reformat_filter
    354350
    355 class StringReader:
    356     def __init__(self, buf):
    357         self.buf = buf
    358         self.pos = 0
    359         self.len = len(self.buf)
    360     # end def __init__
    361     def read(self, n = 0):
    362         if n <= 0:
    363             n = self.len - self.pos
    364         else:
    365             n = min(n, self.len - self.pos)
    366         # end if
    367         r = self.buf[self.pos : self.pos + n]
    368         self.pos = self.pos + n
    369         return r
    370     # end def read
    371     def readline(self):
    372         i = self.buf.find('\n', self.pos)
    373         return self.read(i + 1 - self.pos)
    374     # end def readline
    375     def readlines(self):
    376         lines = []
    377         line = self.readline()
    378         while line:
    379             lines.append(line)
    380             line = self.readline()
    381         # end while
    382         return lines
    383     # end def readlines
    384     # seek/tell etc. are left as an exercise for the reader
    385 # end class StringReader
    386 
    387 class StringWriter:
    388     def __init__(self):
    389         self.buf = ''
    390     # end def __init__
    391     def write(self, s):
    392         self.buf = self.buf + s
    393     # end def write
    394     def getvalue(self):
    395         return self.buf
    396     # end def getvalue
    397 # end class StringWriter
    398 
    399351def complete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
    400     input = StringReader(source)
    401     output = StringWriter()
     352    input = io.BytesIO(source)
     353    output = io.BytesIO()
    402354    pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
    403355    pi.complete()
     
    406358
    407359def delete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
    408     input = StringReader(source)
    409     output = StringWriter()
     360    input = io.BytesIO(source)
     361    output = io.BytesIO()
    410362    pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
    411363    pi.delete()
     
    414366
    415367def reformat_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
    416     input = StringReader(source)
    417     output = StringWriter()
     368    input = io.BytesIO(source)
     369    output = io.BytesIO()
    418370    pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
    419371    pi.reformat()
     
    421373# end def reformat_string
    422374
     375def make_backup(filename):
     376    import os, os.path
     377    backup = filename + '~'
     378    if os.path.lexists(backup):
     379        try:
     380            os.remove(backup)
     381        except os.error:
     382            print("Can't remove backup %r" % (backup,), file=sys.stderr)
     383        # end try
     384    # end if
     385    try:
     386        os.rename(filename, backup)
     387    except os.error:
     388        print("Can't rename %r to %r" % (filename, backup), file=sys.stderr)
     389    # end try
     390# end def make_backup
     391
    423392def complete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
    424     source = open(filename, 'r').read()
     393    with open(filename, 'r') as f:
     394        source = f.read()
     395    # end with
    425396    result = complete_string(source, stepsize, tabsize, expandtabs)
    426397    if source == result: return 0
    427398    # end if
    428     import os
    429     try: os.rename(filename, filename + '~')
    430     except os.error: pass
    431     # end try
    432     f = open(filename, 'w')
    433     f.write(result)
    434     f.close()
     399    make_backup(filename)
     400    with open(filename, 'w') as f:
     401        f.write(result)
     402    # end with
    435403    return 1
    436404# end def complete_file
    437405
    438406def delete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
    439     source = open(filename, 'r').read()
     407    with open(filename, 'r') as f:
     408        source = f.read()
     409    # end with
    440410    result = delete_string(source, stepsize, tabsize, expandtabs)
    441411    if source == result: return 0
    442412    # end if
    443     import os
    444     try: os.rename(filename, filename + '~')
    445     except os.error: pass
    446     # end try
    447     f = open(filename, 'w')
    448     f.write(result)
    449     f.close()
     413    make_backup(filename)
     414    with open(filename, 'w') as f:
     415        f.write(result)
     416    # end with
    450417    return 1
    451418# end def delete_file
    452419
    453420def reformat_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
    454     source = open(filename, 'r').read()
     421    with open(filename, 'r') as f:
     422        source = f.read()
     423    # end with
    455424    result = reformat_string(source, stepsize, tabsize, expandtabs)
    456425    if source == result: return 0
    457426    # end if
    458     import os
    459     try: os.rename(filename, filename + '~')
    460     except os.error: pass
    461     # end try
    462     f = open(filename, 'w')
    463     f.write(result)
    464     f.close()
     427    make_backup(filename)
     428    with open(filename, 'w') as f:
     429        f.write(result)
     430    # end with
    465431    return 1
    466432# end def reformat_file
     
    475441-s stepsize: indentation step (default %(STEPSIZE)d)
    476442-t tabsize : the worth in spaces of a tab (default %(TABSIZE)d)
    477 -e         : expand TABs into spaces (defailt OFF)
     443-e         : expand TABs into spaces (default OFF)
    478444[file] ... : files are changed in place, with backups in file~
    479445If no files are specified or a single - is given,
     
    518484            tabsize = int(a)
    519485        elif o == '-e':
    520             expandtabs = 1
     486            expandtabs = True
    521487        # end if
    522488    # end for
Note: See TracChangeset for help on using the changeset viewer.