[Python-ideas] PEP 479: Change StopIteration handling inside generators

Chris Angelico rosuav at gmail.com
Thu Nov 20 06:54:20 CET 2014


On Thu, Nov 20, 2014 at 2:44 PM, Chris Angelico <rosuav at gmail.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.

ChrisA
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pep0479.patch
Type: text/x-patch
Size: 4590 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141120/9549ff50/attachment.bin>


More information about the Python-ideas mailing list