Skip to content

Commit 592d9ab

Browse files
committed
Fix set PythonPath, set ProgramName
Note on PythonPath. Its actually mapping to `Py_SetPath` which is very different from PYTHONPATH env var. There is no test on it because it should be set to real paths with libraries. Otherwise it crashes. 2nd Note. `Py_SetPath` doesn't exist on PY27.
1 parent f55532c commit 592d9ab

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

src/embed_tests/TestPythonEngineProperties.cs

+12
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,17 @@ public void SetPythonHomeTwice()
130130
Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
131131
PythonEngine.Shutdown();
132132
}
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+
}
133145
}
134146
}

src/runtime/pythonengine.cs

+29-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class PythonEngine : IDisposable
1515
private static DelegateManager delegateManager;
1616
private static bool initialized;
1717
private static IntPtr _pythonHome = IntPtr.Zero;
18+
private static IntPtr _programName = IntPtr.Zero;
19+
private static IntPtr _pythonPath = IntPtr.Zero;
1820

1921
public PythonEngine()
2022
{
@@ -65,7 +67,17 @@ public static string ProgramName
6567

6668
return result ?? "";
6769
}
68-
set { Runtime.Py_SetProgramName(value); }
70+
set
71+
{
72+
if (_programName != IntPtr.Zero)
73+
{
74+
Marshal.FreeHGlobal(_programName);
75+
}
76+
_programName = Runtime.IsPython3
77+
? UcsMarshaler.GetInstance("").MarshalManagedToNative(value)
78+
: Marshal.StringToHGlobalAnsi(value);
79+
Runtime.Py_SetProgramName(_programName);
80+
}
6981
}
7082

7183
public static string PythonHome
@@ -103,7 +115,17 @@ public static string PythonPath
103115

104116
return result ?? "";
105117
}
106-
set { Runtime.Py_SetPath(value); }
118+
set
119+
{
120+
if (_pythonPath != IntPtr.Zero)
121+
{
122+
Marshal.FreeHGlobal(_pythonPath);
123+
}
124+
_pythonPath = Runtime.IsPython3
125+
? UcsMarshaler.GetInstance("").MarshalManagedToNative(value)
126+
: Marshal.StringToHGlobalAnsi(value);
127+
Runtime.Py_SetPath(_pythonPath);
128+
}
107129
}
108130

109131
public static string Version
@@ -297,6 +319,11 @@ public static void Shutdown()
297319
{
298320
Marshal.FreeHGlobal(_pythonHome);
299321
_pythonHome = IntPtr.Zero;
322+
Marshal.FreeHGlobal(_programName);
323+
_programName = IntPtr.Zero;
324+
Marshal.FreeHGlobal(_pythonPath);
325+
_pythonPath = IntPtr.Zero;
326+
300327
Runtime.Shutdown();
301328
initialized = false;
302329
}

src/runtime/runtime.cs

+4-8
Original file line numberDiff line numberDiff line change
@@ -686,9 +686,7 @@ 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();
@@ -700,15 +698,13 @@ internal static extern void Py_SetProgramName(
700698
internal static extern IntPtr Py_GetPath();
701699

702700
[DllImport(PythonDll)]
703-
internal static extern void Py_SetPath(
704-
[MarshalAs(UnmanagedType.LPWStr)] string home
705-
);
701+
internal static extern void Py_SetPath(IntPtr home);
706702
#elif PYTHON2
707703
[DllImport(PythonDll)]
708704
internal static extern IntPtr Py_GetProgramName();
709705

710706
[DllImport(PythonDll)]
711-
internal static extern void Py_SetProgramName(string name);
707+
internal static extern void Py_SetProgramName(IntPtr name);
712708

713709
[DllImport(PythonDll)]
714710
internal static extern IntPtr Py_GetPythonHome();
@@ -720,7 +716,7 @@ internal static extern void Py_SetPath(
720716
internal static extern IntPtr Py_GetPath();
721717

722718
[DllImport(PythonDll)]
723-
internal static extern void Py_SetPath(string home);
719+
internal static extern void Py_SetPath(IntPtr home);
724720
#endif
725721

726722
[DllImport(PythonDll)]

0 commit comments

Comments
 (0)