Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b64091b

Browse files
sweinsttonyroberts
authored andcommitted
Fixed call to Py_SetPythonHome and Py_SetProgramName. Added support for Py_SetPythonPath
1 parent 79457fe commit b64091b

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

pythonnet/src/runtime/pythonengine.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ public static string PythonHome {
6464
}
6565
}
6666

67+
public static string PythonPath {
68+
get
69+
{
70+
string result = Runtime.Py_GetPath();
71+
if (result == null)
72+
{
73+
return "";
74+
}
75+
return result;
76+
}
77+
set
78+
{
79+
Runtime.Py_SetPath(value);
80+
}
81+
}
82+
6783
public static string Version {
6884
get {
6985
return Runtime.Py_GetVersion();

pythonnet/src/runtime/runtime.cs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,25 +583,70 @@ internal unsafe static extern IntPtr
583583
PyEval_GetLocals();
584584

585585

586+
#if PYTHON32 || PYTHON33 || PYTHON34
586587
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
587588
ExactSpelling=true, CharSet=CharSet.Ansi)]
589+
[return: MarshalAs(UnmanagedType.LPWStr)]
588590
internal unsafe static extern string
589591
Py_GetProgramName();
590592

591593
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
592594
ExactSpelling=true, CharSet=CharSet.Ansi)]
593595
internal unsafe static extern void
594-
Py_SetProgramName(string name);
596+
Py_SetProgramName([MarshalAsAttribute(UnmanagedType.LPWStr)]string name);
595597

596598
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
597599
ExactSpelling=true, CharSet=CharSet.Ansi)]
600+
[return: MarshalAs(UnmanagedType.LPWStr)]
598601
internal unsafe static extern string
599602
Py_GetPythonHome();
600603

601604
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
602605
ExactSpelling=true, CharSet=CharSet.Ansi)]
603606
internal unsafe static extern void
604-
Py_SetPythonHome(string home);
607+
Py_SetPythonHome([MarshalAsAttribute(UnmanagedType.LPWStr)]string home);
608+
609+
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
610+
ExactSpelling=true, CharSet=CharSet.Ansi)]
611+
[return: MarshalAs(UnmanagedType.LPWStr)]
612+
internal unsafe static extern string
613+
Py_GetPath();
614+
615+
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
616+
ExactSpelling=true, CharSet=CharSet.Ansi)]
617+
internal unsafe static extern void
618+
Py_SetPath([MarshalAsAttribute(UnmanagedType.LPWStr)]string home);
619+
#else
620+
[DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl,
621+
ExactSpelling = true, CharSet = CharSet.Ansi)]
622+
internal unsafe static extern string
623+
Py_GetProgramName();
624+
625+
[DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl,
626+
ExactSpelling = true, CharSet = CharSet.Ansi)]
627+
internal unsafe static extern void
628+
Py_SetProgramName(string name);
629+
630+
[DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl,
631+
ExactSpelling = true, CharSet = CharSet.Ansi)]
632+
internal unsafe static extern string
633+
Py_GetPythonHome();
634+
635+
[DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl,
636+
ExactSpelling = true, CharSet = CharSet.Ansi)]
637+
internal unsafe static extern void
638+
Py_SetPythonHome(string home);
639+
640+
[DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl,
641+
ExactSpelling = true, CharSet = CharSet.Ansi)]
642+
internal unsafe static extern string
643+
Py_GetPath();
644+
645+
[DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl,
646+
ExactSpelling = true, CharSet = CharSet.Ansi)]
647+
internal unsafe static extern void
648+
Py_SetPath(string home);
649+
#endif
605650

606651
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
607652
ExactSpelling=true, CharSet=CharSet.Ansi)]

0 commit comments

Comments
 (0)