Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2795,12 +2795,22 @@ delta_new_impl(PyTypeObject *type, PyObject *days, PyObject *seconds,
PyObject *minutes, PyObject *hours, PyObject *weeks)
/*[clinic end generated code: output=61d7e02a92a97700 input=8aebc4cdef360d1f]*/
{
/* vectorcall passes borrowed references; hold strong references before

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, wouldn't this possibly be an issue in other parts of the code that was touched with the new AC change? can we make it easier to use in AC with a vectorcall that does an incref bump before calling it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been reviewing the AC Vectorcall conversions for this issue, hence why found it here. The vast majority of __init__ and __new__ where vectorcall improves speed substantially have none of these hazards as if the call protocol is most their time they're just doing things like Py_SETREF. This is the "Thin Ice" case in the docs: https://docs.python.org/3/extending/extending.html#thin-ice

From the base issue here:

tp_call keeps strong references in the argument tuple. In vectorcall the arguments are borrowed references. Make sure to hold a strong reference (Py_INCREF) before calling arbitrary Python code.

In pure-python this bug is not easy to hit at least. There is an issue with functools.partial since 3.15 but that is one specific function implementation to fix.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am trying to find a low overhead way to have C Vectorcall keep the objects alive like the interpreter stack does. Unfortunately all so far have quite a bit of overhead. Just doing Py_INCREF will likely cause substantial slowdown in free-threaded builds for references shared across threads (ex. make a bunch of work objects in a main thread than other threads process).

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 *self = NULL;

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 */
Expand Down Expand Up @@ -2886,7 +2896,16 @@ delta_new_impl(PyTypeObject *type, PyObject *days, PyObject *seconds,
Py_DECREF(x);

Done:
RELEASE_CURRENT_STATE(st, current_mod);
Py_XDECREF(days);
Py_XDECREF(seconds);
Py_XDECREF(microseconds);
Py_XDECREF(milliseconds);
Py_XDECREF(minutes);
Py_XDECREF(hours);
Py_XDECREF(weeks);
if (st != NULL) {
RELEASE_CURRENT_STATE(st, current_mod);
}
return self;

#undef CLEANUP
Expand Down
Loading