Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix set pythonhome#415

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
vmuriart merged 2 commits intopythonnet:masterfromvmuriart:fix_PYTHONHOME
Mar 3, 2017
Merged
Show file tree
Hide file tree
Changes fromall 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
37 changes: 37 additions & 0 deletionssrc/embed_tests/TestPythonEngineProperties.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -105,5 +105,42 @@ public static void GetPythonHomeDefault()
Assert.AreEqual(envPythonHome, enginePythonHome);
PythonEngine.Shutdown();
}

[Test]
public void SetPythonHome()
{
var pythonHome = "/dummypath/";

PythonEngine.PythonHome = pythonHome;
PythonEngine.Initialize();

Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
PythonEngine.Shutdown();
}

[Test]
public void SetPythonHomeTwice()
{
var pythonHome = "/dummypath/";

PythonEngine.PythonHome = "/dummypath2/";
PythonEngine.PythonHome = pythonHome;
PythonEngine.Initialize();

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();
}
}
}
37 changes: 34 additions & 3 deletionssrc/runtime/pythonengine.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,6 +14,9 @@ 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 DownExpand Up@@ -64,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)
: Marshal.StringToHGlobalAnsi(value);
Runtime.Py_SetProgramName(_programName);
}
}

public static string PythonHome
Expand All@@ -78,7 +88,14 @@ public static string PythonHome

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

public static string PythonPath
Expand All@@ -92,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 DownExpand Up@@ -284,6 +308,13 @@ public static void Shutdown()
{
if (initialized)
{
Marshal.FreeHGlobal(_pythonHome);
_pythonHome = IntPtr.Zero;
Marshal.FreeHGlobal(_programName);
_programName = IntPtr.Zero;
Marshal.FreeHGlobal(_pythonPath);
_pythonPath = IntPtr.Zero;

Runtime.Shutdown();
initialized = false;
}
Expand Down
18 changes: 6 additions & 12 deletionssrc/runtime/runtime.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -686,43 +686,37 @@ 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();

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

[DllImport(PythonDll)]
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();

[DllImport(PythonDll)]
internal static extern void Py_SetPythonHome(string home);
internal static extern void Py_SetPythonHome(IntPtr home);

[DllImport(PythonDll)]
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

[8]ページ先頭

©2009-2025 Movatter.jp