|
14 | 14 | import traceback |
15 | 15 | from test.support import TESTFN, unlink, run_unittest, check_warnings |
16 | 16 | from operator import neg |
| 17 | +import pickle |
17 | 18 | try: |
18 | 19 | import pty, signal |
19 | 20 | except ImportError: |
@@ -110,7 +111,30 @@ class TestFailingIter: |
110 | 111 | def __iter__(self): |
111 | 112 | raise RuntimeError |
112 | 113 |
|
| 114 | +def filter_char(arg): |
| 115 | + return ord(arg) > ord("d") |
| 116 | + |
| 117 | +def map_char(arg): |
| 118 | + return chr(ord(arg)+1) |
| 119 | + |
113 | 120 | class BuiltinTest(unittest.TestCase): |
| 121 | + # Helper to check picklability |
| 122 | + def check_iter_pickle(self, it, seq): |
| 123 | + itorg = it |
| 124 | + d = pickle.dumps(it) |
| 125 | + it = pickle.loads(d) |
| 126 | + self.assertEqual(type(itorg), type(it)) |
| 127 | + self.assertEqual(list(it), seq) |
| 128 | + |
| 129 | + #test the iterator after dropping one from it |
| 130 | + it = pickle.loads(d) |
| 131 | + try: |
| 132 | + next(it) |
| 133 | + except StopIteration: |
| 134 | + return |
| 135 | + d = pickle.dumps(it) |
| 136 | + it = pickle.loads(d) |
| 137 | + self.assertEqual(list(it), seq[1:]) |
114 | 138 |
|
115 | 139 | def test_import(self): |
116 | 140 | __import__('sys') |
@@ -566,6 +590,11 @@ def badfunc(): |
566 | 590 | self.assertEqual(list(filter(lambda x: x>=3, (1, 2, 3, 4))), [3, 4]) |
567 | 591 | self.assertRaises(TypeError, list, filter(42, (1, 2))) |
568 | 592 |
|
| 593 | + def test_filter_pickle(self): |
| 594 | + f1 = filter(filter_char, "abcdeabcde") |
| 595 | + f2 = filter(filter_char, "abcdeabcde") |
| 596 | + self.check_iter_pickle(f1, list(f2)) |
| 597 | + |
569 | 598 | def test_getattr(self): |
570 | 599 | self.assertTrue(getattr(sys, 'stdout') is sys.stdout) |
571 | 600 | self.assertRaises(TypeError, getattr, sys, 1) |
@@ -759,6 +788,11 @@ def badfunc(x): |
759 | 788 | raise RuntimeError |
760 | 789 | self.assertRaises(RuntimeError, list, map(badfunc, range(5))) |
761 | 790 |
|
| 791 | + def test_map_pickle(self): |
| 792 | + m1 = map(map_char, "Is this the real life?") |
| 793 | + m2 = map(map_char, "Is this the real life?") |
| 794 | + self.check_iter_pickle(m1, list(m2)) |
| 795 | + |
762 | 796 | def test_max(self): |
763 | 797 | self.assertEqual(max('123123'), '3') |
764 | 798 | self.assertEqual(max(1, 2, 3), 3) |
@@ -1300,6 +1334,13 @@ def __getitem__(self, i): |
1300 | 1334 | return i |
1301 | 1335 | self.assertRaises(ValueError, list, zip(BadSeq(), BadSeq())) |
1302 | 1336 |
|
| 1337 | + def test_zip_pickle(self): |
| 1338 | + a = (1, 2, 3) |
| 1339 | + b = (4, 5, 6) |
| 1340 | + t = [(1, 4), (2, 5), (3, 6)] |
| 1341 | + z1 = zip(a, b) |
| 1342 | + self.check_iter_pickle(z1, t) |
| 1343 | + |
1303 | 1344 | def test_format(self): |
1304 | 1345 | # Test the basic machinery of the format() builtin. Don't test |
1305 | 1346 | # the specifics of the various formatters |
|
0 commit comments