Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 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,15 @@ def test_iter(self):
self.assertEqual(res[1].format_spec, '')
self.assertEqual(res[2], ' yz')

def test_exhausted(self):
# gh-134119
template_iter = iter(t"{1}")
self.assertIsInstance(next(template_iter), Interpolation)
with self.assertRaises(StopIteration):
next(template_iter)
with 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 ``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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is worth to add condition here like

if (!item || PyUnicode_GET_LENGTH(item) == 0) {
  Py_XSETREF(item, PyItem_Next(self->interpolationsiter));
  self->from_strings = 1;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would potentially make an API call with an exception set, which is not allowed, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good point. Thanks!

Py_SETREF(item, PyIter_Next(self->interpolationsiter));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the result of PyIter_Next(self->interpolationsiter) be checked for NULL too?

Copy link
Contributor

@davepeck davepeck May 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That case just falls through to returning NULL safely, so it's probably fine as-is.

(The fall-through is intentional: since there is always one more string than interpolation, the first NULL returned by the iterator will always be here.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Py_SETREF is safe if it assigns to NULL, the behavior should be correct in that case.

self->from_strings = 1;
Expand Down
Loading