Dennis Sweeney <sweeney.dennis...@gmail.com> added the comment:
The save-a-boolean-for-each-context-manager approach has an issue if used with concurrent generators, where the lifetimes of two generator objects might be overlapping but not completely nested, as shown below. The same issue should arise when using multiple threads. The approach in no_gc.py with counting the times_disabled does not run into the same issue. >>> from contextlib import contextmanager >>> import gc >>> @contextmanager def no_gc(): was_enabled = gc.isenabled() try: yield finally: if was_enabled: gc.enable() >>> def gen1(): with no_gc(): yield "a" yield "b" yield "c" >>> def gen2(): with no_gc(): yield 1 yield 2 yield 3 >>> >>> g1 = gen1() >>> g2 = gen2() >>> next(g1) 'a' >>> next(g2) 1 >>> list(g1) ['b', 'c'] >>> gc.isenabled() # should be False! True >>> list(g2) [2, 3] ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue41545> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com