This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Dennis Sweeney
Recipients Dennis Sweeney, Yonatan Goldschmidt, ammar2, iritkatriel, steven.daprano
Date 2020-08-17.16:21:21
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1597681281.46.0.568014999176.issue41545@roundup.psfhosted.org>
In-reply-to
Content
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]
History
Date User Action Args
2020-08-17 16:21:21Dennis Sweeneysetrecipients: + Dennis Sweeney, steven.daprano, ammar2, Yonatan Goldschmidt, iritkatriel
2020-08-17 16:21:21Dennis Sweeneysetmessageid: <1597681281.46.0.568014999176.issue41545@roundup.psfhosted.org>
2020-08-17 16:21:21Dennis Sweeneylinkissue41545 messages
2020-08-17 16:21:21Dennis Sweeneycreate