Skip to content
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
Next Next commit
Fix PythonEngine PYTHONHOME setter
Keep memory reference & fix PY3 marshal
  • Loading branch information
vmuriart committed Mar 3, 2017
commit 6668ebf5fef1a28246d5c691bf1d05d86b25fd32
25 changes: 25 additions & 0 deletions src/embed_tests/TestPythonEngineProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,30 @@ public static void GetPythonHomeDefault()
Assert.AreEqual(envPythonHome, enginePythonHome);
PythonEngine.Shutdown();
}

[Test]
public void SetPythonHome()
{
var pythonHome = "/dummypath/";

PythonEngine.PythonHome = pythonHome;
PythonEngine.Initialize();

Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
PythonEngine.Shutdown();
}

[Test]
public void SetPythonHomeTwice()
{
var pythonHome = "/dummypath/";

PythonEngine.PythonHome = "/dummypath2/";
PythonEngine.PythonHome = pythonHome;
PythonEngine.Initialize();

Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
PythonEngine.Shutdown();
}
}
}
15 changes: 14 additions & 1 deletion src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class PythonEngine : IDisposable
{
private static DelegateManager delegateManager;
private static bool initialized;
private static IntPtr _pythonHome = IntPtr.Zero;

public PythonEngine()
{
Expand Down Expand Up @@ -78,7 +79,17 @@ public static string PythonHome

return result ?? "";
}
set { Runtime.Py_SetPythonHome(value); }
set
{
if (_pythonHome != IntPtr.Zero)
{
Marshal.FreeHGlobal(_pythonHome);
}
_pythonHome = Runtime.IsPython3
? UcsMarshaler.GetInstance("").MarshalManagedToNative(value)
: Marshal.StringToHGlobalAnsi(value);
Runtime.Py_SetPythonHome(_pythonHome);
}
}

public static string PythonPath
Expand Down Expand Up @@ -284,6 +295,8 @@ public static void Shutdown()
{
if (initialized)
{
Marshal.FreeHGlobal(_pythonHome);
_pythonHome = IntPtr.Zero;
Runtime.Shutdown();
initialized = false;
}
Expand Down
6 changes: 2 additions & 4 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -694,9 +694,7 @@ internal static extern void Py_SetProgramName(
internal static extern IntPtr Py_GetPythonHome();

[DllImport(PythonDll)]
internal static extern void Py_SetPythonHome(
[MarshalAs(UnmanagedType.LPWStr)] string home
);
internal static extern void Py_SetPythonHome(IntPtr home);

[DllImport(PythonDll)]
internal static extern IntPtr Py_GetPath();
Expand All @@ -716,7 +714,7 @@ internal static extern void Py_SetPath(
internal static extern IntPtr Py_GetPythonHome();

[DllImport(PythonDll)]
internal static extern void Py_SetPythonHome(string home);
internal static extern void Py_SetPythonHome(IntPtr home);

[DllImport(PythonDll)]
internal static extern IntPtr Py_GetPath();
Expand Down