@@ -1532,6 +1532,35 @@ switch_to_main_interpreter(PyThreadState *tstate)
1532
1532
return main_tstate ;
1533
1533
}
1534
1534
1535
+ static void
1536
+ switch_back_from_main_interpreter (PyThreadState * tstate ,
1537
+ PyThreadState * main_tstate ,
1538
+ PyObject * tempobj )
1539
+ {
1540
+ assert (main_tstate == PyThreadState_GET ());
1541
+ assert (_Py_IsMainInterpreter (main_tstate -> interp ));
1542
+ assert (tstate -> interp != main_tstate -> interp );
1543
+
1544
+ /* Handle any exceptions, which we cannot propagate directly
1545
+ * to the subinterpreter. */
1546
+ if (PyErr_Occurred ()) {
1547
+ if (PyErr_ExceptionMatches (PyExc_MemoryError )) {
1548
+ /* We trust it will be caught again soon. */
1549
+ PyErr_Clear ();
1550
+ }
1551
+ else {
1552
+ /* Printing the exception should be sufficient. */
1553
+ PyErr_PrintEx (0 );
1554
+ }
1555
+ }
1556
+
1557
+ Py_XDECREF (tempobj );
1558
+
1559
+ PyThreadState_Clear (main_tstate );
1560
+ (void )PyThreadState_Swap (tstate );
1561
+ PyThreadState_Delete (main_tstate );
1562
+ }
1563
+
1535
1564
static PyObject *
1536
1565
get_core_module_dict (PyInterpreterState * interp ,
1537
1566
PyObject * name , PyObject * path )
@@ -2027,27 +2056,10 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
2027
2056
/* Switch back to the subinterpreter. */
2028
2057
if (switched ) {
2029
2058
assert (main_tstate != tstate );
2030
-
2031
- /* Handle any exceptions, which we cannot propagate directly
2032
- * to the subinterpreter. */
2033
- if (PyErr_Occurred ()) {
2034
- if (PyErr_ExceptionMatches (PyExc_MemoryError )) {
2035
- /* We trust it will be caught again soon. */
2036
- PyErr_Clear ();
2037
- }
2038
- else {
2039
- /* Printing the exception should be sufficient. */
2040
- PyErr_PrintEx (0 );
2041
- }
2042
- }
2043
-
2059
+ switch_back_from_main_interpreter (tstate , main_tstate , mod );
2044
2060
/* Any module we got from the init function will have to be
2045
2061
* reloaded in the subinterpreter. */
2046
- Py_CLEAR (mod );
2047
-
2048
- PyThreadState_Clear (main_tstate );
2049
- (void )PyThreadState_Swap (tstate );
2050
- PyThreadState_Delete (main_tstate );
2062
+ mod = NULL ;
2051
2063
}
2052
2064
2053
2065
/*****************************************************************/
@@ -2141,9 +2153,21 @@ clear_singlephase_extension(PyInterpreterState *interp,
2141
2153
}
2142
2154
}
2143
2155
2156
+ /* We must use the main interpreter to clean up the cache.
2157
+ * See the note in import_run_extension(). */
2158
+ PyThreadState * tstate = PyThreadState_GET ();
2159
+ PyThreadState * main_tstate = switch_to_main_interpreter (tstate );
2160
+ if (main_tstate == NULL ) {
2161
+ return -1 ;
2162
+ }
2163
+
2144
2164
/* Clear the cached module def. */
2145
2165
_extensions_cache_delete (path , name );
2146
2166
2167
+ if (main_tstate != tstate ) {
2168
+ switch_back_from_main_interpreter (tstate , main_tstate , NULL );
2169
+ }
2170
+
2147
2171
return 0 ;
2148
2172
}
2149
2173
0 commit comments