Skip to content

Commit 7e26298

Browse files
committed
Fix the shutdown issue by keeping PyMethodDef.
Keeping the PyMethodDef around like the documentation (https://docs.python.org/3.6/c-api/module.html#c.PyModuleDef) suggests fixes issue #262.
1 parent 05482e5 commit 7e26298

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

src/runtime/importhook.cs

+19-13
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ internal class ImportHook
1616

1717
#if PYTHON3
1818
static IntPtr py_clr_module;
19-
static IntPtr module_def;
19+
static IntPtr module_def = IntPtr.Zero;
20+
21+
internal static void InitializeModuleDef()
22+
{
23+
if (module_def == IntPtr.Zero)
24+
module_def = ModuleDefOffset.AllocModuleDef("clr");
25+
}
2026
#endif
2127

2228
//===================================================================
@@ -44,8 +50,8 @@ internal static void Initialize()
4450
root = new CLRModule();
4551

4652
#if PYTHON3
47-
// create a python module with the same methods as the clr module-like object
48-
module_def = ModuleDefOffset.AllocModuleDef("clr");
53+
// create a python module with the same methods as the clr module-like object
54+
InitializeModuleDef();
4955
py_clr_module = Runtime.PyModule_Create2(module_def, 3);
5056

5157
// both dicts are borrowed references
@@ -70,25 +76,25 @@ internal static void Initialize()
7076

7177
internal static void Shutdown()
7278
{
73-
#if PYTHON3
7479
if (0 != Runtime.Py_IsInitialized()) {
80+
#if PYTHON3
7581
Runtime.XDecref(py_clr_module);
76-
Runtime.XDecref(root.pyHandle);
77-
}
78-
ModuleDefOffset.FreeModuleDef(module_def);
7982
#elif PYTHON2
80-
if (0 != Runtime.Py_IsInitialized())
81-
{
82-
Runtime.XDecref(root.pyHandle);
8383
Runtime.XDecref(root.pyHandle);
84-
}
8584
#endif
86-
if (0 != Runtime.Py_IsInitialized())
87-
{
85+
Runtime.XDecref(root.pyHandle);
8886
Runtime.XDecref(py_import);
8987
}
9088
}
9189

90+
internal static void Cleanup()
91+
{
92+
#if PYTHON3
93+
ModuleDefOffset.FreeModuleDef(module_def);
94+
module_def = IntPtr.Zero;
95+
#endif
96+
}
97+
9298
//===================================================================
9399
// Return the clr python module (new reference)
94100
//===================================================================

src/runtime/interop.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public static IntPtr AllocModuleDef(string modulename) {
227227
byte[] ascii = Encoding.ASCII.GetBytes(modulename);
228228
int size = name + ascii.Length + 1;
229229
IntPtr ptr = Marshal.AllocHGlobal(size);
230-
for (int i = 0; i <= m_free; i += IntPtr.Size)
230+
for (int i = 0; i < m_free; i += IntPtr.Size)
231231
Marshal.WriteIntPtr(ptr, i, IntPtr.Zero);
232232
Marshal.Copy(ascii, 0, (IntPtr)(ptr + name), ascii.Length);
233233
Marshal.WriteIntPtr(ptr, m_name, (IntPtr)(ptr + name));

src/runtime/pythonengine.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.IO;
33
using System.Threading;
44
using System.Reflection;

0 commit comments

Comments
 (0)