8000 gh-134119: Fix crash from calling next() on exhausted template iterat… · python/cpython@fc7f4c3 · GitHub
[go: up one dir, main page]

Skip to content

Commit fc7f4c3

Browse files
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>
1 parent 84914ad commit fc7f4c3

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

Lib/test/test_string/test_templatelib.py

+7
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ def test_iter(self):
148148
self.assertEqual(res[1].format_spec, '')
149149
self.assertEqual(res[2], ' yz')
150150

151+
def test_exhausted(self):
152+
# See https://github.com/python/cpython/issues/134119.
153+
template_iter = iter(t"{1}")
154+
self.assertIsInstance(next(template_iter), Interpolation)
155+
self.assertRaises(StopIteration, next, template_iter)
156+
self.assertRaises(StopIteration, next, template_iter)
157+
151158

152159
if __name__ == '__main__':
153160
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix crash when calling :func:`next` on an exhausted template string iterator.
2+
Patch by Jelle Zijlstra.

Objects/templateobject.c

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ templateiter_next(PyObject *op)
2323
if (self->from_strings) {
2424
item = PyIter_Next(self->stringsiter);
2525
self->from_strings = 0;
26+
if (item == NULL) {
27+
return NULL;
28+
}
2629
if (PyUnicode_GET_LENGTH(item) == 0) {
2730
Py_SETREF(item, PyIter_Next(self->interpolationsiter));
2831
self->from_strings = 1;

0 commit comments

Comments
 (0)
0