https://github.com/python/cpython/commit/7719eefcce1c204a37c455d140d840d34a5c8f19
commit: 7719eefcce1c204a37c455d140d840d34a5c8f19
branch: 3.12
author: neonene <53406459+neon...@users.noreply.github.com>
committer: ericsnowcurrently <ericsnowcurren...@gmail.com>
date: 2024-06-13T12:47:46-06:00
summary:

[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 127c1d2771749853e287632c086b6054212bf12a, AKA 
gh-120224)

files:
A Misc/NEWS.d/next/Library/2024-06-07-11-23-31.gh-issue-71587.IjFajE.rst
M Modules/_datetimemodule.c
M 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
index 00000000000000..50a662977993f5
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-06-07-11-23-31.gh-issue-71587.IjFajE.rst
@@ -0,0 +1,2 @@
+Fix crash in C version of :meth:`datetime.datetime.strptime` when called again
+on the restarted interpreter.
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index 8552e42d8be5ee..5a062b9c8c0e2c 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -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. */
diff --git a/Tools/c-analyzer/cpython/globals-to-fix.tsv 
b/Tools/c-analyzer/cpython/globals-to-fix.tsv
index b47393e6fdd36a..ac60ee4e1416a7 100644
--- a/Tools/c-analyzer/cpython/globals-to-fix.tsv
+++ b/Tools/c-analyzer/cpython/globals-to-fix.tsv
@@ -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       -

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to