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 all commits
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
32 changes: 32 additions & 0 deletions src/embed_tests/pysethome.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leomeuk you can get python path using this c# code which requires python interpreter:

dynamic pyos = Py.Import("os");
string homepath = pyos.path.dirname(pyos.path.dirname(pyos.__file__));

Note that PythonEngine.PythonPath is also "broken" like PythonEngine.PythonHome:

> #r "C:\Python\Python27\Lib\site-packages\Python.Runtime.dll"
> using Python.Runtime;
> PythonEngine.Initialize()
> PythonEngine.PythonPath
Hosting process exited with exit code -1073740940.
Loading context from 'CSharpInteractive.rsp'.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updating this PR and noticed this comment. I documented the issue with PythonEngine.PythonPath on #414. All the get functions were broken on 64bit python.

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