diff options
author | Tim Peters <tim.peters@gmail.com> | 2001-05-06 01:05:02 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2001-05-06 01:05:02 (GMT) |
commit | 8572b4fedf7e6ee4cd350680d53cd0a21574b083 (patch) | |
tree | d8f3f982d9b35dffb659ec228ae53628bf85a56e /Lib/test/test_iter.py | |
parent | ef0c42d4e5d0d7493395b1e3c37fe53dc48a377f (diff) | |
download | cpython-8572b4fedf7e6ee4cd350680d53cd0a21574b083.zip cpython-8572b4fedf7e6ee4cd350680d53cd0a21574b083.tar.gz cpython-8572b4fedf7e6ee4cd350680d53cd0a21574b083.tar.bz2 |
Generalize zip() to work with iterators.
NEEDS DOC CHANGES.
More AttributeErrors transmuted into TypeErrors, in test_b2.py, and,
again, this strikes me as a good thing.
This checkin completes the iterator generalization work that obviously
needed to be done. Can anyone think of others that should be changed?
Diffstat (limited to 'Lib/test/test_iter.py')
-rw-r--r-- | Lib/test/test_iter.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/Lib/test/test_iter.py b/Lib/test/test_iter.py index a50c74f..ddc58a7 100644 --- a/Lib/test/test_iter.py +++ b/Lib/test/test_iter.py @@ -418,6 +418,52 @@ class TestCase(unittest.TestCase): except OSError: pass + # Test zip()'s use of iterators. + def test_builtin_zip(self): + self.assertRaises(TypeError, zip) + self.assertRaises(TypeError, zip, None) + self.assertRaises(TypeError, zip, range(10), 42) + self.assertRaises(TypeError, zip, range(10), zip) + + self.assertEqual(zip(IteratingSequenceClass(3)), + [(0,), (1,), (2,)]) + self.assertEqual(zip(SequenceClass(3)), + [(0,), (1,), (2,)]) + + d = {"one": 1, "two": 2, "three": 3} + self.assertEqual(d.items(), zip(d, d.itervalues())) + + # Generate all ints starting at constructor arg. + class IntsFrom: + def __init__(self, start): + self.i = start + + def __iter__(self): + return self + + def next(self): + i = self.i + self.i = i+1 + return i + + f = open(TESTFN, "w") + try: + f.write("a\n" "bbb\n" "cc\n") + finally: + f.close() + f = open(TESTFN, "r") + try: + self.assertEqual(zip(IntsFrom(0), f, IntsFrom(-100)), + [(0, "a\n", -100), + (1, "bbb\n", -99), + (2, "cc\n", -98)]) + finally: + f.close() + try: + unlink(TESTFN) + except OSError: + pass + # Test reduces()'s use of iterators. def test_builtin_reduce(self): from operator import add |