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

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 17, 2025
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
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) {
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
Contributor

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