Skip to content

gh-71587: Clear cached strptime module #101783

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
16 changes: 11 additions & 5 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -5189,22 +5189,23 @@ datetime_utcfromtimestamp(PyObject *cls, PyObject *args)
return result;
}

static PyObject *strptime_module = NULL;

/* Return new datetime from _strptime.strptime_datetime(). */
static PyObject *
datetime_strptime(PyObject *cls, PyObject *args)
{
static PyObject *module = NULL;
PyObject *string, *format;

if (!PyArg_ParseTuple(args, "UU:strptime", &string, &format))
return NULL;

if (module == NULL) {
module = PyImport_ImportModule("_strptime");
if (module == NULL)
if (strptime_module == NULL) {
strptime_module = PyImport_ImportModule("_strptime");
if (strptime_module == NULL)
return NULL;
}
return PyObject_CallMethodObjArgs(module, &_Py_ID(_strptime_datetime),
return PyObject_CallMethodObjArgs(strptime_module, &_Py_ID(_strptime_datetime),
cls, string, format, NULL);
}

Expand Down Expand Up @@ -6863,12 +6864,17 @@ _datetime_exec(PyObject *module)
return 0;
}

static void module_free(void *_) {
Py_CLEAR(strptime_module);
}

static struct PyModuleDef datetimemodule = {
PyModuleDef_HEAD_INIT,
.m_name = "_datetime",
.m_doc = "Fast implementation of the datetime type.",
.m_size = -1,
.m_methods = module_methods,
.m_free = module_free,
};

PyMODINIT_FUNC
Expand Down