Show More
@@ -399,6 +399,21 b' def wrapfilecache(cls, propname, wrapper' | |||||
399 | raise AttributeError(r"type '%s' has no property '%s'" % ( |
|
399 | raise AttributeError(r"type '%s' has no property '%s'" % ( | |
400 | cls, propname)) |
|
400 | cls, propname)) | |
401 |
|
401 | |||
|
402 | class wrappedfunction(object): | |||
|
403 | '''context manager for temporarily wrapping a function''' | |||
|
404 | ||||
|
405 | def __init__(self, container, funcname, wrapper): | |||
|
406 | assert callable(wrapper) | |||
|
407 | self._container = container | |||
|
408 | self._funcname = funcname | |||
|
409 | self._wrapper = wrapper | |||
|
410 | ||||
|
411 | def __enter__(self): | |||
|
412 | wrapfunction(self._container, self._funcname, self._wrapper) | |||
|
413 | ||||
|
414 | def __exit__(self, exctype, excvalue, traceback): | |||
|
415 | unwrapfunction(self._container, self._funcname, self._wrapper) | |||
|
416 | ||||
402 | def wrapfunction(container, funcname, wrapper): |
|
417 | def wrapfunction(container, funcname, wrapper): | |
403 | '''Wrap the function named funcname in container |
|
418 | '''Wrap the function named funcname in container | |
404 |
|
419 |
@@ -37,3 +37,20 b' def batchunwrap(wrappers):' | |||||
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()) |
@@ -12,3 +12,9 b' unwrap 0: -: ValueError' | |||||
12 | unwrap 2: 2: [1, 'orig'] |
|
12 | unwrap 2: 2: [1, 'orig'] | |
13 | unwrap 1: 1: ['orig'] |
|
13 | unwrap 1: 1: ['orig'] | |
14 | unwrap -: -: IndexError |
|
14 | unwrap -: -: IndexError | |
|
15 | context manager ['orig'] | |||
|
16 | context manager [1, 'orig'] | |||
|
17 | context manager [0, 1, 'orig'] | |||
|
18 | context manager [2, 0, 1, 'orig'] | |||
|
19 | context manager [2, 1, 'orig'] | |||
|
20 | context manager [2, 'orig'] |
General Comments 0
You need to be logged in to leave comments.
Login now