Closed bob-white closed 8 years ago
I see where you're going, I'm not sure its safe to leave a stack object in scope for that long: they are supposed to be prone to leaking because they contain a whole lot of state. I'm going to see if it's easier to add a conditional skip to Nested instead so we don't have to store a stack frame
Adding a simple conditional that just walks up the chain one more frame does work, that is how I was double checking if my guess as to why things were exploding on me.
Just felt kind of hacky, but I'd rather hacky than leaky.
Alternative, adding a classmethod to Nested that walks the mro, and counts the number entries that have __exit__
in their __dict__
. We then know exactly how many times we'd need to walk back up the stack to get to the defining scope.
No more reason to hang onto the stack frame, and if a control is created without the context manager none of this silliness would even apply.
I've updated the branch with this method.
Tweaked the stack walking portion. Don't actually need to get a count, instead we just inspect the next parent frame to see if a reference to the current instance exists. If it doesn't we're at the topmost.
Fixes an issue #28 on classes that override
Nested.__exit__
.By moving the call to
inspect.currentframe().f_back
intoNested.__new__
, we are able to grab the context scope at creation time, but still wait untilNested.__exit__
to actually inspect that scope for any names that we might need to capture.Granted this just ends up moving issue around, and if any classes override
Nested.__new__
we'd be right back where we started.