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
This repository was archived by the owner on Jul 22, 2023. It is now read-only.
/pythonnetPublic archive
forked frompythonnet/pythonnet

Commit365cd6e

Browse files
authored
Merge pull requestpythonnet#415 from vmuriart/fix_PYTHONHOME
Fix set pythonhome
2 parents0a47802 +321aa28 commit365cd6e

File tree

3 files changed

+77
-15
lines changed

3 files changed

+77
-15
lines changed

‎src/embed_tests/TestPythonEngineProperties.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,42 @@ public static void GetPythonHomeDefault()
105105
Assert.AreEqual(envPythonHome,enginePythonHome);
106106
PythonEngine.Shutdown();
107107
}
108+
109+
[Test]
110+
publicvoidSetPythonHome()
111+
{
112+
varpythonHome="/dummypath/";
113+
114+
PythonEngine.PythonHome=pythonHome;
115+
PythonEngine.Initialize();
116+
117+
Assert.AreEqual(pythonHome,PythonEngine.PythonHome);
118+
PythonEngine.Shutdown();
119+
}
120+
121+
[Test]
122+
publicvoidSetPythonHomeTwice()
123+
{
124+
varpythonHome="/dummypath/";
125+
126+
PythonEngine.PythonHome="/dummypath2/";
127+
PythonEngine.PythonHome=pythonHome;
128+
PythonEngine.Initialize();
129+
130+
Assert.AreEqual(pythonHome,PythonEngine.PythonHome);
131+
PythonEngine.Shutdown();
132+
}
133+
134+
[Test]
135+
publicvoidSetProgramName()
136+
{
137+
varprogramName="FooBar";
138+
139+
PythonEngine.ProgramName=programName;
140+
PythonEngine.Initialize();
141+
142+
Assert.AreEqual(programName,PythonEngine.ProgramName);
143+
PythonEngine.Shutdown();
144+
}
108145
}
109146
}

‎src/runtime/pythonengine.cs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public class PythonEngine : IDisposable
1414
{
1515
privatestaticDelegateManagerdelegateManager;
1616
privatestaticboolinitialized;
17+
privatestaticIntPtr_pythonHome=IntPtr.Zero;
18+
privatestaticIntPtr_programName=IntPtr.Zero;
19+
privatestaticIntPtr_pythonPath=IntPtr.Zero;
1720

1821
publicPythonEngine()
1922
{
@@ -64,7 +67,14 @@ public static string ProgramName
6467

6568
returnresult??"";
6669
}
67-
set{Runtime.Py_SetProgramName(value);}
70+
set
71+
{
72+
Marshal.FreeHGlobal(_programName);
73+
_programName=Runtime.IsPython3
74+
?UcsMarshaler.GetInstance("").MarshalManagedToNative(value)
75+
:Marshal.StringToHGlobalAnsi(value);
76+
Runtime.Py_SetProgramName(_programName);
77+
}
6878
}
6979

7080
publicstaticstringPythonHome
@@ -78,7 +88,14 @@ public static string PythonHome
7888

7989
returnresult??"";
8090
}
81-
set{Runtime.Py_SetPythonHome(value);}
91+
set
92+
{
93+
Marshal.FreeHGlobal(_pythonHome);
94+
_pythonHome=Runtime.IsPython3
95+
?UcsMarshaler.GetInstance("").MarshalManagedToNative(value)
96+
:Marshal.StringToHGlobalAnsi(value);
97+
Runtime.Py_SetPythonHome(_pythonHome);
98+
}
8299
}
83100

84101
publicstaticstringPythonPath
@@ -92,7 +109,14 @@ public static string PythonPath
92109

93110
returnresult??"";
94111
}
95-
set{Runtime.Py_SetPath(value);}
112+
set
113+
{
114+
Marshal.FreeHGlobal(_pythonPath);
115+
_pythonPath=Runtime.IsPython3
116+
?UcsMarshaler.GetInstance("").MarshalManagedToNative(value)
117+
:Marshal.StringToHGlobalAnsi(value);
118+
Runtime.Py_SetPath(_pythonPath);
119+
}
96120
}
97121

98122
publicstaticstringVersion
@@ -284,6 +308,13 @@ public static void Shutdown()
284308
{
285309
if(initialized)
286310
{
311+
Marshal.FreeHGlobal(_pythonHome);
312+
_pythonHome=IntPtr.Zero;
313+
Marshal.FreeHGlobal(_programName);
314+
_programName=IntPtr.Zero;
315+
Marshal.FreeHGlobal(_pythonPath);
316+
_pythonPath=IntPtr.Zero;
317+
287318
Runtime.Shutdown();
288319
initialized=false;
289320
}

‎src/runtime/runtime.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -686,43 +686,37 @@ public static extern int Py_Main(
686686
internalstaticexternIntPtr Py_GetProgramName();
687687

688688
[DllImport(PythonDll)]
689-
internalstaticexternvoidPy_SetProgramName(
690-
[MarshalAs(UnmanagedType.LPWStr)]stringname
691-
);
689+
internalstaticexternvoidPy_SetProgramName(IntPtrname);
692690

693691
[DllImport(PythonDll)]
694692
internalstaticexternIntPtr Py_GetPythonHome();
695693

696694
[DllImport(PythonDll)]
697-
internalstaticexternvoidPy_SetPythonHome(
698-
[MarshalAs(UnmanagedType.LPWStr)]stringhome
699-
);
695+
internalstaticexternvoidPy_SetPythonHome(IntPtrhome);
700696

701697
[DllImport(PythonDll)]
702698
internalstaticexternIntPtr Py_GetPath();
703699

704700
[DllImport(PythonDll)]
705-
internalstaticexternvoidPy_SetPath(
706-
[MarshalAs(UnmanagedType.LPWStr)]stringhome
707-
);
701+
internalstaticexternvoidPy_SetPath(IntPtrhome);
708702
#elifPYTHON2
709703
[DllImport(PythonDll)]
710704
internalstaticexternIntPtr Py_GetProgramName();
711705

712706
[DllImport(PythonDll)]
713-
internalstaticexternvoidPy_SetProgramName(stringname);
707+
internalstaticexternvoidPy_SetProgramName(IntPtrname);
714708

715709
[DllImport(PythonDll)]
716710
internalstaticexternIntPtr Py_GetPythonHome();
717711

718712
[DllImport(PythonDll)]
719-
internalstaticexternvoidPy_SetPythonHome(stringhome);
713+
internalstaticexternvoidPy_SetPythonHome(IntPtrhome);
720714

721715
[DllImport(PythonDll)]
722716
internalstaticexternIntPtr Py_GetPath();
723717

724718
[DllImport(PythonDll)]
725-
internalstaticexternvoidPy_SetPath(stringhome);
719+
internalstaticexternvoidPy_SetPath(IntPtrhome);
726720
#endif
727721

728722
[DllImport(PythonDll)]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp