]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-71587: Drop local reference cache to `_strptime` module in `_datetime`...
authorneonene <53406459+neonene@users.noreply.github.com>
Thu, 13 Jun 2024 18:47:46 +0000 (03:47 +0900)
committerGitHub <noreply@github.com>
Thu, 13 Jun 2024 18:47:46 +0000 (12:47 -0600)
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 127c1d2771749853e287632c086b6054212bf12a, AKA gh-120224)

Misc/NEWS.d/next/Library/2024-06-07-11-23-31.gh-issue-71587.IjFajE.rst [new file with mode: 0644]
Modules/_datetimemodule.c
Tools/c-analyzer/cpython/globals-to-fix.tsv

diff --git a/Misc/NEWS.d/next/Library/2024-06-07-11-23-31.gh-issue-71587.IjFajE.rst b/Misc/NEWS.d/next/Library/2024-06-07-11-23-31.gh-issue-71587.IjFajE.rst
new file mode 100644 (file)
index 0000000..50a6629
--- /dev/null
@@ -0,0 +1,2 @@
+Fix crash in C version of :meth:`datetime.datetime.strptime` when called again
+on the restarted interpreter.
index 8552e42d8be5ee240621f0aae9d5da2db8e03ee5..5a062b9c8c0e2ceb1548931bb357d8be82c031e5 100644 (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 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. */
index b47393e6fdd36a6821c868ba716b73c1ff75408c..ac60ee4e1416a7ccc46a53c7ec67b70a81a35a27 100644 (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       -