|
| 1 | +import doctest |
| 2 | +import traceback |
| 3 | +import unittest |
| 4 | + |
| 5 | +from test.support import BrokenIter |
| 6 | + |
| 7 | + |
1 | 8 | doctests = """ |
2 | 9 | ########### Tests mostly copied from test_listcomps.py ############ |
3 | 10 |
|
|
144 | 151 |
|
145 | 152 | """ |
146 | 153 |
|
| 154 | +class SetComprehensionTest(unittest.TestCase): |
| 155 | + @unittest.expectedFailure # TODO: RUSTPYTHON; AttributeError: 'FrameSummary' object has no attribute 'end_lineno' |
| 156 | + def test_exception_locations(self): |
| 157 | + # The location of an exception raised from __init__ or |
| 158 | + # __next__ should should be the iterator expression |
| 159 | + |
| 160 | + def init_raises(): |
| 161 | + try: |
| 162 | + {x for x in BrokenIter(init_raises=True)} |
| 163 | + except Exception as e: |
| 164 | + return e |
| 165 | + |
| 166 | + def next_raises(): |
| 167 | + try: |
| 168 | + {x for x in BrokenIter(next_raises=True)} |
| 169 | + except Exception as e: |
| 170 | + return e |
| 171 | + |
| 172 | + def iter_raises(): |
| 173 | + try: |
| 174 | + {x for x in BrokenIter(iter_raises=True)} |
| 175 | + except Exception as e: |
| 176 | + return e |
| 177 | + |
| 178 | + for func, expected in [(init_raises, "BrokenIter(init_raises=True)"), |
| 179 | + (next_raises, "BrokenIter(next_raises=True)"), |
| 180 | + (iter_raises, "BrokenIter(iter_raises=True)"), |
| 181 | + ]: |
| 182 | + with self.subTest(func): |
| 183 | + exc = func() |
| 184 | + f = traceback.extract_tb(exc.__traceback__)[0] |
| 185 | + indent = 16 |
| 186 | + co = func.__code__ |
| 187 | + self.assertEqual(f.lineno, co.co_firstlineno + 2) |
| 188 | + self.assertEqual(f.end_lineno, co.co_firstlineno + 2) |
| 189 | + self.assertEqual(f.line[f.colno - indent : f.end_colno - indent], |
| 190 | + expected) |
147 | 191 |
|
148 | 192 | __test__ = {'doctests' : doctests} |
149 | 193 |
|
150 | | -def test_main(verbose=None): |
151 | | - import sys |
152 | | - from test import support |
153 | | - from test import test_setcomps |
154 | | - support.run_doctest(test_setcomps, verbose) |
155 | | - |
156 | | - # verify reference counting |
157 | | - if verbose and hasattr(sys, "gettotalrefcount"): |
158 | | - import gc |
159 | | - counts = [None] * 5 |
160 | | - for i in range(len(counts)): |
161 | | - support.run_doctest(test_setcomps, verbose) |
162 | | - gc.collect() |
163 | | - counts[i] = sys.gettotalrefcount() |
164 | | - print(counts) |
| 194 | +def load_tests(loader, tests, pattern): |
| 195 | + tests.addTest(doctest.DocTestSuite()) |
| 196 | + return tests |
| 197 | + |
165 | 198 |
|
166 | 199 | if __name__ == "__main__": |
167 | | - test_main(verbose=True) |
| 200 | + unittest.main() |
0 commit comments