From 3274eeea5775e6efd49e8ca2c06ea5e4c140c40e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sun, 12 Oct 2025 11:00:14 +0200 Subject: [PATCH] fix a leak when failing to create a Union type --- .../2025-10-12-11-00-06.gh-issue-139988.4wi51t.rst | 2 ++ Objects/unionobject.c | 2 ++ 2 files changed, 4 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-10-12-11-00-06.gh-issue-139988.4wi51t.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-10-12-11-00-06.gh-issue-139988.4wi51t.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-10-12-11-00-06.gh-issue-139988.4wi51t.rst new file mode 100644 index 00000000000000..60fa3b1d339cb1 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-10-12-11-00-06.gh-issue-139988.4wi51t.rst @@ -0,0 +1,2 @@ +Fix a memory leak when failing to create a :class:`~typing.Union` type. +Patch by Bénédikt Tran. diff --git a/Objects/unionobject.c b/Objects/unionobject.c index 2206ed80ef03fd..c4ece0fe09f018 100644 --- a/Objects/unionobject.c +++ b/Objects/unionobject.c @@ -474,11 +474,13 @@ _Py_union_from_tuple(PyObject *args) } if (PyTuple_CheckExact(args)) { if (!unionbuilder_add_tuple(&ub, args)) { + unionbuilder_finalize(&ub); return NULL; } } else { if (!unionbuilder_add_single(&ub, args)) { + unionbuilder_finalize(&ub); return NULL; } }