8000 Python to CLR string marshaling LRU cache. by dmitriyse · Pull Request #538 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Python to CLR string marshaling LRU cache. #538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 19 commits into from
Closed
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 committed Sep 5, 2017
commit ed29200beba1a6f4918a25332a07d8b6695959dd
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
16 changes: 10 additions & 6 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,16 @@ public static void Shutdown()
if (initialized)
{
PyScopeManager.Global.Clear();
Marshal.FreeHGlobal(_pythonHome);
_pythonHome = IntPtr.Zero;
Marshal.FreeHGlobal(_programName);
_programName = IntPtr.Zero;
Marshal.FreeHGlobal(_pythonPath);
_pythonPath = IntPtr.Zero;
// We should not release memory for variables that can be used without initialized python engine.
// It's assumed that Py_GetPythonHome returns valid string without engine initialize. Py_GetPythonHome will always return the
// same pointer that was passed before to Py_SetPythonHome and stored in the _pythonHome.

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

Runtime.Shutdown();
initialized = false;
Expand Down
0