1 from __future__ import absolute_import, print_function |
1 from __future__ import absolute_import, print_function |
2 |
2 |
3 from mercurial import extensions |
3 from mercurial import extensions |
4 |
4 |
|
5 |
5 def genwrapper(x): |
6 def genwrapper(x): |
6 def f(orig, *args, **kwds): |
7 def f(orig, *args, **kwds): |
7 return [x] + orig(*args, **kwds) |
8 return [x] + orig(*args, **kwds) |
|
9 |
8 f.x = x |
10 f.x = x |
9 return f |
11 return f |
|
12 |
10 |
13 |
11 def getid(wrapper): |
14 def getid(wrapper): |
12 return getattr(wrapper, 'x', '-') |
15 return getattr(wrapper, 'x', '-') |
13 |
16 |
|
17 |
14 wrappers = [genwrapper(i) for i in range(5)] |
18 wrappers = [genwrapper(i) for i in range(5)] |
|
19 |
15 |
20 |
16 class dummyclass(object): |
21 class dummyclass(object): |
17 def getstack(self): |
22 def getstack(self): |
18 return ['orig'] |
23 return ['orig'] |
19 |
24 |
|
25 |
20 dummy = dummyclass() |
26 dummy = dummyclass() |
|
27 |
21 |
28 |
22 def batchwrap(wrappers): |
29 def batchwrap(wrappers): |
23 for w in wrappers: |
30 for w in wrappers: |
24 extensions.wrapfunction(dummy, 'getstack', w) |
31 extensions.wrapfunction(dummy, 'getstack', w) |
25 print('wrap %d: %s' % (getid(w), dummy.getstack())) |
32 print('wrap %d: %s' % (getid(w), dummy.getstack())) |
|
33 |
26 |
34 |
27 def batchunwrap(wrappers): |
35 def batchunwrap(wrappers): |
28 for w in wrappers: |
36 for w in wrappers: |
29 result = None |
37 result = None |
30 try: |
38 try: |
32 msg = str(dummy.getstack()) |
40 msg = str(dummy.getstack()) |
33 except (ValueError, IndexError) as e: |
41 except (ValueError, IndexError) as e: |
34 msg = e.__class__.__name__ |
42 msg = e.__class__.__name__ |
35 print('unwrap %s: %s: %s' % (getid(w), getid(result), msg)) |
43 print('unwrap %s: %s: %s' % (getid(w), getid(result), msg)) |
36 |
44 |
|
45 |
37 batchwrap(wrappers + [wrappers[0]]) |
46 batchwrap(wrappers + [wrappers[0]]) |
38 batchunwrap([(wrappers[i] if i is not None and i >= 0 else None) |
47 batchunwrap( |
39 for i in [3, None, 0, 4, 0, 2, 1, None]]) |
48 [ |
|
49 (wrappers[i] if i is not None and i >= 0 else None) |
|
50 for i in [3, None, 0, 4, 0, 2, 1, None] |
|
51 ] |
|
52 ) |
40 |
53 |
41 wrap0 = extensions.wrappedfunction(dummy, 'getstack', wrappers[0]) |
54 wrap0 = extensions.wrappedfunction(dummy, 'getstack', wrappers[0]) |
42 wrap1 = extensions.wrappedfunction(dummy, 'getstack', wrappers[1]) |
55 wrap1 = extensions.wrappedfunction(dummy, 'getstack', wrappers[1]) |
43 |
56 |
44 # Use them in a different order from how they were created to check that |
57 # Use them in a different order from how they were created to check that |
57 |
70 |
58 # Wrap callable object which has no __name__ |
71 # Wrap callable object which has no __name__ |
59 class callableobj(object): |
72 class callableobj(object): |
60 def __call__(self): |
73 def __call__(self): |
61 return ['orig'] |
74 return ['orig'] |
|
75 |
|
76 |
62 dummy.cobj = callableobj() |
77 dummy.cobj = callableobj() |
63 extensions.wrapfunction(dummy, 'cobj', wrappers[0]) |
78 extensions.wrapfunction(dummy, 'cobj', wrappers[0]) |
64 print('wrap callable object', dummy.cobj()) |
79 print('wrap callable object', dummy.cobj()) |