@@ -50,7 +50,8 @@ typedef struct {
5050    /* The interned Unix epoch datetime instance */ 
5151    PyObject  * epoch ;
5252
53-     /* Interpreter's dict holds the module */ 
53+     /* Reference to the interpreter's dict where the current module will be 
54+      * reserved even after the referent dict becomes NULL at shutdown. */ 
5455    PyObject  * interp_dict ;
5556} datetime_state ;
5657
@@ -185,7 +186,8 @@ _get_current_state(PyObject **p_mod)
185186    Py_DECREF(MOD_VAR)
186187
187188static  int 
188- set_current_module (datetime_state  * st , PyObject  * mod )
189+ set_current_module (datetime_state  * st ,
190+                    PyInterpreterState  * interp , PyObject  * mod )
189191{
190192    assert (mod  !=  NULL );
191193    PyObject  * dict  =  st -> interp_dict ;
@@ -199,13 +201,13 @@ set_current_module(datetime_state *st, PyObject *mod)
199201static  void 
200202clear_current_module (datetime_state  * st , PyObject  * expected )
201203{
202-     PyObject  * exc  =  PyErr_GetRaisedException ();
203- 
204204    PyObject  * dict  =  st -> interp_dict ;
205205    if  (dict  ==  NULL ) {
206206        return ;  /* Already cleared */ 
207207    }
208208
209+     PyObject  * exc  =  PyErr_GetRaisedException ();
210+ 
209211    if  (expected  !=  NULL ) {
210212        PyObject  * current ;
211213        if  (PyDict_GetItemRef (dict , INTERP_KEY , & current ) <  0 ) {
@@ -7284,10 +7286,8 @@ init_state(datetime_state *st,
72847286static  int 
72857287traverse_state (datetime_state  * st , visitproc  visit , void  * arg )
72867288{
7287-     /* heap types */ 
72887289    Py_VISIT (st -> isocalendar_date_type );
72897290    Py_VISIT (st -> interp_dict );
7290- 
72917291    return  0 ;
72927292}
72937293
@@ -7477,7 +7477,7 @@ _datetime_exec(PyObject *module)
74777477    static_assert (DI100Y  ==  25  *  DI4Y  -  1 , "DI100Y" );
74787478    assert (DI100Y  ==  days_before_year (100 + 1 ));
74797479
7480-     if  (set_current_module (st , module ) <  0 ) {
7480+     if  (set_current_module (st , interp ,  module ) <  0 ) {
74817481        goto error ;
74827482    }
74837483
0 commit comments