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
Fix set PythonPath, set ProgramName
Note on PythonPath. Its actually mapping to `Py_SetPath` which is
very different from PYTHONPATH env var. There is no test on it
because it should be set to real paths with libraries. Otherwise it
crashes.

2nd Note. `Py_SetPath` doesn't exist on PY27.
  • Loading branch information
vmuriart committed Mar 3, 2017
commit 321aa28ba6229bf5eea619c6d484093c16f12700
12 changes: 12 additions & 0 deletions src/embed_tests/TestPythonEngineProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,17 @@ public void SetPythonHomeTwice()
Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
PythonEngine.Shutdown();
}

[Test]
public void SetProgramName()
{
var programName = "FooBar";

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

Assert.AreEqual(programName, PythonEngine.ProgramName);
PythonEngine.Shutdown();
}
}
}
30 changes: 24 additions & 6 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class PythonEngine : IDisposable
private static DelegateManager delegateManager;
private static bool initialized;
private static IntPtr _pythonHome = IntPtr.Zero;
private static IntPtr _programName = IntPtr.Zero;
private static IntPtr _pythonPath = IntPtr.Zero;

public PythonEngine()
{
Expand Down Expand Up @@ -65,7 +67,14 @@ public static string ProgramName

return result ?? "";
}
set { Runtime.Py_SetProgramName(value); }
set
{
Marshal.FreeHGlobal(_programName);
_programName = Runtime.IsPython3
? UcsMarshaler.GetInstance("").MarshalManagedToNative(value)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

2 custom marshalling articles suggest that GetInstance should be a singleton like this:

public static ICustomMarshaler GetInstance(string cookie)
   {
       if (marshaler == null)
       {
              marshaler = new UcsMarshaler();
       }
       return marshaler;
   }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it is already.

private static readonly MarshalerBase Instance = new Utf8Marshaler();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

oh, sorry, missed that!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No worries. I almost went w that implementation

: Marshal.StringToHGlobalAnsi(value);
Runtime.Py_SetProgramName(_programName);
}
}

public static string PythonHome
Expand All @@ -81,10 +90,7 @@ public static string PythonHome
}
set
{
if (_pythonHome != IntPtr.Zero)
{
Marshal.FreeHGlobal(_pythonHome);
}
Marshal.FreeHGlobal(_pythonHome);
_pythonHome = Runtime.IsPython3
? UcsMarshaler.GetInstance("").MarshalManagedToNative(value)
: Marshal.StringToHGlobalAnsi(value);
Expand All @@ -103,7 +109,14 @@ public static string PythonPath

return result ?? "";
}
set { Runtime.Py_SetPath(value); }
set
{
Marshal.FreeHGlobal(_pythonPath);
_pythonPath = Runtime.IsPython3
? UcsMarshaler.GetInstance("").MarshalManagedToNative(value)
: Marshal.StringToHGlobalAnsi(value);
Runtime.Py_SetPath(_pythonPath);
}
}

public static string Version
Expand Down Expand Up @@ -297,6 +310,11 @@ public static void Shutdown()
{
Marshal.FreeHGlobal(_pythonHome);
_pythonHome = IntPtr.Zero;
Marshal.FreeHGlobal(_programName);
_programName = IntPtr.Zero;
Marshal.FreeHGlobal(_pythonPath);
_pythonPath = IntPtr.Zero;

Runtime.Shutdown();
initialized = false;
}
Expand Down
12 changes: 4 additions & 8 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,7 @@ public static extern int Py_Main(
internal static extern IntPtr Py_GetProgramName();

[DllImport(PythonDll)]
internal static extern void Py_SetProgramName(
[MarshalAs(UnmanagedType.LPWStr)] string name
);
internal static extern void Py_SetProgramName(IntPtr name);

[DllImport(PythonDll)]
internal static extern IntPtr Py_GetPythonHome();
Expand All @@ -700,15 +698,13 @@ internal static extern void Py_SetProgramName(
internal static extern IntPtr Py_GetPath();

[DllImport(PythonDll)]
internal static extern void Py_SetPath(
[MarshalAs(UnmanagedType.LPWStr)] string home
);
internal static extern void Py_SetPath(IntPtr home);
#elif PYTHON2
[DllImport(PythonDll)]
internal static extern IntPtr Py_GetProgramName();

[DllImport(PythonDll)]
internal static extern void Py_SetProgramName(string name);
internal static extern void Py_SetProgramName(IntPtr name);

[DllImport(PythonDll)]
internal static extern IntPtr Py_GetPythonHome();
Expand All @@ -720,7 +716,7 @@ internal static extern void Py_SetPath(
internal static extern IntPtr Py_GetPath();

[DllImport(PythonDll)]
internal static extern void Py_SetPath(string home);
internal static extern void Py_SetPath(IntPtr home);
#endif

[DllImport(PythonDll)]
Expand Down