gh-71587: Drop local reference cache to _strptime module in _datetime (gh-120224)

The _strptime module object was cached in a static local variable (in the datetime.strptime() implementation).  That's a problem when it crosses isolation boundaries, such as reinitializing the runtme or between interpreters.  This change fixes the problem by dropping the static variable, instead always relying on the normal sys.modules cache (via PyImport_Import()).
This commit is contained in:
neonene 2024-06-13 01:46:39 +09:00 committed by GitHub
parent fabcf6bc8f
commit 127c1d2771
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 24 additions and 8 deletions

View file

@ -5514,19 +5514,19 @@ datetime_utcfromtimestamp(PyObject *cls, PyObject *args)
static PyObject *
datetime_strptime(PyObject *cls, PyObject *args)
{
static PyObject *module = NULL;
PyObject *string, *format;
PyObject *string, *format, *result;
if (!PyArg_ParseTuple(args, "UU:strptime", &string, &format))
return NULL;
PyObject *module = PyImport_Import(&_Py_ID(_strptime));
if (module == NULL) {
module = PyImport_ImportModule("_strptime");
if (module == NULL)
return NULL;
return NULL;
}
return PyObject_CallMethodObjArgs(module, &_Py_ID(_strptime_datetime),
cls, string, format, NULL);
result = PyObject_CallMethodObjArgs(module, &_Py_ID(_strptime_datetime),
cls, string, format, NULL);
Py_DECREF(module);
return result;
}
/* Return new datetime from date/datetime and time arguments. */