Skip to content

Explicit marshalling of Py_SetPythonHome string. #186

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 2 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
Next Next commit
Py_SetPythonHome argument updated to IntPtr such that string marhsal …
…is explicit and will not be freed automatically.
  • Loading branch information
leomeuk committed Mar 13, 2016
commit 63c43dd3ad5b8ccf59bb6305f8ce684b4c3f1d2e
6 changes: 4 additions & 2 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
using System.IO;
using System.Threading;
using System.Reflection;

using System.Runtime.InteropServices;

namespace Python.Runtime {

/// <summary>
Expand Down Expand Up @@ -62,7 +63,8 @@ public static string PythonHome {
return result;
}
set {
Runtime.Py_SetPythonHome(value);
IntPtr pythonHome = Marshal.StringToHGlobalAnsi(value);
Runtime.Py_SetPythonHome(pythonHome);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)]
Expand Down Expand Up @@ -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)]
Expand Down