Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
Add Shutdown call, disable Py_Finalize for now
  • Loading branch information
filmor committed Apr 14, 2020
commit 5c3109db52eab9e4b2fd0f915db78bfee21caf98
26 changes: 22 additions & 4 deletions Python.Loader/Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public Assembly LoadAssembly()

static class Internal
{
static Type PythonEngine = null;

public static int Initialize(IntPtr data, int size)
{
try
Expand All @@ -76,10 +78,26 @@ public static int Initialize(IntPtr data, int size)
loader.Remap(pythonDll);
var assembly = loader.LoadAssembly();

var eng = assembly.GetType("Python.Runtime.PythonEngine");
var method = eng.GetMethod("InternalInitialize");
var res = method.Invoke(null, new object[] { data, size });
return (int)res;
PythonEngine = assembly.GetType("Python.Runtime.PythonEngine");
var method = PythonEngine.GetMethod("InternalInitialize");
return (int)method.Invoke(null, new object[] { data, size });
}
catch (Exception exc)
{
Console.WriteLine($"{exc}\n{exc.StackTrace}");
return -1;
}
}

public static int Shutdown(IntPtr data, int size)
{
if (PythonEngine == null)
return -2;

try
{
var method = PythonEngine.GetMethod("InternalShutdown");
return (int)method.Invoke(null, new object[] { data, size });
}
catch (Exception exc)
{
Expand Down
4 changes: 2 additions & 2 deletions Python.Runtime/moduleobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,9 @@ public static string[] ListAssemblies(bool verbose)
}

[ModuleFunction]
public static void _AtExit()
public static int _AtExit()
{
Runtime.AtExit();
return Runtime.AtExit();
}
}
}
28 changes: 28 additions & 0 deletions Python.Runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,34 @@ public static int InternalInitialize(IntPtr data, int size)
}
}

public static int InternalShutdown(IntPtr data, int size)
{
IntPtr gilState = IntPtr.Zero;
try
{
gilState = Runtime.PyGILState_Ensure();
Shutdown();

return 0;
}
catch (PythonException exc)
{
exc.Restore();
Console.WriteLine($"{exc.Message}\n{exc.Format()}");
return -1;
}
catch (Exception exc)
{
Console.WriteLine($"{exc}\n{exc.StackTrace}");
return -1;
}
finally
{
if (gilState != IntPtr.Zero)
Runtime.PyGILState_Release(gilState);
}
}

/// <summary>
/// Shutdown Method
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions Python.Runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,16 +354,16 @@ internal static void Shutdown()
// TOOD: PyCLRMetaType's release operation still in #958
PyCLRMetaType = IntPtr.Zero;
ResetPyMembers();
Py_Finalize();
}

// called *without* the GIL acquired by clr._AtExit
internal static void AtExit()
internal static int AtExit()
{
lock (IsFinalizingLock)
{
IsFinalizing = true;
}
return 0;
}

private static void SetPyMember(ref IntPtr obj, IntPtr value, Action onRelease, [CallerLineNumber]int lineNumber = 0)
Expand Down