Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JelleZijlstra merged 2 commits intopython:mainfromJelleZijlstra:templateiter-crash
May 17, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsLib/test/test_string/test_templatelib.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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()
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsObjects/templateobject.c
View file
Open in desktop
Original file line numberDiff line numberDiff 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
MemberAuthor

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 ofPyIter_Next(self->interpolationsiter) be checked forNULL too?

Copy link
Contributor

@davepeckdavepeckMay 17, 2025
edited
Loading

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 returningNULL safely, so it's probably fine as-is.

(The fall-through is intentional: since there is always one morestring thaninterpolation, the firstNULL returned by the iterator will always be here.)

devdanzin, lysnikolaou, and sergey-miryanov reacted with thumbs up emoji
Copy link
MemberAuthor

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

[8]ページ先頭

©2009-2025 Movatter.jp