(self)
| 936 | |
| 937 | # Test iterators on RHS of unpacking assignments. |
| 938 | def test_unpack_iter(self): |
| 939 | a, b = 1, 2 |
| 940 | self.assertEqual((a, b), (1, 2)) |
| 941 | |
| 942 | a, b, c = IteratingSequenceClass(3) |
| 943 | self.assertEqual((a, b, c), (0, 1, 2)) |
| 944 | |
| 945 | try: # too many values |
| 946 | a, b = IteratingSequenceClass(3) |
| 947 | except ValueError: |
| 948 | pass |
| 949 | else: |
| 950 | self.fail("should have raised ValueError") |
| 951 | |
| 952 | try: # not enough values |
| 953 | a, b, c = IteratingSequenceClass(2) |
| 954 | except ValueError: |
| 955 | pass |
| 956 | else: |
| 957 | self.fail("should have raised ValueError") |
| 958 | |
| 959 | try: # not iterable |
| 960 | a, b, c = len |
| 961 | except TypeError: |
| 962 | pass |
| 963 | else: |
| 964 | self.fail("should have raised TypeError") |
| 965 | |
| 966 | a, b, c = {1: 42, 2: 42, 3: 42}.values() |
| 967 | self.assertEqual((a, b, c), (42, 42, 42)) |
| 968 | |
| 969 | f = open(TESTFN, "w", encoding="utf-8") |
| 970 | lines = ("a\n", "bb\n", "ccc\n") |
| 971 | try: |
| 972 | for line in lines: |
| 973 | f.write(line) |
| 974 | finally: |
| 975 | f.close() |
| 976 | f = open(TESTFN, "r", encoding="utf-8") |
| 977 | try: |
| 978 | a, b, c = f |
| 979 | self.assertEqual((a, b, c), lines) |
| 980 | finally: |
| 981 | f.close() |
| 982 | try: |
| 983 | unlink(TESTFN) |
| 984 | except OSError: |
| 985 | pass |
| 986 | |
| 987 | (a, b), (c,) = IteratingSequenceClass(2), {42: 24} |
| 988 | self.assertEqual((a, b, c), (0, 1, 42)) |
| 989 | |
| 990 | |
| 991 | @cpython_only |
nothing calls this directly
no test coverage detected