|
| 1 | +__license__ = """ |
| 2 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 3 | +of this software and associated documentation files (the "Software"), to deal |
| 4 | +in the Software without restriction, including without limitation the rights |
| 5 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 6 | +copies of the Software, and to permit persons to whom the Software is |
| 7 | +furnished to do so, subject to the following conditions: |
| 8 | +
|
| 9 | +The above copyright notice and this permission notice shall be included in |
| 10 | +all copies or substantial portions of the Software. |
| 11 | +
|
| 12 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 13 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 14 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 15 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 16 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 17 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 18 | +THE SOFTWARE. |
| 19 | +""" |
| 20 | + |
| 21 | + |
| 22 | +__doc__ = """ |
| 23 | +Debugging helpers |
| 24 | +================= |
| 25 | +
|
| 26 | +.. autofunction:: make_unique_filesystem_object |
| 27 | +.. autofunction:: open_unique_debug_file |
| 28 | +.. autofunction:: refdebug |
| 29 | +.. autofunction:: get_object_cycles |
| 30 | +.. autofunction:: estimate_memory_usage |
| 31 | +
|
| 32 | +""" |
| 33 | + |
1 | 34 | import sys |
2 | 35 | from typing import Collection, List, Set |
3 | 36 |
|
@@ -142,13 +175,28 @@ def is_excluded(o): |
142 | 175 |
|
143 | 176 | # {{{ Find circular references |
144 | 177 |
|
145 | | -# https://code.activestate.com/recipes/523004-find-cyclical-references/ |
| 178 | +# Based on https://code.activestate.com/recipes/523004-find-cyclical-references/ |
146 | 179 |
|
147 | 180 | def get_object_cycles(objects: Collection[object]) -> List[List[object]]: |
148 | 181 | """ |
149 | | - :param objects: A collection of objects to find cycles in. It is often |
150 | | - useful to pass in gc.garbage to find the cycles that are preventing some |
151 | | - objects from being garbage collected. |
| 182 | + Find circular references in *objects*. This can be useful for example to debug |
| 183 | + why certain objects need to be freed via garbage collection instead of |
| 184 | + reference counting. |
| 185 | +
|
| 186 | + :arg objects: A collection of objects to find cycles in. A potential way |
| 187 | + to find a list of objects potentially containing cycles from the garbage |
| 188 | + collector is the following code:: |
| 189 | +
|
| 190 | + gc.set_debug(gc.DEBUG_SAVEALL) |
| 191 | + gc.collect() |
| 192 | + gc.set_debug(0) |
| 193 | + obj_list = gc.garbage |
| 194 | +
|
| 195 | + from pytools.debug import get_object_cycles |
| 196 | + print(get_object_cycles(obj_list)) |
| 197 | +
|
| 198 | + :returns: A :class:`list` in which each element contains a :class:`list` |
| 199 | + of objects forming a cycle. |
152 | 200 | """ |
153 | 201 | def recurse(obj: object, start: object, all_objs: Set[object], |
154 | 202 | current_path: List[object]) -> None: |
|
0 commit comments