view tests/test-extensions-wrapfunction.py @ 30632:1d0e4832e616

convert: parse perforce data on-demand We are using read-only attributes that parse the perforce data on demand. We are reading the data only once whenever an attribute is requested and use it throughout the import process. This is equivalent to the previous behavior, but we are avoiding reading from perforce when we initialize the object, but instead run it during the actual import process, when the first attribute is requested (usually getheads(), see `convertcmd.convert`).
author David Soria Parra <davidsp@fb.com>
date Tue, 20 Dec 2016 09:23:50 -0800
parents 19578bb84731
children 47e52f079a57
line wrap: on
line source

from __future__ import absolute_import, print_function

from mercurial import extensions

def genwrapper(x):
    def f(orig, *args, **kwds):
        return [x] + orig(*args, **kwds)
    f.x = x
    return f

def getid(wrapper):
    return getattr(wrapper, 'x', '-')

wrappers = [genwrapper(i) for i in range(5)]

class dummyclass(object):
    def getstack(self):
        return ['orig']

dummy = dummyclass()

def batchwrap(wrappers):
    for w in wrappers:
        extensions.wrapfunction(dummy, 'getstack', w)
        print('wrap %d: %s' % (getid(w), dummy.getstack()))

def batchunwrap(wrappers):
    for w in wrappers:
        result = None
        try:
            result = extensions.unwrapfunction(dummy, 'getstack', w)
            msg = str(dummy.getstack())
        except (ValueError, IndexError) as e:
            msg = e.__class__.__name__
        print('unwrap %s: %s: %s' % (getid(w), getid(result), msg))

batchwrap(wrappers + [wrappers[0]])
batchunwrap([(wrappers[i] if i >= 0 else None)
             for i in [3, None, 0, 4, 0, 2, 1, None]])