File tree 3 files changed +41
-5
lines changed
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()
105
105
Assert . AreEqual ( envPythonHome , enginePythonHome ) ;
106
106
PythonEngine . Shutdown ( ) ;
107
107
}
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
+ }
108
133
}
109
134
}
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ public class PythonEngine : IDisposable
14
14
{
15
15
private static DelegateManager delegateManager ;
16
16
private static bool initialized ;
17
+ private static IntPtr _pythonHome = IntPtr . Zero ;
17
18
18
19
public PythonEngine ( )
19
20
{
@@ -78,7 +79,17 @@ public static string PythonHome
78
79
79
80
return result ?? "" ;
80
81
}
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
+ }
82
93
}
83
94
84
95
public static string PythonPath
@@ -284,6 +295,8 @@ public static void Shutdown()
284
295
{
285
296
if ( initialized )
286
297
{
298
+ Marshal . FreeHGlobal ( _pythonHome ) ;
299
+ _pythonHome = IntPtr . Zero ;
287
300
Runtime . Shutdown ( ) ;
288
301
initialized = false ;
289
302
}
Original file line number Diff line number Diff line change @@ -694,9 +694,7 @@ internal static extern void Py_SetProgramName(
694
694
internal static extern IntPtr Py_GetPythonHome( ) ;
695
695
696
696
[ 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 ) ;
700
698
701
699
[ DllImport ( PythonDll ) ]
702
700
internal static extern IntPtr Py_GetPath( ) ;
@@ -716,7 +714,7 @@ internal static extern void Py_SetPath(
716
714
internal static extern IntPtr Py_GetPythonHome( ) ;
717
715
718
716
[ DllImport ( PythonDll ) ]
719
- internal static extern void Py_SetPythonHome ( string home ) ;
717
+ internal static extern void Py_SetPythonHome ( IntPtr home ) ;
720
718
721
719
[ DllImport ( PythonDll ) ]
722
720
internal static extern IntPtr Py_GetPath( ) ;
You can’t perform that action at this time.
0 commit comments