Skip to content

Commit cc7fe9a

Browse files
committed
When shutting down don't delete any Python objects after Py_Finalize has
been called.
1 parent d64273e commit cc7fe9a

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

src/runtime/exceptions.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,19 @@ internal static void Initialize() {
168168
//===================================================================
169169

170170
internal static void Shutdown() {
171-
Type type = typeof(Exceptions);
172-
foreach (FieldInfo fi in type.GetFields(BindingFlags.Public |
173-
BindingFlags.Static)) {
174-
IntPtr op = (IntPtr)fi.GetValue(type);
175-
if (op != IntPtr.Zero) {
176-
Runtime.Decref(op);
171+
if (0 != Runtime.Py_IsInitialized()) {
172+
Type type = typeof(Exceptions);
173+
foreach (FieldInfo fi in type.GetFields(BindingFlags.Public |
174+
BindingFlags.Static)) {
175+
IntPtr op = (IntPtr)fi.GetValue(type);
176+
if (op != IntPtr.Zero) {
177+
Runtime.Decref(op);
178+
}
177179
}
180+
Runtime.Decref(exceptions_module);
181+
Runtime.PyObject_HasAttrString(warnings_module, "xx");
182+
Runtime.Decref(warnings_module);
178183
}
179-
Runtime.Decref(exceptions_module);
180-
Runtime.Decref(warnings_module);
181184
}
182185

183186
/// <summary>

src/runtime/importhook.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,20 @@ internal static void Initialize() {
7979

8080
internal static void Shutdown() {
8181
#if (PYTHON32 || PYTHON33 || PYTHON34)
82-
Runtime.Decref(py_clr_module);
83-
Runtime.Decref(root.pyHandle);
82+
if (0 != Runtime.Py_IsInitialized()) {
83+
Runtime.Decref(py_clr_module);
84+
Runtime.Decref(root.pyHandle);
85+
}
8486
ModuleDefOffset.FreeModuleDef(module_def);
8587
#else
86-
Runtime.Decref(root.pyHandle);
87-
Runtime.Decref(root.pyHandle);
88+
if (0 != Runtime.Py_IsInitialized()) {
89+
Runtime.Decref(root.pyHandle);
90+
Runtime.Decref(root.pyHandle);
91+
}
8892
#endif
89-
Runtime.Decref(py_import);
93+
if (0 != Runtime.Py_IsInitialized()) {
94+
Runtime.Decref(py_import);
95+
}
9096
}
9197

9298
//===================================================================

0 commit comments

Comments
 (0)