File tree Expand file tree Collapse file tree 3 files changed +41
-5
lines changed
Expand file tree Collapse file tree 3 files changed +41
-5
lines changed Original file line number Diff line number Diff line change @@ -105,5 +105,30 @@ 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+ }
108133 }
109134}
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ public class PythonEngine : IDisposable
1414 {
1515 private static DelegateManager delegateManager ;
1616 private static bool initialized ;
17+ private static IntPtr _pythonHome = IntPtr . Zero ;
1718
1819 public PythonEngine ( )
1920 {
@@ -78,7 +79,17 @@ public static string PythonHome
7879
7980 return result ?? "" ;
8081 }
81- set { Runtime . Py_SetPythonHome ( value ) ; }
82+ set
83+ {
84+ if ( _pythonHome != IntPtr . Zero )
85+ {
86+ Marshal . FreeHGlobal ( _pythonHome ) ;
87+ }
88+ _pythonHome = Runtime . IsPython3
89+ ? UcsMarshaler . GetInstance ( "" ) . MarshalManagedToNative ( value )
90+ : Marshal . StringToHGlobalAnsi ( value ) ;
91+ Runtime . Py_SetPythonHome ( _pythonHome ) ;
92+ }
8293 }
8394
8495 public static string PythonPath
@@ -284,6 +295,8 @@ public static void Shutdown()
284295 {
285296 if ( initialized )
286297 {
298+ Marshal . FreeHGlobal ( _pythonHome ) ;
299+ _pythonHome = IntPtr . Zero ;
287300 Runtime . Shutdown ( ) ;
288301 initialized = false ;
289302 }
Original file line number Diff line number Diff line change @@ -694,9 +694,7 @@ internal static extern void Py_SetProgramName(
694694 internal static extern IntPtr Py_GetPythonHome( ) ;
695695
696696 [ DllImport ( PythonDll ) ]
697- internal static extern void Py_SetPythonHome (
698- [ MarshalAs ( UnmanagedType . LPWStr ) ] string home
699- ) ;
697+ internal static extern void Py_SetPythonHome ( IntPtr home ) ;
700698
701699 [ DllImport ( PythonDll ) ]
702700 internal static extern IntPtr Py_GetPath( ) ;
@@ -716,7 +714,7 @@ internal static extern void Py_SetPath(
716714 internal static extern IntPtr Py_GetPythonHome( ) ;
717715
718716 [ DllImport ( PythonDll ) ]
719- internal static extern void Py_SetPythonHome ( string home ) ;
717+ internal static extern void Py_SetPythonHome ( IntPtr home ) ;
720718
721719 [ DllImport ( PythonDll ) ]
722720 internal static extern IntPtr Py_GetPath( ) ;
You can’t perform that action at this time.
0 commit comments