Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 37 additions & 0 deletions src/embed_tests/TestPythonEngineProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
37 changes: 34 additions & 3 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
18 changes: 6 additions & 12 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down