@@ -136,6 +136,41 @@ set_current_module(PyInterpreterState *interp, PyObject *mod)
136
136
return PyDict_SetItemString (dict , INTERP_KEY , mod );
137
137
}
138
138
139
+ static void
140
+ clear_current_module (PyInterpreterState * interp , PyObject * expected )
141
+ {
142
+ PyObject * exc = PyErr_GetRaisedException ();
143
+
144
+ PyObject * dict = PyInterpreterState_GetDict (interp );
145
+ if (dict == NULL ) {
146
+ goto error ;
147
+ }
148
+
149
+ if (expected != NULL ) {
150
+ PyObject * current = NULL ;
151
+ if (PyDict_GetItemStringRef (dict , INTERP_KEY , & current ) < 0 ) {
152
+ goto error ;
153
+ }
154
+ if (current != expected ) {
155
+ goto finally ;
156
+ }
157
+ }
158
+
159
+ if (PyDict_DelItemString (dict , INTERP_KEY ) < 0 ) {
160
+ if (!PyErr_ExceptionMatches (PyExc_KeyError )) {
161
+ goto error ;
162
+ }
163
+ }
164
+
165
+ goto finally ;
166
+
167
+ error :
168
+ PyErr_Print ();
169
+
170
+ finally :
171
+ PyErr_SetRaisedException (exc );
172
+ }
173
+
139
174
140
175
/* We require that C int be at least 32 bits, and use int virtually
141
176
* everywhere. In just a few cases we use a temp long, where a Python
@@ -7199,14 +7234,21 @@ module_clear(PyObject *mod)
7199
7234
{
7200
7235
datetime_state * st = get_module_state (mod );
7201
7236
clear_state (st );
7237
+
7238
+ PyInterpreterState * interp = PyInterpreterState_Get ();
7239
+ clear_current_module (interp , mod );
7240
+
7202
7241
return 0 ;
7203
7242
}
7204
7243
7205
7244
static void
7206
7245
module_free (void * mod )
7207
7246
{
7208
- datetime_state * st = get_module_state (mod );
7247
+ datetime_state * st = get_module_state (( PyObject * ) mod );
7209
7248
clear_state (st );
7249
+
7250
+ PyInterpreterState * interp = PyInterpreterState_Get ();
7251
+ clear_current_module (interp , (PyObject * )mod );
7210
7252
}
7211
7253
7212
7254
static PyModuleDef datetimemodule = {
0 commit comments