Mercurial > public > mercurial-scm > hg-stable
comparison tests/test-extensions-wrapfunction.py @ 34032:47e52f079a57
extensions: add wrappedfunction() context manager
Several extensions exist that temporarily want to wrap a function (at
least narrowhg, any many of the extensions in hg-experimental). That's
why we have the unwrapfunction() that was introduced in 19578bb84731
(extensions: add unwrapfunction to undo wrapfunction, 2016-08-10).
This patch adds a simple wrappedfunction() that returns a context
manager.
Differential Revision: https://phab.mercurial-scm.org/D472
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Mon, 21 Aug 2017 16:46:05 -0700 |
parents | 19578bb84731 |
children | 82bd4c5a81e5 |
comparison
equal
deleted
inserted
replaced
34031:da07367d683b | 34032:47e52f079a57 |
---|---|
35 print('unwrap %s: %s: %s' % (getid(w), getid(result), msg)) | 35 print('unwrap %s: %s: %s' % (getid(w), getid(result), msg)) |
36 | 36 |
37 batchwrap(wrappers + [wrappers[0]]) | 37 batchwrap(wrappers + [wrappers[0]]) |
38 batchunwrap([(wrappers[i] if i >= 0 else None) | 38 batchunwrap([(wrappers[i] if i >= 0 else None) |
39 for i in [3, None, 0, 4, 0, 2, 1, None]]) | 39 for i in [3, None, 0, 4, 0, 2, 1, None]]) |
40 | |
41 wrap0 = extensions.wrappedfunction(dummy, 'getstack', wrappers[0]) | |
42 wrap1 = extensions.wrappedfunction(dummy, 'getstack', wrappers[1]) | |
43 | |
44 # Use them in a different order from how they were created to check that | |
45 # the wrapping happens in __enter__, not in __init__ | |
46 print('context manager', dummy.getstack()) | |
47 with wrap1: | |
48 print('context manager', dummy.getstack()) | |
49 with wrap0: | |
50 print('context manager', dummy.getstack()) | |
51 # Bad programmer forgets to unwrap the function, but the context | |
52 # managers still unwrap their wrappings. | |
53 extensions.wrapfunction(dummy, 'getstack', wrappers[2]) | |
54 print('context manager', dummy.getstack()) | |
55 print('context manager', dummy.getstack()) | |
56 print('context manager', dummy.getstack()) |