mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
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>
This commit is contained in:
parent
84914ad0e5
commit
fc7f4c3666
3 changed files with 12 additions and 0 deletions
|
@ -148,6 +148,13 @@ class TemplateIterTests(unittest.TestCase):
|
||||||
self.assertEqual(res[1].format_spec, '')
|
self.assertEqual(res[1].format_spec, '')
|
||||||
self.assertEqual(res[2], ' yz')
|
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__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix crash when calling :func:`next` on an exhausted template string iterator.
|
||||||
|
Patch by Jelle Zijlstra.
|
|
@ -23,6 +23,9 @@ templateiter_next(PyObject *op)
|
||||||
if (self->from_strings) {
|
if (self->from_strings) {
|
||||||
item = PyIter_Next(self->stringsiter);
|
item = PyIter_Next(self->stringsiter);
|
||||||
self->from_strings = 0;
|
self->from_strings = 0;
|
||||||
|
if (item == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
if (PyUnicode_GET_LENGTH(item) == 0) {
|
if (PyUnicode_GET_LENGTH(item) == 0) {
|
||||||
Py_SETREF(item, PyIter_Next(self->interpolationsiter));
|
Py_SETREF(item, PyIter_Next(self->interpolationsiter));
|
||||||
self->from_strings = 1;
|
self->from_strings = 1;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue