Skip to content

Fix Re-initialization #343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 31, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
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.
  • Loading branch information
filmor committed Jan 31, 2017
commit af6d37f268a2ab5d9f460d4b09ddfa8a6cd800fc
24 changes: 11 additions & 13 deletions src/runtime/importhook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ internal class ImportHook

#if PYTHON3
static IntPtr py_clr_module;
static IntPtr module_def;
static IntPtr module_def = IntPtr.Zero;

internal static void InitializeModuleDef()
{
if (module_def == IntPtr.Zero)
module_def = ModuleDefOffset.AllocModuleDef("clr");
}
#endif

//===================================================================
Expand Down Expand Up @@ -44,8 +50,8 @@ internal static void Initialize()
root = new CLRModule();

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

// both dicts are borrowed references
Expand All @@ -70,21 +76,13 @@ internal static void Initialize()

internal static void Shutdown()
{
#if PYTHON3
if (0 != Runtime.Py_IsInitialized()) {
#if PYTHON3
Runtime.XDecref(py_clr_module);
Runtime.XDecref(root.pyHandle);
}
ModuleDefOffset.FreeModuleDef(module_def);
#elif PYTHON2
if (0 != Runtime.Py_IsInitialized())
{
Runtime.XDecref(root.pyHandle);
Runtime.XDecref(root.pyHandle);
}
#endif
if (0 != Runtime.Py_IsInitialized())
{
Runtime.XDecref(root.pyHandle);
Runtime.XDecref(py_import);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public static IntPtr AllocModuleDef(string modulename) {
byte[] ascii = Encoding.ASCII.GetBytes(modulename);
int size = name + ascii.Length + 1;
IntPtr ptr = Marshal.AllocHGlobal(size);
for (int i = 0; i <= m_free; i += IntPtr.Size)
for (int i = 0; i < m_free; i += IntPtr.Size)
Marshal.WriteIntPtr(ptr, i, IntPtr.Zero);
Marshal.Copy(ascii, 0, (IntPtr)(ptr + name), ascii.Length);
Marshal.WriteIntPtr(ptr, m_name, (IntPtr)(ptr + name));
Expand Down