diff options
author | Tim Peters <tim.peters@gmail.com> | 2001-05-01 20:45:31 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2001-05-01 20:45:31 (GMT) |
commit | f553f89d45f049e32f5eadc0c78a1e2440745d7a (patch) | |
tree | 085e1bcc3a073a4faac1e6cf32e88354142b2994 /Lib/test/test_iter.py | |
parent | 47668928e6f02713a1fbd27b434b08b08ca75d1b (diff) | |
download | cpython-f553f89d45f049e32f5eadc0c78a1e2440745d7a.zip cpython-f553f89d45f049e32f5eadc0c78a1e2440745d7a.tar.gz cpython-f553f89d45f049e32f5eadc0c78a1e2440745d7a.tar.bz2 |
Generalize list(seq) to work with iterators. This also generalizes list()
to no longer insist that len(seq) be defined.
NEEDS DOC CHANGES.
This is meant to be a model for how other functions of this ilk (max,
filter, etc) can be generalized similarly. Feel encouraged to grab your
favorite and convert it!
Note some cute consequences:
list(file) == file.readlines() == list(file.xreadlines())
list(dict) == dict.keys()
list(dict.iteritems()) = dict.items()
list(xrange(i, j, k)) == range(i, j, k)
Diffstat (limited to 'Lib/test/test_iter.py')
-rw-r--r-- | Lib/test/test_iter.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/Lib/test/test_iter.py b/Lib/test/test_iter.py index d4b8736..5b9bf65 100644 --- a/Lib/test/test_iter.py +++ b/Lib/test/test_iter.py @@ -243,4 +243,36 @@ class TestCase(unittest.TestCase): except OSError: pass + # Test list()'s use of iterators. + def test_builtin_list(self): + self.assertEqual(list(SequenceClass(5)), range(5)) + self.assertEqual(list(SequenceClass(0)), []) + self.assertEqual(list(()), []) + self.assertEqual(list(range(10, -1, -1)), range(10, -1, -1)) + + d = {"one": 1, "two": 2, "three": 3} + self.assertEqual(list(d), d.keys()) + + self.assertRaises(TypeError, list, list) + self.assertRaises(TypeError, list, 42) + + f = open(TESTFN, "w") + try: + for i in range(5): + f.write("%d\n" % i) + finally: + f.close() + f = open(TESTFN, "r") + try: + self.assertEqual(list(f), ["0\n", "1\n", "2\n", "3\n", "4\n"]) + f.seek(0, 0) + self.assertEqual(list(f.xreadlines()), + ["0\n", "1\n", "2\n", "3\n", "4\n"]) + finally: + f.close() + try: + unlink(TESTFN) + except OSError: + pass + run_unittest(TestCase) |