| 9 | |
| 10 | |
| 11 | class _IterationGuard: |
| 12 | # This context manager registers itself in the current iterators of the |
| 13 | # weak container, such as to delay all removals until the context manager |
| 14 | # exits. |
| 15 | # This technique should be relatively thread-safe (since sets are). |
| 16 | |
| 17 | def __init__(self, weakcontainer): |
| 18 | # Don't create cycles |
| 19 | self.weakcontainer = ref(weakcontainer) |
| 20 | |
| 21 | def __enter__(self): |
| 22 | w = self.weakcontainer() |
| 23 | if w is not None: |
| 24 | w._iterating.add(self) |
| 25 | return self |
| 26 | |
| 27 | def __exit__(self, e, t, b): |
| 28 | w = self.weakcontainer() |
| 29 | if w is not None: |
| 30 | s = w._iterating |
| 31 | s.remove(self) |
| 32 | if not s: |
| 33 | w._commit_removals() |
| 34 | |
| 35 | |
| 36 | class WeakSet: |
no outgoing calls
no test coverage detected