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

Skip to content

Commit ed29200

Browse files
author
dse
committed
TestPythonEngineProperties fixes.
1 parent 7898833 commit ed29200

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
@@ -295,12 +295,16 @@ public static void Shutdown()
295295
if (initialized)
296296
{
297297
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;
304308

305309
Runtime.Shutdown();
306310
initialized = false;

0 commit comments

Comments
 (0)