| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6668ebf commit 321aa28
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -130,5 +130,17 @@ public void SetPythonHomeTwice() | |||
| 130 | 130 | Assert.AreEqual(pythonHome, PythonEngine.PythonHome); | |
| 131 | 131 | PythonEngine.Shutdown(); | |
| 132 | 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 | + } | ||
| 133 | 145 | } | |
| 134 | 146 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,6 +15,8 @@ public class PythonEngine : IDisposable | |||
| 15 | 15 | private static DelegateManager delegateManager; | |
| 16 | 16 | private static bool initialized; | |
| 17 | 17 | private static IntPtr _pythonHome = IntPtr.Zero; | |
| 18 | + private static IntPtr _programName = IntPtr.Zero; | ||
| 19 | + private static IntPtr _pythonPath = IntPtr.Zero; | ||
| 18 | 20 | ||
| 19 | 21 | public PythonEngine() | |
| 20 | 22 | { | |
@@ -65,7 +67,14 @@ public static string ProgramName | |||
| 65 | 67 | ||
| 66 | 68 | return result ?? ""; | |
| 67 | 69 | } | |
| 68 | - 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 | + } | ||
| 69 | 78 | } | |
| 70 | 79 | ||
| 71 | 80 | public static string PythonHome | |
@@ -81,10 +90,7 @@ public static string PythonHome | |||
| 81 | 90 | } | |
| 82 | 91 | set | |
| 83 | 92 | { | |
| 84 | - if (_pythonHome != IntPtr.Zero) | ||
| 85 | - { | ||
| 86 | - Marshal.FreeHGlobal(_pythonHome); | ||
| 87 | - } | ||
| 93 | + Marshal.FreeHGlobal(_pythonHome); | ||
| 88 | 94 | _pythonHome = Runtime.IsPython3 | |
| 89 | 95 | ? UcsMarshaler.GetInstance("").MarshalManagedToNative(value) | |
| 90 | 96 | : Marshal.StringToHGlobalAnsi(value); | |
@@ -103,7 +109,14 @@ public static string PythonPath | |||
| 103 | 109 | ||
| 104 | 110 | return result ?? ""; | |
| 105 | 111 | } | |
| 106 | - 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 | + } | ||
| 107 | 120 | } | |
| 108 | 121 | ||
| 109 | 122 | public static string Version | |
@@ -297,6 +310,11 @@ public static void Shutdown() | |||
| 297 | 310 | { | |
| 298 | 311 | Marshal.FreeHGlobal(_pythonHome); | |
| 299 | 312 | _pythonHome = IntPtr.Zero; | |
| 313 | + Marshal.FreeHGlobal(_programName); | ||
| 314 | + _programName = IntPtr.Zero; | ||
| 315 | + Marshal.FreeHGlobal(_pythonPath); | ||
| 316 | + _pythonPath = IntPtr.Zero; | ||
| 317 | + | ||
| 300 | 318 | Runtime.Shutdown(); | |
| 301 | 319 | initialized = false; | |
| 302 | 320 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -686,9 +686,7 @@ public static extern int Py_Main( | |||
| 686 | 686 | internal static extern IntPtr Py_GetProgramName(); | |
| 687 | 687 | ||
| 688 | 688 | [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); | ||
| 692 | 690 | ||
| 693 | 691 | [DllImport(PythonDll)] | |
| 694 | 692 | internal static extern IntPtr Py_GetPythonHome(); | |
@@ -700,15 +698,13 @@ internal static extern void Py_SetProgramName( | |||
| 700 | 698 | internal static extern IntPtr Py_GetPath(); | |
| 701 | 699 | ||
| 702 | 700 | [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); | ||
| 706 | 702 | #elif PYTHON2 | |
| 707 | 703 | [DllImport(PythonDll)] | |
| 708 | 704 | internal static extern IntPtr Py_GetProgramName(); | |
| 709 | 705 | ||
| 710 | 706 | [DllImport(PythonDll)] | |
| 711 | - internal static extern void Py_SetProgramName(string name); | ||
| 707 | + internal static extern void Py_SetProgramName(IntPtr name); | ||
| 712 | 708 | ||
| 713 | 709 | [DllImport(PythonDll)] | |
| 714 | 710 | internal static extern IntPtr Py_GetPythonHome(); | |
@@ -720,7 +716,7 @@ internal static extern void Py_SetPath( | |||
| 720 | 716 | internal static extern IntPtr Py_GetPath(); | |
| 721 | 717 | ||
| 722 | 718 | [DllImport(PythonDll)] | |
| 723 | - internal static extern void Py_SetPath(string home); | ||
| 719 | + internal static extern void Py_SetPath(IntPtr home); | ||
| 724 | 720 | #endif | |
| 725 | 721 | ||
| 726 | 722 | [DllImport(PythonDll)] | |
| Back | FazBrowse Home | New Git URL |
0 commit comments