]> granicus.if.org Git - python/commitdiff
Merged revisions 86857 via svnmerge from
authorAlexander Belopolsky <alexander.belopolsky@gmail.com>
Tue, 30 Nov 2010 01:18:17 +0000 (01:18 +0000)
committerAlexander Belopolsky <alexander.belopolsky@gmail.com>
Tue, 30 Nov 2010 01:18:17 +0000 (01:18 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r86857 | raymond.hettinger | 2010-11-28 22:56:12 -0500 (Sun, 28 Nov 2010) | 1 line

  Issue #10565:  Iterator ABC should require both __next__ and __iter__.
........

Lib/_abcoll.py
Lib/test/test_collections.py
Misc/NEWS

index 73e668c11c5f23ba075ef36e482fd4c437d71905..e7376e4a56af0d1ca8599c870bdd2b0396cc39fe 100644 (file)
@@ -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
 
index 94305ab2f41385216b1bd958e7b6abc10ccec32e..2d2319fe269b29bd6d1c34cef4871b8f49848eb4 100644 (file)
@@ -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,
index 7bac7f4efff8b8e6fbecc81d49646b9caf05a43d..47ddc7fd8547c429a3e8d8e7bbece2a18b37b2e6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -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.