diff --git a/src/embed_tests/TestPythonEngineProperties.cs b/src/embed_tests/TestPythonEngineProperties.cs index 25da158f2..11db57b3d 100644 --- a/src/embed_tests/TestPythonEngineProperties.cs +++ b/src/embed_tests/TestPythonEngineProperties.cs @@ -105,5 +105,42 @@ 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(); + } + + [Test] + public void SetProgramName() + { + var programName = "FooBar"; + + PythonEngine.ProgramName = programName; + PythonEngine.Initialize(); + + Assert.AreEqual(programName, PythonEngine.ProgramName); + PythonEngine.Shutdown(); + } } } diff --git a/src/runtime/pythonengine.cs b/src/runtime/pythonengine.cs index 4a641e538..b5f609630 100644 --- a/src/runtime/pythonengine.cs +++ b/src/runtime/pythonengine.cs @@ -14,6 +14,9 @@ public class PythonEngine : IDisposable { private static DelegateManager delegateManager; private static bool initialized; + private static IntPtr _pythonHome = IntPtr.Zero; + private static IntPtr _programName = IntPtr.Zero; + private static IntPtr _pythonPath = IntPtr.Zero; public PythonEngine() { @@ -64,7 +67,14 @@ public static string ProgramName return result ?? ""; } - set { Runtime.Py_SetProgramName(value); } + set + { + Marshal.FreeHGlobal(_programName); + _programName = Runtime.IsPython3 + ? UcsMarshaler.GetInstance("").MarshalManagedToNative(value) + : Marshal.StringToHGlobalAnsi(value); + Runtime.Py_SetProgramName(_programName); + } } public static string PythonHome @@ -78,7 +88,14 @@ public static string PythonHome return result ?? ""; } - set { Runtime.Py_SetPythonHome(value); } + set + { + Marshal.FreeHGlobal(_pythonHome); + _pythonHome = Runtime.IsPython3 + ? UcsMarshaler.GetInstance("").MarshalManagedToNative(value) + : Marshal.StringToHGlobalAnsi(value); + Runtime.Py_SetPythonHome(_pythonHome); + } } public static string PythonPath @@ -92,7 +109,14 @@ public static string PythonPath return result ?? ""; } - set { Runtime.Py_SetPath(value); } + set + { + Marshal.FreeHGlobal(_pythonPath); + _pythonPath = Runtime.IsPython3 + ? UcsMarshaler.GetInstance("").MarshalManagedToNative(value) + : Marshal.StringToHGlobalAnsi(value); + Runtime.Py_SetPath(_pythonPath); + } } public static string Version @@ -284,6 +308,13 @@ public static void Shutdown() { if (initialized) { + Marshal.FreeHGlobal(_pythonHome); + _pythonHome = IntPtr.Zero; + Marshal.FreeHGlobal(_programName); + _programName = IntPtr.Zero; + Marshal.FreeHGlobal(_pythonPath); + _pythonPath = IntPtr.Zero; + Runtime.Shutdown(); initialized = false; } diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index 540e2c45c..d47c02490 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -686,43 +686,37 @@ public static extern int Py_Main( internal static extern IntPtr Py_GetProgramName(); [DllImport(PythonDll)] - internal static extern void Py_SetProgramName( - [MarshalAs(UnmanagedType.LPWStr)] string name - ); + internal static extern void Py_SetProgramName(IntPtr name); [DllImport(PythonDll)] 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(); [DllImport(PythonDll)] - internal static extern void Py_SetPath( - [MarshalAs(UnmanagedType.LPWStr)] string home - ); + internal static extern void Py_SetPath(IntPtr home); #elif PYTHON2 [DllImport(PythonDll)] internal static extern IntPtr Py_GetProgramName(); [DllImport(PythonDll)] - internal static extern void Py_SetProgramName(string name); + internal static extern void Py_SetProgramName(IntPtr name); [DllImport(PythonDll)] 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(); [DllImport(PythonDll)] - internal static extern void Py_SetPath(string home); + internal static extern void Py_SetPath(IntPtr home); #endif [DllImport(PythonDll)]