@@ -1967,6 +1967,46 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
1967
1967
assert (def != NULL );
1968
1968
}
1969
1969
1970
+ /* Do anything else that should be done
1971
+ * while still using the main interpreter. */
1972
+ if (res .kind == _Py_ext_module_kind_SINGLEPHASE ) {
1973
+ /* Remember the filename as the __file__ attribute */
1974
+ if (info -> filename != NULL ) {
1975
+ if (PyModule_AddObjectRef (mod , "__file__" , info -> filename ) < 0 ) {
1976
+ PyErr_Clear (); /* Not important enough to report */
1977
+ }
1978
+ }
1979
+
1980
+ /* Update global import state. */
1981
+ assert (def -> m_base .m_index != 0 );
1982
+ struct singlephase_global_update singlephase = {
1983
+ // XXX Modules that share a def should each get their own index,
1984
+ // whereas currently they share (which means the per-interpreter
1985
+ // cache is less reliable than it should be).
1986
+ .m_index = def -> m_base .m_index ,
1987
+ .origin = info -> origin ,
1988
+ };
1989
+ // gh-88216: Extensions and def->m_base.m_copy can be updated
1990
+ // when the extension module doesn't support sub-interpreters.
1991
+ if (def -> m_size == -1 ) {
1992
+ /* We will reload from m_copy. */
1993
+ assert (def -> m_base .m_init == NULL );
1994
+ singlephase .m_dict = PyModule_GetDict (mod );
1995
+ assert (singlephase .m_dict != NULL );
1996
+ }
1997
+ else {
1998
+ /* We will reload via the init function. */
1999
+ assert (def -> m_size >= 0 );
2000
+ assert (def -> m_base .m_copy == NULL );
2001
+ singlephase .m_init = p0 ;
2002
+ }
2003
+ cached = update_global_state_for_extension (
2004
+ tstate , info -> path , info -> name , def , & singlephase );
2005
+ if (cached == NULL ) {
2006
+ goto error ;
2007
+ }
2008
+ }
2009
+
1970
2010
/* Switch back to the subinterpreter. */
1971
2011
if (main_tstate != tstate ) {
1972
2012
/* Any module we got from the init function will have to be
@@ -2003,63 +2043,31 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
2003
2043
if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed (name_buf ) < 0 ) {
2004
2044
goto error ;
2005
2045
}
2006
- assert (mod != NULL );
2007
- assert (PyModule_Check (mod ));
2008
-
2009
- /* Remember the filename as the __file__ attribute */
2010
- if (info -> filename != NULL ) {
2011
- if (PyModule_AddObjectRef (mod , "__file__" , info -> filename ) < 0 ) {
2012
- PyErr_Clear (); /* Not important enough to report */
2013
- }
2014
- }
2015
-
2016
- /* Update global import state. */
2017
- assert (def -> m_base .m_index != 0 );
2018
- struct singlephase_global_update singlephase = {
2019
- // XXX Modules that share a def should each get their own index,
2020
- // whereas currently they share (which means the per-interpreter
2021
- // cache is less reliable than it should be).
2022
- .m_index = def -> m_base .m_index ,
2023
- .origin = info -> origin ,
2024
- };
2025
- // gh-88216: Extensions and def->m_base.m_copy can be updated
2026
- // when the extension module doesn't support sub-interpreters.
2027
- if (def -> m_size == -1 ) {
2028
- /* We will reload from m_copy. */
2029
- assert (def -> m_base .m_init == NULL );
2030
- singlephase .m_dict = PyModule_GetDict (mod );
2031
- assert (singlephase .m_dict != NULL );
2032
- }
2033
- else {
2034
- /* We will reload via the init function. */
2035
- assert (def -> m_size >= 0 );
2036
- assert (def -> m_base .m_copy == NULL );
2037
- singlephase .m_init = p0 ;
2038
- }
2039
- cached = update_global_state_for_extension (
2040
- tstate , info -> path , info -> name , def , & singlephase );
2041
- if (cached == NULL ) {
2042
- goto error ;
2043
- }
2046
+ assert (!PyErr_Occurred ());
2044
2047
2045
2048
if (main_tstate != tstate ) {
2046
2049
/* We switched to the main interpreter to run the init
2047
2050
* function, so now we will "reload" the module from the
2048
2051
* cached data using the original subinterpreter. */
2049
- Py_CLEAR (mod );
2052
+ assert (mod == NULL );
2050
2053
mod = reload_singlephase_extension (tstate , cached , info );
2051
2054
if (mod == NULL ) {
2052
2055
goto error ;
2053
2056
}
2057
+ assert (!PyErr_Occurred ());
2058
+ assert (PyModule_Check (mod ));
2054
2059
}
2055
- assert (!PyErr_Occurred ());
2056
-
2057
- /* Update per-interpreter import state. */
2058
- PyObject * modules = get_modules_dict (tstate , true);
2059
- if (finish_singlephase_extension (
2060
- tstate , mod , cached , info -> name , modules ) < 0 )
2061
- {
2062
- goto error ;
2060
+ else {
2061
+ assert (mod != NULL );
2062
+ assert (PyModule_Check (mod ));
2063
+
2064
+ /* Update per-interpreter import state. */
2065
+ PyObject * modules = get_modules_dict (tstate , true);
2066
+ if (finish_singlephase_extension (
2067
+ tstate , mod , cached , info -> name , modules ) < 0 )
2068
+ {
2069
+ goto error ;
2070
+ }
2063
2071
}
2064
2072
}
2065
2073
0 commit comments