On Thu, Nov 20, 2014 at 2:44 PM, Chris Angelico email@example.com wrote:
I poked around a bit in the code and managed to come up with this. It doesn't chain the previous exception, so the traceback is a little scanty, but it does turn a StopIteration into a RuntimeError. (It might also leak the original StopIteration. I'm not sure.) Prior to this patch, I had 377 of 390 tests passing flawlessly and no failures (just skips and warnings); with this applied, six failures.
With the attached demo patch, all tests pass except test_generators, which explicitly tests stuff about the correlation between return and StopIteration. There's the contextlib changes, a couple of places that were raising StopIteration and should be returning, and a couple that were letting StopIteration bubble and now need to catch it and return. I've deliberately not followed PEP 8 here, in the interests of minimizing diff size; in several cases, blocks of code ought to be indented a level, but I cheated and created a half-indentation to show how little actually changes.
If anyone would care to try this on their own codebases, that'd be helpful.