From c86f144cef51eb226c28bb6fb9a6dc7727cfbbf2 Mon Sep 17 00:00:00 2001 From: Tan Long Date: Fri, 27 Nov 2026 16:14:18 +0800 Subject: [PATCH 1/4] add strong references to arguments of `delta_new_impl`. --- Modules/_datetimemodule.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 64ed9ef0787180..e8a5985159fae8 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -2803,6 +2803,16 @@ delta_new_impl(PyTypeObject *type, PyObject *days, PyObject *seconds, return NULL; } + /* vectorcall passes borrowed references; hold strong references before + any conversion can execute arbitrary Python code. */ + Py_XINCREF(days); + Py_XINCREF(seconds); + Py_XINCREF(microseconds); + Py_XINCREF(milliseconds); + Py_XINCREF(minutes); + Py_XINCREF(hours); + Py_XINCREF(weeks); + PyObject *x = NULL; /* running sum of microseconds */ PyObject *y = NULL; /* temp sum of microseconds */ double leftover_us = 0.0; @@ -2886,6 +2896,13 @@ delta_new_impl(PyTypeObject *type, PyObject *days, PyObject *seconds, Py_DECREF(x); Done: + Py_XDECREF(days); + Py_XDECREF(seconds); + Py_XDECREF(microseconds); + Py_XDECREF(milliseconds); + Py_XDECREF(minutes); + Py_XDECREF(hours); + Py_XDECREF(weeks); RELEASE_CURRENT_STATE(st, current_mod); return self; From 9e7148ba998a0475c7c98d9c114a7cfbc6a90d54 Mon Sep 17 00:00:00 2001 From: Tan Long Date: Sat, 28 Nov 2026 04:32:22 +0800 Subject: [PATCH 2/4] move Py_XINCREF before GET_CURRENT_STATE --- Modules/_datetimemodule.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index e8a5985159fae8..2a6e0731cce6fe 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -2795,14 +2795,6 @@ delta_new_impl(PyTypeObject *type, PyObject *days, PyObject *seconds, PyObject *minutes, PyObject *hours, PyObject *weeks) /*[clinic end generated code: output=61d7e02a92a97700 input=8aebc4cdef360d1f]*/ { - PyObject *self = NULL; - - PyObject *current_mod; - datetime_state *st = GET_CURRENT_STATE(current_mod); - if (st == NULL) { - return NULL; - } - /* vectorcall passes borrowed references; hold strong references before any conversion can execute arbitrary Python code. */ Py_XINCREF(days); @@ -2813,6 +2805,14 @@ delta_new_impl(PyTypeObject *type, PyObject *days, PyObject *seconds, Py_XINCREF(hours); Py_XINCREF(weeks); + PyObject *self = NULL; + + PyObject *current_mod; + datetime_state *st = GET_CURRENT_STATE(current_mod); + if (st == NULL) { + return NULL; + } + PyObject *x = NULL; /* running sum of microseconds */ PyObject *y = NULL; /* temp sum of microseconds */ double leftover_us = 0.0; @@ -4365,7 +4365,15 @@ static PyObject * timezone_new_impl(PyTypeObject *type, PyObject *offset, PyObject *name) /*[clinic end generated code: output=41a2dda500424187 input=8901a8befbc52561]*/ { - return new_timezone(offset, name); + /* vectorcall passes borrowed references; hold strong references before + new_timezone() can execute arbitrary Python code. */ + PyObject *result; + Py_INCREF(offset); + Py_XINCREF(name); + result = new_timezone(offset, name); + Py_DECREF(offset); + Py_XDECREF(name); + return result; } static void From ffe7e74f15e85c822c2f73c1b0ee5780b30d0691 Mon Sep 17 00:00:00 2001 From: Tan Long Date: Sat, 28 Nov 2026 04:51:57 +0800 Subject: [PATCH 3/4] ensure calling Py_XDECREF --- Modules/_datetimemodule.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 2a6e0731cce6fe..552d5d1830d302 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -2810,7 +2810,7 @@ delta_new_impl(PyTypeObject *type, PyObject *days, PyObject *seconds, PyObject *current_mod; datetime_state *st = GET_CURRENT_STATE(current_mod); if (st == NULL) { - return NULL; + goto Done; } PyObject *x = NULL; /* running sum of microseconds */ @@ -2903,7 +2903,9 @@ delta_new_impl(PyTypeObject *type, PyObject *days, PyObject *seconds, Py_XDECREF(minutes); Py_XDECREF(hours); Py_XDECREF(weeks); - RELEASE_CURRENT_STATE(st, current_mod); + if (st != NULL) { + RELEASE_CURRENT_STATE(st, current_mod); + } return self; #undef CLEANUP From 747ca92910fbc0aa9d0d46b4b08c915433bda9ba Mon Sep 17 00:00:00 2001 From: Tan Long Date: Sat, 28 Nov 2026 05:40:49 +0800 Subject: [PATCH 4/4] revert timezone Py_INCREF --- Modules/_datetimemodule.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 552d5d1830d302..ae390955e0d709 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -4367,15 +4367,7 @@ static PyObject * timezone_new_impl(PyTypeObject *type, PyObject *offset, PyObject *name) /*[clinic end generated code: output=41a2dda500424187 input=8901a8befbc52561]*/ { - /* vectorcall passes borrowed references; hold strong references before - new_timezone() can execute arbitrary Python code. */ - PyObject *result; - Py_INCREF(offset); - Py_XINCREF(name); - result = new_timezone(offset, name); - Py_DECREF(offset); - Py_XDECREF(name); - return result; + return new_timezone(offset, name); } static void