gh-134119: Fix crash from calling next() on exhausted template iterator (#134120)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
Jelle Zijlstra 2025-05-17 12:23:19 -07:00 committed by GitHub
parent 84914ad0e5
commit fc7f4c3666
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 12 additions and 0 deletions

View file

@ -148,6 +148,13 @@ class TemplateIterTests(unittest.TestCase):
self.assertEqual(res[1].format_spec, '')
self.assertEqual(res[2], ' yz')
def test_exhausted(self):
# See https://github.com/python/cpython/issues/134119.
template_iter = iter(t"{1}")
self.assertIsInstance(next(template_iter), Interpolation)
self.assertRaises(StopIteration, next, template_iter)
self.assertRaises(StopIteration, next, template_iter)
if __name__ == '__main__':
unittest.main()