(self)
| 883 | |
| 884 | # Test iterators with file.writelines(). |
| 885 | def test_writelines(self): |
| 886 | f = open(TESTFN, "w", encoding="utf-8") |
| 887 | |
| 888 | try: |
| 889 | self.assertRaises(TypeError, f.writelines, None) |
| 890 | self.assertRaises(TypeError, f.writelines, 42) |
| 891 | |
| 892 | f.writelines(["1\n", "2\n"]) |
| 893 | f.writelines(("3\n", "4\n")) |
| 894 | f.writelines({'5\n': None}) |
| 895 | f.writelines({}) |
| 896 | |
| 897 | # Try a big chunk too. |
| 898 | class Iterator: |
| 899 | def __init__(self, start, finish): |
| 900 | self.start = start |
| 901 | self.finish = finish |
| 902 | self.i = self.start |
| 903 | |
| 904 | def __next__(self): |
| 905 | if self.i >= self.finish: |
| 906 | raise StopIteration |
| 907 | result = str(self.i) + '\n' |
| 908 | self.i += 1 |
| 909 | return result |
| 910 | |
| 911 | def __iter__(self): |
| 912 | return self |
| 913 | |
| 914 | class Whatever: |
| 915 | def __init__(self, start, finish): |
| 916 | self.start = start |
| 917 | self.finish = finish |
| 918 | |
| 919 | def __iter__(self): |
| 920 | return Iterator(self.start, self.finish) |
| 921 | |
| 922 | f.writelines(Whatever(6, 6+2000)) |
| 923 | f.close() |
| 924 | |
| 925 | f = open(TESTFN, encoding="utf-8") |
| 926 | expected = [str(i) + "\n" for i in range(1, 2006)] |
| 927 | self.assertEqual(list(f), expected) |
| 928 | |
| 929 | finally: |
| 930 | f.close() |
| 931 | try: |
| 932 | unlink(TESTFN) |
| 933 | except OSError: |
| 934 | pass |
| 935 | |
| 936 | |
| 937 | # Test iterators on RHS of unpacking assignments. |
nothing calls this directly
no test coverage detected