diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-09-02 14:53:39 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-02 14:53:39 (GMT) |
commit | 494181e44db809204f55eaebc88a255acb09a18f (patch) | |
tree | 328eae3eef4e77450bebabf780f64b217ae94ded /Lib/test/test_with.py | |
parent | 3b3a1a8e7ebbd824778d16fad457a3fa15a9f7e4 (diff) | |
download | cpython-494181e44db809204f55eaebc88a255acb09a18f.zip cpython-494181e44db809204f55eaebc88a255acb09a18f.tar.gz cpython-494181e44db809204f55eaebc88a255acb09a18f.tar.bz2 |
[3.13] gh-93691: fix too broad source locations of with-statement instructions (GH-120125) (#123604)
gh-93691: fix too broad source locations of with-statement instructions (GH-120125)
(cherry picked from commit eca3f7762c23b22a73a5e0b09520748c88aab4a0)
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Diffstat (limited to 'Lib/test/test_with.py')
-rw-r--r-- | Lib/test/test_with.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/Lib/test/test_with.py b/Lib/test/test_with.py index d819023..e8c4ddf 100644 --- a/Lib/test/test_with.py +++ b/Lib/test/test_with.py @@ -5,6 +5,7 @@ __author__ = "Mike Bland" __email__ = "mbland at acm dot org" import sys +import traceback import unittest from collections import deque from contextlib import _GeneratorContextManager, contextmanager, nullcontext @@ -749,5 +750,48 @@ class NestedWith(unittest.TestCase): self.assertEqual(10, b1) self.assertEqual(20, b2) + def testExceptionLocation(self): + # The location of an exception raised from + # __init__, __enter__ or __exit__ of a context + # manager should be just the context manager expression, + # pinpointing the precise context manager in case there + # is more than one. + + def init_raises(): + try: + with self.Dummy(), self.InitRaises() as cm, self.Dummy() as d: + pass + except Exception as e: + return e + + def enter_raises(): + try: + with self.EnterRaises(), self.Dummy() as d: + pass + except Exception as e: + return e + + def exit_raises(): + try: + with self.ExitRaises(), self.Dummy() as d: + pass + except Exception as e: + return e + + for func, expected in [(init_raises, "self.InitRaises()"), + (enter_raises, "self.EnterRaises()"), + (exit_raises, "self.ExitRaises()"), + ]: + 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) + + if __name__ == '__main__': unittest.main() |