diff options
-rw-r--r-- | Lib/_abcoll.py | 2 | ||||
-rw-r--r-- | Lib/test/test_collections.py | 15 | ||||
-rw-r--r-- | Misc/NEWS | 3 |
3 files changed, 17 insertions, 3 deletions
diff --git a/Lib/_abcoll.py b/Lib/_abcoll.py index 73e668c11c..e7376e4a56 100644 --- a/Lib/_abcoll.py +++ b/Lib/_abcoll.py @@ -82,7 +82,7 @@ class Iterator(Iterable): @classmethod def __subclasshook__(cls, C): if cls is Iterator: - if _hasattr(C, "next"): + if _hasattr(C, "next") and _hasattr(C, "__iter__"): return True return NotImplemented diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 94305ab2f4..2d2319fe26 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -355,8 +355,19 @@ class TestOneTrickPonyABCs(ABCTestCase): for x in samples: self.assertIsInstance(x, Iterator) self.assertTrue(issubclass(type(x), Iterator), repr(type(x))) - self.validate_abstract_methods(Iterator, 'next') - self.validate_isinstance(Iterator, 'next') + self.validate_abstract_methods(Iterator, 'next', '__iter__') + + # Issue 10565 + class NextOnly: + def __next__(self): + yield 1 + raise StopIteration + self.assertNotIsInstance(NextOnly(), Iterator) + class NextOnlyNew(object): + def __next__(self): + yield 1 + raise StopIteration + self.assertNotIsInstance(NextOnlyNew(), Iterator) def test_Sized(self): non_samples = [None, 42, 3.14, 1j, @@ -22,6 +22,9 @@ What's New in Python 2.7.1? Library ------- +- Issue #10565: The collections.Iterator ABC now checks for both + ``__iter__`` and ``next``. + - Issue #10092: Properly reset locale in calendar.Locale*Calendar classes. - Issue #10459: Update CJK character names to Unicode 5.2. |