Skip to content

Commit a3a8d05

Browse files
Fix a refleak.
1 parent 8be5bc2 commit a3a8d05

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

Python/crossinterp.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,16 @@ ensure_isolated_main(PyThreadState *tstate, struct sync_module *main)
187187
return -1;
188188
}
189189

190+
#ifndef NDEBUG
191+
static int
192+
main_mod_matches(PyObject *expected)
193+
{
194+
PyObject *mod = PyImport_GetModule(&_Py_ID(__main__));
195+
Py_XDECREF(mod);
196+
return mod == expected;
197+
}
198+
#endif
199+
190200
static int
191201
apply_isolated_main(PyThreadState *tstate, struct sync_module *main)
192202
{
@@ -199,7 +209,7 @@ apply_isolated_main(PyThreadState *tstate, struct sync_module *main)
199209
}
200210
assert(main->cached.loaded != NULL);
201211

202-
assert(PyImport_GetModule(&_Py_ID(__main__)) == main->cached.module);
212+
assert(main_mod_matches(main->cached.module));
203213
if (_PyImport_SetModule(&_Py_ID(__main__), main->cached.loaded) < 0) {
204214
sync_module_capture_exc(tstate, main);
205215
return -1;
@@ -214,7 +224,7 @@ restore_main(PyThreadState *tstate, struct sync_module *main)
214224
assert(main->cached.module != NULL);
215225
assert(main->cached.loaded != NULL);
216226
PyObject *exc = _PyErr_GetRaisedException(tstate);
217-
assert(PyImport_GetModule(&_Py_ID(__main__)) == main->cached.loaded);
227+
assert(main_mod_matches(main->cached.loaded));
218228
int res = _PyImport_SetModule(&_Py_ID(__main__), main->cached.module);
219229
assert(res == 0);
220230
if (res < 0) {

0 commit comments

Comments
 (0)