[Python-ideas] Generator-based context managers can't skip __exit__

Ethan Furman ethan at stoneleaf.us
Sun Nov 6 23:19:19 EST 2016


On 11/06/2016 08:11 PM, Nick Coghlan wrote:
> On 7 November 2016 at 12:25, Ethan Furman <ethan at stoneleaf.us> wrote:
>> On 11/06/2016 12:44 AM, Ram Rachum wrote:
>>
>>> I see that Python does allow you to not call `__exit__` if you don't want
>>>   to [...]
>>
>> Um, how?  I was unaware of this (mis-)feature.
>
> It involves wrapping the context manager in another context manager
> that deliberately doesn't delegate the call to __exit__ in some cases
> (cf contextlib.ExitStack.pop_all()).

Ah, okay.

Perhaps a custom iterator class would suit Ram's needs better than using a generator shortcut.

--
~Ethan~


More information about the Python-ideas mailing list