[3.12] gh-71587: Drop local reference cache to _strptime module in _datetime (gh-120431)

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()).

(cherry picked from commit 127c1d2771, AKA gh-120224)
This commit is contained in:
neonene 2024-06-14 03:47:46 +09:00 committed by GitHub
parent eff055340f
commit 7719eefcce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 9 additions and 8 deletions

View file

@ -0,0 +1,2 @@
Fix crash in C version of :meth:`datetime.datetime.strptime` when called again
on the restarted interpreter.

View file

@ -5209,19 +5209,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_ImportModule("_strptime");
if (module == NULL) {
module = PyImport_ImportModule("_strptime");
if (module == NULL)
return NULL;
}
return PyObject_CallMethodObjArgs(module, &_Py_ID(_strptime_datetime),
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. */

View file

@ -422,7 +422,6 @@ Modules/_ctypes/_ctypes.c CreateSwappedType suffix -
Modules/_ctypes/_ctypes.c - _unpickle -
Modules/_ctypes/_ctypes.c PyCArrayType_from_ctype cache -
Modules/_cursesmodule.c - ModDict -
Modules/_datetimemodule.c datetime_strptime module -
Modules/_datetimemodule.c - PyDateTime_TimeZone_UTC -
Modules/_datetimemodule.c - PyDateTime_Epoch -
Modules/_datetimemodule.c - us_per_ms -

Can't render this file because it has a wrong number of fields in line 4.