Skip to content

newnext is replacing exceptions raised inside iterator.__next__() #301

Open
@dphoyes

Description

@dphoyes

Failing test case:

from builtins import *
import unittest

def my_generator():
    yield None.some_var

class TestCase(unittest.TestCase):
    def test(self):
        with self.assertRaises(AttributeError):
            next(my_generator())

unittest.main()

Under Python3, this passes. Under Python2 without the builtins import, this passes. Under Python2, using newnext as provided by the builtins import, we get this failure:

Traceback (most recent call last):
  File "/home/davhoy01/tmp/futurebug.py", line 10, in test
    next(my_generator())
  File "/usr/lib/python2.7/dist-packages/future/builtins/newnext.py", line 62, in newnext
    iterator.__class__.__name__))
TypeError: 'generator' object is not an iterator

This is caused by the fact that newnext is trying to catch the AttributeError resulting from a non-existent __next__ attribute, and replacing the exception with a TypeError exception. But it's also inadvertently catching any AttributeError raised from inside the call to __next__, which ought to instead be propagated normally.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions