Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 982d916

Browse files
dsefilmor
dse
authored andcommitted
TestPythonEngineProperties fixes.
1 parent 91cc607 commit 982d916

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

src/embed_tests/TestPythonEngineProperties.cs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using NUnit.Framework;
34
using Python.Runtime;
45

@@ -109,18 +110,41 @@ public static void GetPythonHomeDefault()
109110
[Test]
110111
public void SetPythonHome()
111112
{
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+
112124
var pythonHome = "/dummypath/";
113125

114126
PythonEngine.PythonHome = pythonHome;
115127
PythonEngine.Initialize();
116128

117-
Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
118129
PythonEngine.Shutdown();
130+
131+
// Restoring valid pythonhome.
132+
PythonEngine.PythonHome = pythonHomeBackup;
119133
}
120134

121135
[Test]
122136
public void SetPythonHomeTwice()
123137
{
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+
124148
var pythonHome = "/dummypath/";
125149

126150
PythonEngine.PythonHome = "/dummypath2/";
@@ -129,18 +153,29 @@ public void SetPythonHomeTwice()
129153

130154
Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
131155
PythonEngine.Shutdown();
156+
157+
PythonEngine.PythonHome = pythonHomeBackup;
132158
}
133159

134160
[Test]
135161
public void SetProgramName()
136162
{
163+
if (PythonEngine.IsInitialized)
164+
{
165+
PythonEngine.Shutdown();
166+
}
167+
168+
var programNameBackup = PythonEngine.ProgramName;
169+
137170
var programName = "FooBar";
138171

139172
PythonEngine.ProgramName = programName;
140173
PythonEngine.Initialize();
141174

142175
Assert.AreEqual(programName, PythonEngine.ProgramName);
143176
PythonEngine.Shutdown();
177+
178+
PythonEngine.ProgramName = programNameBackup;
144179
}
145180

146181
[Test]
@@ -156,7 +191,7 @@ public void SetPythonPath()
156191
string path = PythonEngine.PythonPath;
157192
PythonEngine.Shutdown();
158193

159-
PythonEngine.ProgramName = path;
194+
PythonEngine.PythonPath = path;
160195
PythonEngine.Initialize();
161196

162197
Assert.AreEqual(path, PythonEngine.PythonPath);
@@ -171,7 +206,6 @@ public void SetPythonPathExceptionOn27()
171206
Assert.Pass();
172207
}
173208

174-
// Get previous path to avoid crashing Python
175209
PythonEngine.Initialize();
176210
string path = PythonEngine.PythonPath;
177211
PythonEngine.Shutdown();

src/runtime/pythonengine.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,16 @@ public static void Shutdown()
303303
if (initialized)
304304
{
305305
PyScopeManager.Global.Clear();
306-
Marshal.FreeHGlobal(_pythonHome);
307-
_pythonHome = IntPtr.Zero;
308-
Marshal.FreeHGlobal(_programName);
309-
_programName = IntPtr.Zero;
310-
Marshal.FreeHGlobal(_pythonPath);
311-
_pythonPath = IntPtr.Zero;
306+
// We should not release memory for variables that can be used without initialized python engine.
307+
// It's assumed that Py_GetPythonHome returns valid string without engine initialize. Py_GetPythonHome will always return the
308+
// same pointer that was passed before to Py_SetPythonHome and stored in the _pythonHome.
309+
310+
////Marshal.FreeHGlobal(_pythonHome);
311+
////_pythonHome = IntPtr.Zero;
312+
////Marshal.FreeHGlobal(_programName);
313+
////_programName = IntPtr.Zero;
314+
////Marshal.FreeHGlobal(_pythonPath);
315+
////_pythonPath = IntPtr.Zero;
312316

313317
Runtime.Shutdown();
314318

0 commit comments

Comments
 (0)