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

Ram Rachum ram at rachum.com
Fri Nov 18 15:42:29 EST 2016


Sure, here are a couple of use cases:

1. I'm making a program that lets people lease machines. They can issue a
command to lease 7 machines. When they do, my program leases them one by
one and adds them all to an exit stack, so in case there aren't 7 machines
available, all the machines we leased get released and the situation is
back to normal. If everything goes fine, I do pop_all on the exit stack so
it doesn't get exited and the machines stay leased, then the command exits
and the user gets his machines.

2. I have a test suite that creates a temporary folder to put files that
are used by the test. The temporary folder is created by a context manager
that deletes it at the end. But, if the test fails I want to move the
temporary folder away into a dedicated folder for the user to be able to
examine those files later to figure out why the test fails. So I want to
tell the temporary folder context manager to not delete the folder, because
it'll fail since it was moved away so it's not at the expected location.

(If you're replying please keep me in "to" because the Gmail filter I set
up isn't smart enough to let the messages in otherwise.)

On Nov 18, 2016 21:27, "Sven R. Kunze" <srkunze at mail.de> wrote:

> On 06.11.2016 09:07, Steven D'Aprano wrote:
>
>> I'm having a lot of difficulty in understanding your use-case here, and
>> so maybe I've completely misunderstood something.
>>
>
> Although, this thread is dead for a week or so, I am still curious to hear
> the real-world use-case. I am equally puzzled by the fact that somebody
> really wants to use a context manager not to work like a context manager;
> without even considering not to use context managers at all and using
> regular functions instead.
>
> Cheers,
> Sven
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161118/3fb4154e/attachment.html>


More information about the Python-ideas mailing list