contrib/hgfixes/fix_bytesmod.py
author Durham Goode <durham@fb.com>
Fri, 11 Oct 2013 17:19:40 -0700
changeset 19887 dd7c294365f0
parent 19872 681f7b9213a4
child 20399 74daabdf5ab5
permissions -rw-r--r--
shelve: fix dirstate corruption during unshelve (issue4055) If you shelved on top of commit A, then rebased A to @ and unshelved, any file changed in A would appear as modified in hg status despite the contents not having changed. The fix is to use dirstate.setparents() instead of doing it manually. This will be a little slower since it has to iterate through everything in the dirstate instead of only what's in the mergestate, but this will be more correct since the mergestate did not include files which were merged but had no conflict. The tests also had several bad dirstate's hardcoded in them. This change updates the tests appropriately and adds a new test to cover this specific rebase case.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11749
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
     1
"""Fixer that changes bytes % whatever to a function that actually formats
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
     2
it."""
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
     3
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
     4
from lib2to3 import fixer_base
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
     5
from lib2to3.fixer_util import is_tuple, Call, Comma, Name, touch_import
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
     6
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
     7
# XXX: Implementing a blacklist in 2to3 turned out to be more troublesome than
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
     8
# blacklisting some modules inside the fixers. So, this is what I came with.
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
     9
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    10
blacklist = ['mercurial/demandimport.py',
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    11
             'mercurial/py3kcompat.py',
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    12
             'mercurial/i18n.py',
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    13
            ]
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    14
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    15
def isnumberremainder(formatstr, data):
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    16
    try:
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    17
        if data.value.isdigit():
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    18
            return True
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    19
    except AttributeError:
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    20
        return False
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    21
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    22
class FixBytesmod(fixer_base.BaseFix):
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    23
    # XXX: There's one case (I suppose) I can't handle: when a remainder
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    24
    # operation like foo % bar is performed, I can't really know what the
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    25
    # contents of foo and bar are. I believe the best approach is to "correct"
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    26
    # the to-be-converted code and let bytesformatter handle that case in
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    27
    # runtime.
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    28
    PATTERN = '''
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    29
              term< formatstr=STRING '%' data=STRING > |
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    30
              term< formatstr=STRING '%' data=atom > |
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    31
              term< formatstr=NAME '%' data=any > |
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    32
              term< formatstr=any '%' data=any >
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    33
              '''
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    34
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    35
    def transform(self, node, results):
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    36
        if self.filename in blacklist:
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    37
            return
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    38
        elif self.filename == 'mercurial/util.py':
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    39
            touch_import('.', 'py3kcompat', node=node)
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    40
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    41
        formatstr = results['formatstr'].clone()
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    42
        data = results['data'].clone()
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    43
        formatstr.prefix = '' # remove spaces from start
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    44
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    45
        if isnumberremainder(formatstr, data):
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    46
            return
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    47
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    48
        # We have two possibilities:
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    49
        # 1- An identifier or name is passed, it is going to be a leaf, thus, we
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    50
        #    just need to copy its value as an argument to the formatter;
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    51
        # 2- A tuple is explicitly passed. In this case, we're gonna explode it
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    52
        # to pass to the formatter
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    53
        # TODO: Check for normal strings. They don't need to be translated
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    54
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    55
        if is_tuple(data):
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    56
            args = [formatstr, Comma().clone()] + \
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    57
                   [c.clone() for c in data.children[:]]
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    58
        else:
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    59
            args = [formatstr, Comma().clone(), data]
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    60
19872
681f7b9213a4 check-code: check for spaces around = for named parameters
Mads Kiilerich <madski@unity3d.com>
parents: 11749
diff changeset
    61
        call = Call(Name('bytesformatter', prefix=' '), args)
11749
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    62
        return call
e627fef94604 hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
    63