diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-26-12-00-01.gh-issue-151815.d4e5f6.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-26-12-00-01.gh-issue-151815.d4e5f6.rst new file mode 100644 index 000000000000000..66a9edefaf08ad4 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-26-12-00-01.gh-issue-151815.d4e5f6.rst @@ -0,0 +1 @@ +Fix a crash in t-string iteration when creating the iterator fails partway through due to memory pressure. diff --git a/Objects/templateobject.c b/Objects/templateobject.c index 1609e82b444516c..806f403bc5fa537 100644 --- a/Objects/templateobject.c +++ b/Objects/templateobject.c @@ -226,6 +226,9 @@ template_iter(PyObject *op) if (iter == NULL) { return NULL; } + iter->stringsiter = NULL; + iter->interpolationsiter = NULL; + iter->from_strings = 1; PyObject *stringsiter = PyObject_GetIter(self->strings); if (stringsiter == NULL) { @@ -242,7 +245,6 @@ template_iter(PyObject *op) iter->stringsiter = stringsiter; iter->interpolationsiter = interpolationsiter; - iter->from_strings = 1; PyObject_GC_Track(iter); return (PyObject *)iter; }