Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-134119: Fix crash from calling next() on exhausted template iterat…
…or (GH-134120)

(cherry picked from commit fc7f4c3)

Co-authored-by: Jelle Zijlstra <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
  • Loading branch information
2 people authored and miss-islington committed May 17, 2025
commit 519a795677b4c75bf76ce4b0ce5e47f41dce70a8
7 changes: 7 additions & 0 deletions Lib/test/test_string/test_templatelib.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ def test_iter(self):
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()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash when calling :func:`next` on an exhausted template string iterator.
Patch by Jelle Zijlstra.
3 changes: 3 additions & 0 deletions Objects/templateobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ templateiter_next(PyObject *op)
if (self->from_strings) {
item = PyIter_Next(self->stringsiter);
self->from_strings = 0;
if (item == NULL) {
return NULL;
}
if (PyUnicode_GET_LENGTH(item) == 0) {
Py_SETREF(item, PyIter_Next(self->interpolationsiter));
self->from_strings = 1;
Expand Down
Loading

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant