8000 TestPythonEngineProperties fixes. · pythonnet/pythonnet@d3315d4 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d3315d4

Browse files
dsefilmor
authored andcommitted
TestPythonEngineProperties fixes.
1 parent 6a6414d commit d3315d4

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
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: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,19 +309,14 @@ public static void Shutdown()
309309
{
310310
if (initialized)
311311
{
312+
PyScopeManager.Global.Clear();
313+
312314
// If the shutdown handlers trigger a domain unload,
313315
// don't call shutdown again.
314316
AppDomain.CurrentDomain.DomainUnload -= OnDomainUnload;
315317

316318
ExecuteShutdownHandlers();
317319

318-
Marshal.FreeHGlobal(_pythonHome);
319-
_pythonHome = IntPtr.Zero;
320-
Marshal.FreeHGlobal(_programName);
321-
_programName = IntPtr.Zero;
322-
Marshal.FreeHGlobal(_pythonPath);
323-
_pythonPath = IntPtr.Zero;
324-
325320
initialized = false;
326321
}
327322
}

0 commit comments

Comments
 (0)
0