From 63c43dd3ad5b8ccf59bb6305f8ce684b4c3f1d2e Mon Sep 17 00:00:00 2001 From: leo Date: Sun, 13 Mar 2016 12:59:52 +0000 Subject: [PATCH 1/2] Py_SetPythonHome argument updated to IntPtr such that string marhsal is explicit and will not be freed automatically. --- src/runtime/pythonengine.cs | 6 ++++-- src/runtime/runtime.cs | 10 +++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/runtime/pythonengine.cs b/src/runtime/pythonengine.cs index 0cb99bffe..faf4029c0 100644 --- a/src/runtime/pythonengine.cs +++ b/src/runtime/pythonengine.cs @@ -11,7 +11,8 @@ using System.IO; using System.Threading; using System.Reflection; - +using System.Runtime.InteropServices; + namespace Python.Runtime { /// @@ -62,7 +63,8 @@ public static string PythonHome { return result; } set { - Runtime.Py_SetPythonHome(value); + IntPtr pythonHome = Marshal.StringToHGlobalAnsi(value); + Runtime.Py_SetPythonHome(pythonHome); } } diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index 260d968d8..86a011eda 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -738,9 +738,9 @@ internal unsafe static extern IntPtr [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] internal unsafe static extern IntPtr - PyEval_GetLocals(); - - + PyEval_GetLocals(); + + #if PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] @@ -762,7 +762,7 @@ internal unsafe static extern string [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] internal unsafe static extern void - Py_SetPythonHome([MarshalAsAttribute(UnmanagedType.LPWStr)]string home); + Py_SetPythonHome(IntPtr home); [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] @@ -793,7 +793,7 @@ internal unsafe static extern string [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern void - Py_SetPythonHome(string home); + Py_SetPythonHome(IntPtr home); [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] From e6294a77897ca00ab6e3d90e06f62b23a1adea68 Mon Sep 17 00:00:00 2001 From: leo Date: Sun, 13 Mar 2016 14:01:04 +0000 Subject: [PATCH 2/2] Unit test for verifying Python27 home path is correctly set. --- src/embed_tests/pysethome.cs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/embed_tests/pysethome.cs diff --git a/src/embed_tests/pysethome.cs b/src/embed_tests/pysethome.cs new file mode 100644 index 000000000..310842c6f --- /dev/null +++ b/src/embed_tests/pysethome.cs @@ -0,0 +1,32 @@ +using System; +using System.Reflection; +using System.Collections.Generic; +using NUnit.Framework; +using Python.Runtime; + + +namespace Python.EmbeddingTest +{ + public class PySetHomeSet + { + [SetUp] + public void SetUp() + { + } + + [TearDown] + public void TearDown() + { + PythonEngine.Shutdown(); + } + + [Test] + public void TestSetHome() + { + string homePath = @"C:\Python27\"; + PythonEngine.PythonHome = homePath; + PythonEngine.Initialize(); + Assert.AreEqual(PythonEngine.PythonHome, homePath); + } + } +}