<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sun, Nov 6, 2016 at 8:53 AM, Nick Coghlan <span dir="ltr"><<a href="mailto:ncoghlan@gmail.com" target="_blank">ncoghlan@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 6 November 2016 at 16:07, Ram Rachum <<a href="mailto:ram@rachum.com">ram@rachum.com</a>> wrote:<br>
> Heh, I just played with this, and found a workaround. If I do something like<br>
> this after creating the generator:<br>
><br>
> sys.g = g<br>
><br>
> Then it wouldn't get closed when Python finishes, and the cleanup won't<br>
> happen, which is what I want.<br>
<br>
</span>The interpreter goes to significant lengths to make sure that finally<br>
clauses get executed prior to or during interpreter shutdown, and any<br>
means you find by which they don't get executed is considered a bug<br>
(not always a fixable bug, but a bug nonetheless). If you rely on<br>
those bugs and limitations to get your program to perform the way you<br>
want it to you're going to run into problems later when upgrading to<br>
new CPython versions, or trying out different interpreter<br>
implementations.<br></blockquote><div><br></div><div>I understand, and I agree with the reasoning. Still, I think I'll take my chances. </div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
There's still something seriously odd going in relation to your<br>
overall resource management architecture if "cleanup, maybe, unless I<br>
decide to tell you not to" is a behaviour you regularly need. Cleanup<br>
functions in a garbage collected environment should be idempotent, so<br>
it doesn't matter if you redundantly call them again later.<br></blockquote><div><br></div><div>Well, you think it's weird that I want a `finally` clause to not be called in some circumstances. Do you think it's equally weird to want an `__exit__` method that is not called in some circumstances?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
However, if you *do* need that pattern regularly, then the pattern<br>
itself can be encapsulated in a context manager:<br>
<br>
class callback_unless_exit:<br>
def __init__(self, callback):<br>
self.callback = callback<br>
def __enter__(self):<br>
return self<br>
def __exit__(self, exc_type, exc_value, exc_tb):<br>
if issubclass(exc_type, GeneratorExit):<br>
return<br>
self.callback()<br>
<br>
and then do:<br>
<br>
with callback_unless_exit(cleanup):<br>
yield<br>
<br>
in the context managers where you want that behaviour.<br><br></blockquote><div><br></div><div>Thanks for the workaround but I feel it's even less elegant than my original workaround. </div></div><br></div></div>