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

Skip to content

Commit 365cd6e

Browse files
authored
Merge pull request #415 from vmuriart/fix_PYTHONHOME
Fix set pythonhome
2 parents 0a47802 + 321aa28 commit 365cd6e

File tree

3 files changed

+77
-15
lines changed

3 files changed

+77
-15
lines changed

src/embed_tests/TestPythonEngineProperties.cs

+37
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,42 @@ public static void GetPythonHomeDefault()
105105
Assert.AreEqual(envPythonHome, enginePythonHome);
106106
PythonEngine.Shutdown();
107107
}
108+
109+
[Test]
110+
public void SetPythonHome()
111+
{
112+
var pythonHome = "/dummypath/";
113+
114+
PythonEngine.PythonHome = pythonHome;
115+
PythonEngine.Initialize();
116+
117+
Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
118+
PythonEngine.Shutdown();
119+
}
120+
121+
[Test]
122+
public void SetPythonHomeTwice()
123+
{
124+
var pythonHome = "/dummypath/";
125+
126+
PythonEngine.PythonHome = "/dummypath2/";
127+
PythonEngine.PythonHome = pythonHome;
128+
PythonEngine.Initialize();
129+
130+
Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
131+
PythonEngine.Shutdown();
132+
}
133+
134+
[Test]
135+
public void SetProgramName()
136+
{
137+
var programName = "FooBar";
138+
139+
PythonEngine.ProgramName = programName;
140+
PythonEngine.Initialize();
141+
142+
Assert.AreEqual(programName, PythonEngine.ProgramName);
143+
PythonEngine.Shutdown();
144+
}
108145
}
109146
}

src/runtime/pythonengine.cs

+34-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public class PythonEngine : IDisposable
1414
{
1515
private static DelegateManager delegateManager;
1616
private static bool initialized;
17+
private static IntPtr _pythonHome = IntPtr.Zero;
18+
private static IntPtr _programName = IntPtr.Zero;
19+
private static IntPtr _pythonPath = IntPtr.Zero;
1720

1821
public PythonEngine()
1922
{
@@ -64,7 +67,14 @@ public static string ProgramName
6467

6568
return result ?? "";
6669
}
67-
set { Runtime.Py_SetProgramName(value); }
70+
set
71+
{
72+
Marshal.FreeHGlobal(_programName);
73+
_programName = Runtime.IsPython3
74+
? UcsMarshaler.GetInstance("").MarshalManagedToNative(value)
75+
: Marshal.StringToHGlobalAnsi(value);
76+
Runtime.Py_SetProgramName(_programName);
77+
}
6878
}
6979

7080
public static string PythonHome
@@ -78,7 +88,14 @@ public static string PythonHome
7888

7989
return result ?? "";
8090
}
81-
set { Runtime.Py_SetPythonHome(value); }
91+
set
92+
{
93+
Marshal.FreeHGlobal(_pythonHome);
94+
_pythonHome = Runtime.IsPython3
95+
? UcsMarshaler.GetInstance("").MarshalManagedToNative(value)
96+
: Marshal.StringToHGlobalAnsi(value);
97+
Runtime.Py_SetPythonHome(_pythonHome);
98+
}
8299
}
83100

84101
public static string PythonPath
@@ -92,7 +109,14 @@ public static string PythonPath
92109

93110
return result ?? "";
94111
}
95-
set { Runtime.Py_SetPath(value); }
112+
set
113+
{
114+
Marshal.FreeHGlobal(_pythonPath);
115+
_pythonPath = Runtime.IsPython3
116+
? UcsMarshaler.GetInstance("").MarshalManagedToNative(value)
117+
: Marshal.StringToHGlobalAnsi(value);
118+
Runtime.Py_SetPath(_pythonPath);
119+
}
96120
}
97121

98122
public static string Version
@@ -284,6 +308,13 @@ public static void Shutdown()
284308
{
285309
if (initialized)
286310
{
311+
Marshal.FreeHGlobal(_pythonHome);
312+
_pythonHome = IntPtr.Zero;
313+
Marshal.FreeHGlobal(_programName);
314+
_programName = IntPtr.Zero;
315+
Marshal.FreeHGlobal(_pythonPath);
316+
_pythonPath = IntPtr.Zero;
317+
287318
Runtime.Shutdown();
288319
initialized = false;
289320
}

src/runtime/runtime.cs

+6-12
Original file line numberDiff line numberDiff line change
@@ -686,43 +686,37 @@ public static extern int Py_Main(
686686
internal static extern IntPtr Py_GetProgramName();
687687

688688
[DllImport(PythonDll)]
689-
internal static extern void Py_SetProgramName(
690-
[MarshalAs(UnmanagedType.LPWStr)] string name
691-
);
689+
internal static extern void Py_SetProgramName(IntPtr name);
692690

693691
[DllImport(PythonDll)]
694692
internal static extern IntPtr Py_GetPythonHome();
695693

696694
[DllImport(PythonDll)]
697-
internal static extern void Py_SetPythonHome(
698-
[MarshalAs(UnmanagedType.LPWStr)] string home
699-
);
695+
internal static extern void Py_SetPythonHome(IntPtr home);
700696

701697
[DllImport(PythonDll)]
702698
internal static extern IntPtr Py_GetPath();
703699

704700
[DllImport(PythonDll)]
705-
internal static extern void Py_SetPath(
706-
[MarshalAs(UnmanagedType.LPWStr)] string home
707-
);
701+
internal static extern void Py_SetPath(IntPtr home);
708702
#elif PYTHON2
709703
[DllImport(PythonDll)]
710704
internal static extern IntPtr Py_GetProgramName();
711705

712706
[DllImport(PythonDll)]
713-
internal static extern void Py_SetProgramName(string name);
707+
internal static extern void Py_SetProgramName(IntPtr name);
714708

715709
[DllImport(PythonDll)]
716710
internal static extern IntPtr Py_GetPythonHome();
717711

718712
[DllImport(PythonDll)]
719-
internal static extern void Py_SetPythonHome(string home);
713+
internal static extern void Py_SetPythonHome(IntPtr home);
720714

721715
[DllImport(PythonDll)]
722716
internal static extern IntPtr Py_GetPath();
723717

724718
[DllImport(PythonDll)]
725-
internal static extern void Py_SetPath(string home);
719+
internal static extern void Py_SetPath(IntPtr home);
726720
#endif
727721

728722
[DllImport(PythonDll)]

0 commit comments

Comments
 (0)