| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7898833 commit ed29200
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,5 @@ | |||
| 1 | 1 | using System; | |
| 2 | + using System.Diagnostics; | ||
| 2 | 3 | using NUnit.Framework; | |
| 3 | 4 | using Python.Runtime; | |
| 4 | 5 | ||
@@ -109,18 +110,41 @@ public static void GetPythonHomeDefault() | |||
| 109 | 110 | [Test] | |
| 110 | 111 | public void SetPythonHome() | |
| 111 | 112 | { | |
| 113 | + // We needs to ensure that engine was started and shutdown at least once before setting dummy home. | ||
| 114 | + // Otherwise engine will not run with dummy path with random problem. | ||
| 115 | + if (!PythonEngine.IsInitialized) | ||
| 116 | + { | ||
| 117 | + PythonEngine.Initialize(); | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + PythonEngine.Shutdown(); | ||
| 121 | + | ||
| 122 | + var pythonHomeBackup = PythonEngine.PythonHome; | ||
| 123 | + | ||
| 112 | 124 | var pythonHome = "/dummypath/"; | |
| 113 | 125 | ||
| 114 | 126 | PythonEngine.PythonHome = pythonHome; | |
| 115 | 127 | PythonEngine.Initialize(); | |
| 116 | 128 | ||
| 117 | - Assert.AreEqual(pythonHome, PythonEngine.PythonHome); | ||
| 118 | 129 | PythonEngine.Shutdown(); | |
| 130 | + | ||
| 131 | + // Restoring valid pythonhome. | ||
| 132 | + PythonEngine.PythonHome = pythonHomeBackup; | ||
| 119 | 133 | } | |
| 120 | 134 | ||
| 121 | 135 | [Test] | |
| 122 | 136 | public void SetPythonHomeTwice() | |
| 123 | 137 | { | |
| 138 | + // We needs to ensure that engine was started and shutdown at least once before setting dummy home. | ||
| 139 | + // Otherwise engine will not run with dummy path with random problem. | ||
| 140 | + if (!PythonEngine.IsInitialized) | ||
| 141 | + { | ||
| 142 | + PythonEngine.Initialize(); | ||
| 143 | + } | ||
| 144 | + PythonEngine.Shutdown(); | ||
| 145 | + | ||
| 146 | + var pythonHomeBackup = PythonEngine.PythonHome; | ||
| 147 | + | ||
| 124 | 148 | var pythonHome = "/dummypath/"; | |
| 125 | 149 | ||
| 126 | 150 | PythonEngine.PythonHome = "/dummypath2/"; | |
@@ -129,18 +153,29 @@ public void SetPythonHomeTwice() | |||
| 129 | 153 | ||
| 130 | 154 | Assert.AreEqual(pythonHome, PythonEngine.PythonHome); | |
| 131 | 155 | PythonEngine.Shutdown(); | |
| 156 | + | ||
| 157 | + PythonEngine.PythonHome = pythonHomeBackup; | ||
| 132 | 158 | } | |
| 133 | 159 | ||
| 134 | 160 | [Test] | |
| 135 | 161 | public void SetProgramName() | |
| 136 | 162 | { | |
| 163 | + if (PythonEngine.IsInitialized) | ||
| 164 | + { | ||
| 165 | + PythonEngine.Shutdown(); | ||
| 166 | + } | ||
| 167 | + | ||
| 168 | + var programNameBackup = PythonEngine.ProgramName; | ||
| 169 | + | ||
| 137 | 170 | var programName = "FooBar"; | |
| 138 | 171 | ||
| 139 | 172 | PythonEngine.ProgramName = programName; | |
| 140 | 173 | PythonEngine.Initialize(); | |
| 141 | 174 | ||
| 142 | 175 | Assert.AreEqual(programName, PythonEngine.ProgramName); | |
| 143 | 176 | PythonEngine.Shutdown(); | |
| 177 | + | ||
| 178 | + PythonEngine.ProgramName = programNameBackup; | ||
| 144 | 179 | } | |
| 145 | 180 | ||
| 146 | 181 | [Test] | |
@@ -156,7 +191,7 @@ public void SetPythonPath() | |||
| 156 | 191 | string path = PythonEngine.PythonPath; | |
| 157 | 192 | PythonEngine.Shutdown(); | |
| 158 | 193 | ||
| 159 | - PythonEngine.ProgramName = path; | ||
| 194 | + PythonEngine.PythonPath = path; | ||
| 160 | 195 | PythonEngine.Initialize(); | |
| 161 | 196 | ||
| 162 | 197 | Assert.AreEqual(path, PythonEngine.PythonPath); | |
@@ -171,7 +206,6 @@ public void SetPythonPathExceptionOn27() | |||
| 171 | 206 | Assert.Pass(); | |
| 172 | 207 | } | |
| 173 | 208 | ||
| 174 | - // Get previous path to avoid crashing Python | ||
| 175 | 209 | PythonEngine.Initialize(); | |
| 176 | 210 | string path = PythonEngine.PythonPath; | |
| 177 | 211 | PythonEngine.Shutdown(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -295,12 +295,16 @@ public static void Shutdown() | |||
| 295 | 295 | if (initialized) | |
| 296 | 296 | { | |
| 297 | 297 | PyScopeManager.Global.Clear(); | |
| 298 | - Marshal.FreeHGlobal(_pythonHome); | ||
| 299 | - _pythonHome = IntPtr.Zero; | ||
| 300 | - Marshal.FreeHGlobal(_programName); | ||
| 301 | - _programName = IntPtr.Zero; | ||
| 302 | - Marshal.FreeHGlobal(_pythonPath); | ||
| 303 | - _pythonPath = IntPtr.Zero; | ||
| 298 | + // We should not release memory for variables that can be used without initialized python engine. | ||
| 299 | + // It's assumed that Py_GetPythonHome returns valid string without engine initialize. Py_GetPythonHome will always return the | ||
| 300 | + // same pointer that was passed before to Py_SetPythonHome and stored in the _pythonHome. | ||
| 301 | + | ||
| 302 | + ////Marshal.FreeHGlobal(_pythonHome); | ||
| 303 | + ////_pythonHome = IntPtr.Zero; | ||
| 304 | + ////Marshal.FreeHGlobal(_programName); | ||
| 305 | + ////_programName = IntPtr.Zero; | ||
| 306 | + ////Marshal.FreeHGlobal(_pythonPath); | ||
| 307 | + ////_pythonPath = IntPtr.Zero; | ||
| 304 | 308 | ||
| 305 | 309 | Runtime.Shutdown(); | |
| 306 | 310 | initialized = false; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments