Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
TestPythonEngineProperties fixes.
  • Loading branch information
dse authored and filmor committed Nov 14, 2018
commit d3315d490682374901be435065511ac6550313c6
40 changes: 37 additions & 3 deletions src/embed_tests/TestPythonEngineProperties.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using NUnit.Framework;
using Python.Runtime;

Expand Down Expand Up @@ -109,18 +110,41 @@ public static void GetPythonHomeDefault()
[Test]
public void SetPythonHome()
{
// We needs to ensure that engine was started and shutdown at least once before setting dummy home.
// Otherwise engine will not run with dummy path with random problem.
if (!PythonEngine.IsInitialized)
{
PythonEngine.Initialize();
}

PythonEngine.Shutdown();

var pythonHomeBackup = PythonEngine.PythonHome;

var pythonHome = "/dummypath/";

PythonEngine.PythonHome = pythonHome;
PythonEngine.Initialize();

Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
PythonEngine.Shutdown();

// Restoring valid pythonhome.
PythonEngine.PythonHome = pythonHomeBackup;
}

[Test]
public void SetPythonHomeTwice()
{
// We needs to ensure that engine was started and shutdown at least once before setting dummy home.
// Otherwise engine will not run with dummy path with random problem.
if (!PythonEngine.IsInitialized)
{
PythonEngine.Initialize();
}
PythonEngine.Shutdown();

var pythonHomeBackup = PythonEngine.PythonHome;

var pythonHome = "/dummypath/";

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

Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
PythonEngine.Shutdown();

PythonEngine.PythonHome = pythonHomeBackup;
}

[Test]
public void SetProgramName()
{
if (PythonEngine.IsInitialized)
{
PythonEngine.Shutdown();
}

var programNameBackup = PythonEngine.ProgramName;

var programName = "FooBar";

PythonEngine.ProgramName = programName;
PythonEngine.Initialize();

Assert.AreEqual(programName, PythonEngine.ProgramName);
PythonEngine.Shutdown();

PythonEngine.ProgramName = programNameBackup;
}

[Test]
Expand All @@ -156,7 +191,7 @@ public void SetPythonPath()
string path = PythonEngine.PythonPath;
PythonEngine.Shutdown();

PythonEngine.ProgramName = path;
PythonEngine.PythonPath = path;
PythonEngine.Initialize();

Assert.AreEqual(path, PythonEngine.PythonPath);
Expand All @@ -171,7 +206,6 @@ public void SetPythonPathExceptionOn27()
Assert.Pass();
}

// Get previous path to avoid crashing Python
PythonEngine.Initialize();
string path = PythonEngine.PythonPath;
PythonEngine.Shutdown();
Expand Down
9 changes: 2 additions & 7 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,19 +309,14 @@ public static void Shutdown()
{
if (initialized)
{
PyScopeManager.Global.Clear();

// If the shutdown handlers trigger a domain unload,
// don't call shutdown again.
AppDomain.CurrentDomain.DomainUnload -= OnDomainUnload;

ExecuteShutdownHandlers();

Marshal.FreeHGlobal(_pythonHome);
_pythonHome = IntPtr.Zero;
Marshal.FreeHGlobal(_programName);
_programName = IntPtr.Zero;
Marshal.FreeHGlobal(_pythonPath);
_pythonPath = IntPtr.Zero;

initialized = false;
}
}
Expand Down