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

Commit321aa28

Browse files
committed
Fix set PythonPath, set ProgramName
Note on PythonPath. Its actually mapping to `Py_SetPath` which isvery different from PYTHONPATH env var. There is no test on itbecause it should be set to real paths with libraries. Otherwise itcrashes.2nd Note. `Py_SetPath` doesn't exist on PY27.
1 parent6668ebf commit321aa28

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

‎src/embed_tests/TestPythonEngineProperties.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,17 @@ public void SetPythonHomeTwice()
130130
Assert.AreEqual(pythonHome,PythonEngine.PythonHome);
131131
PythonEngine.Shutdown();
132132
}
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+
}
133145
}
134146
}

‎src/runtime/pythonengine.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class PythonEngine : IDisposable
1515
privatestaticDelegateManagerdelegateManager;
1616
privatestaticboolinitialized;
1717
privatestaticIntPtr_pythonHome=IntPtr.Zero;
18+
privatestaticIntPtr_programName=IntPtr.Zero;
19+
privatestaticIntPtr_pythonPath=IntPtr.Zero;
1820

1921
publicPythonEngine()
2022
{
@@ -65,7 +67,14 @@ public static string ProgramName
6567

6668
returnresult??"";
6769
}
68-
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+
}
6978
}
7079

7180
publicstaticstringPythonHome
@@ -81,10 +90,7 @@ public static string PythonHome
8190
}
8291
set
8392
{
84-
if(_pythonHome!=IntPtr.Zero)
85-
{
86-
Marshal.FreeHGlobal(_pythonHome);
87-
}
93+
Marshal.FreeHGlobal(_pythonHome);
8894
_pythonHome=Runtime.IsPython3
8995
?UcsMarshaler.GetInstance("").MarshalManagedToNative(value)
9096
:Marshal.StringToHGlobalAnsi(value);
@@ -103,7 +109,14 @@ public static string PythonPath
103109

104110
returnresult??"";
105111
}
106-
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+
}
107120
}
108121

109122
publicstaticstringVersion
@@ -297,6 +310,11 @@ public static void Shutdown()
297310
{
298311
Marshal.FreeHGlobal(_pythonHome);
299312
_pythonHome=IntPtr.Zero;
313+
Marshal.FreeHGlobal(_programName);
314+
_programName=IntPtr.Zero;
315+
Marshal.FreeHGlobal(_pythonPath);
316+
_pythonPath=IntPtr.Zero;
317+
300318
Runtime.Shutdown();
301319
initialized=false;
302320
}

‎src/runtime/runtime.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -686,9 +686,7 @@ 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();
@@ -700,15 +698,13 @@ internal static extern void Py_SetProgramName(
700698
internalstaticexternIntPtr Py_GetPath();
701699

702700
[DllImport(PythonDll)]
703-
internalstaticexternvoidPy_SetPath(
704-
[MarshalAs(UnmanagedType.LPWStr)]stringhome
705-
);
701+
internalstaticexternvoidPy_SetPath(IntPtrhome);
706702
#elifPYTHON2
707703
[DllImport(PythonDll)]
708704
internalstaticexternIntPtr Py_GetProgramName();
709705

710706
[DllImport(PythonDll)]
711-
internalstaticexternvoidPy_SetProgramName(stringname);
707+
internalstaticexternvoidPy_SetProgramName(IntPtrname);
712708

713709
[DllImport(PythonDll)]
714710
internalstaticexternIntPtr Py_GetPythonHome();
@@ -720,7 +716,7 @@ internal static extern void Py_SetPath(
720716
internalstaticexternIntPtr Py_GetPath();
721717

722718
[DllImport(PythonDll)]
723-
internalstaticexternvoidPy_SetPath(stringhome);
719+
internalstaticexternvoidPy_SetPath(IntPtrhome);
724720
#endif
725721

726722
[DllImport(PythonDll)]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp