diff options
| author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2024-08-22 09:22:39 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-22 09:22:39 (GMT) |
| commit | e4b91b7256036b23a9cc91638106810d4a6ddc7a (patch) | |
| tree | 3bcf7460554cc21b3d7c62ce6951fbd90e66b43c /Lib/test/test_setcomps.py | |
| parent | 50a595b37fe3201cd07701ffdda9d6baa6163562 (diff) | |
| download | cpython-e4b91b7256036b23a9cc91638106810d4a6ddc7a.zip cpython-e4b91b7256036b23a9cc91638106810d4a6ddc7a.tar.gz cpython-e4b91b7256036b23a9cc91638106810d4a6ddc7a.tar.bz2 | |
[3.13] gh-123142: Fix too wide source locations in tracebacks of exceptions from broken iterables in comprehensions (GH-123173). (#123209)
(cherry picked from commit ec89620e5e147ba028a46dd695ef073a72000b84)
Diffstat (limited to 'Lib/test/test_setcomps.py')
| -rw-r--r-- | Lib/test/test_setcomps.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/Lib/test/test_setcomps.py b/Lib/test/test_setcomps.py index 976fa88..ba4173c 100644 --- a/Lib/test/test_setcomps.py +++ b/Lib/test/test_setcomps.py @@ -1,6 +1,9 @@ import doctest +import traceback import unittest +from test.support import BrokenIter + doctests = """ ########### Tests mostly copied from test_listcomps.py ############ @@ -148,6 +151,35 @@ We also repeat each of the above scoping tests inside a function """ +class SetComprehensionTest(unittest.TestCase): + def test_exception_locations(self): + # The location of an exception raised from __init__ or + # __next__ should should be the iterator expression + + def init_raises(): + try: + {x for x in BrokenIter(init_raises=True)} + except Exception as e: + return e + + def next_raises(): + try: + {x for x in BrokenIter(next_raises=True)} + except Exception as e: + return e + + for func, expected in [(init_raises, "BrokenIter(init_raises=True)"), + (next_raises, "BrokenIter(next_raises=True)"), + ]: + with self.subTest(func): + exc = func() + f = traceback.extract_tb(exc.__traceback__)[0] + indent = 16 + co = func.__code__ + self.assertEqual(f.lineno, co.co_firstlineno + 2) + self.assertEqual(f.end_lineno, co.co_firstlineno + 2) + self.assertEqual(f.line[f.colno - indent : f.end_colno - indent], + expected) __test__ = {'doctests' : doctests} |
