A context manager which temporarily modifies the Python recursion limit. The testing framework, coverage, etc. may add an arbitrary number of levels to the depth. To maintain consistency in the tests, the current stack depth is determined when called, then added to the provided limit.
| 89 | |
| 90 | |
| 91 | class recursionlimit: |
| 92 | """ |
| 93 | A context manager which temporarily modifies the Python recursion limit. |
| 94 | |
| 95 | The testing framework, coverage, etc. may add an arbitrary number of levels to the depth. To maintain consistency |
| 96 | in the tests, the current stack depth is determined when called, then added to the provided limit. |
| 97 | |
| 98 | Example usage: |
| 99 | |
| 100 | ``` python |
| 101 | with recursionlimit(20): |
| 102 | # test code here |
| 103 | ``` |
| 104 | |
| 105 | See <https://stackoverflow.com/a/50120316/866026>. |
| 106 | """ |
| 107 | |
| 108 | def __init__(self, limit): |
| 109 | self.limit = util._get_stack_depth() + limit |
| 110 | self.old_limit = sys.getrecursionlimit() |
| 111 | |
| 112 | def __enter__(self): |
| 113 | sys.setrecursionlimit(self.limit) |
| 114 | |
| 115 | def __exit__(self, type, value, tb): |
| 116 | sys.setrecursionlimit(self.old_limit) |
| 117 | |
| 118 | |
| 119 | ######################### |
no outgoing calls