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

Commit096f50a

Browse files
committed
Adjust code a bit and skip PythonHome tests for empty strings
1 parentcc97b8a commit096f50a

File tree

2 files changed

+41
-23
lines changed

2 files changed

+41
-23
lines changed

‎src/embed_tests/TestPythonEngineProperties.cs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ public static void GetProgramNameDefault()
9696
/// Test default behavior of PYTHONHOME. If ENVVAR is set it will
9797
/// return the same value. If not, returns EmptyString.
9898
/// </summary>
99-
/// <remarks>
100-
/// AppVeyor.yml has been update to tests with ENVVAR set.
101-
/// </remarks>
10299
[Test]
103100
publicstaticvoidGetPythonHomeDefault()
104101
{
@@ -114,22 +111,19 @@ public static void GetPythonHomeDefault()
114111
[Test]
115112
publicvoidSetPythonHome()
116113
{
117-
// We needs to ensure that engine was started and shutdown at least once before setting dummy home.
118-
// Otherwise engine will not run with dummy path with random problem.
119-
if(!PythonEngine.IsInitialized)
120-
{
121-
PythonEngine.Initialize();
122-
}
123-
114+
PythonEngine.Initialize();
115+
varpythonHomeBackup=PythonEngine.PythonHome;
124116
PythonEngine.Shutdown();
125117

126-
varpythonHomeBackup=PythonEngine.PythonHome;
118+
if(pythonHomeBackup=="")
119+
Assert.Inconclusive("Can't reset PythonHome to empty string, skipping");
127120

128121
varpythonHome="/dummypath/";
129122

130123
PythonEngine.PythonHome=pythonHome;
131124
PythonEngine.Initialize();
132125

126+
Assert.AreEqual(pythonHome,PythonEngine.PythonHome);
133127
PythonEngine.Shutdown();
134128

135129
// Restoring valid pythonhome.
@@ -139,15 +133,12 @@ public void SetPythonHome()
139133
[Test]
140134
publicvoidSetPythonHomeTwice()
141135
{
142-
// We needs to ensure that engine was started and shutdown at least once before setting dummy home.
143-
// Otherwise engine will not run with dummy path with random problem.
144-
if(!PythonEngine.IsInitialized)
145-
{
146-
PythonEngine.Initialize();
147-
}
136+
PythonEngine.Initialize();
137+
varpythonHomeBackup=PythonEngine.PythonHome;
148138
PythonEngine.Shutdown();
149139

150-
varpythonHomeBackup=PythonEngine.PythonHome;
140+
if(pythonHomeBackup=="")
141+
Assert.Inconclusive("Can't reset PythonHome to empty string, skipping");
151142

152143
varpythonHome="/dummypath/";
153144

@@ -161,6 +152,26 @@ public void SetPythonHomeTwice()
161152
PythonEngine.PythonHome=pythonHomeBackup;
162153
}
163154

155+
[Test]
156+
[Ignore("Currently buggy in Python")]
157+
publicvoidSetPythonHomeEmptyString()
158+
{
159+
PythonEngine.Initialize();
160+
161+
varbackup=PythonEngine.PythonHome;
162+
if(backup=="")
163+
{
164+
PythonEngine.Shutdown();
165+
Assert.Inconclusive("Can't reset PythonHome to empty string, skipping");
166+
}
167+
PythonEngine.PythonHome="";
168+
169+
Assert.AreEqual("",PythonEngine.PythonHome);
170+
171+
PythonEngine.PythonHome=backup;
172+
PythonEngine.Shutdown();
173+
}
174+
164175
[Test]
165176
publicvoidSetProgramName()
166177
{
@@ -207,7 +218,7 @@ public void SetPythonPath()
207218
// The list sys.path is initialized with this value on interpreter startup;
208219
// it can be (and usually is) modified later to change the search path for loading modules.
209220
// See https://docs.python.org/3/c-api/init.html#c.Py_GetPath
210-
// After PythonPath is set, then PythonEngine.PythonPath will correctly return the full search path.
221+
// After PythonPath is set, then PythonEngine.PythonPath will correctly return the full search path.
211222

212223
PythonEngine.Shutdown();
213224

‎src/runtime/PythonEngine.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ public static bool IsInitialized
4747
get{returninitialized;}
4848
}
4949

50+
privatestaticvoidEnsureInitialized()
51+
{
52+
if(!IsInitialized)
53+
thrownewInvalidOperationException(
54+
"Python must be initialized for this operation"
55+
);
56+
}
57+
5058
/// <summary>Set to <c>true</c> to enable GIL debugging assistance.</summary>
5159
publicstaticboolDebugGIL{get;set;}=false;
5260

@@ -96,17 +104,16 @@ public static string PythonHome
96104
{
97105
get
98106
{
107+
EnsureInitialized();
99108
IntPtrp=Runtime.TryUsingDll(()=>Runtime.Py_GetPythonHome());
100109
returnUcsMarshaler.PtrToPy3UnicodePy2String(p)??"";
101110
}
102111
set
103112
{
104113
// this value is null in the beginning
105114
Marshal.FreeHGlobal(_pythonHome);
106-
_pythonHome=Runtime.TryUsingDll(
107-
()=>UcsMarshaler.Py3UnicodePy2StringtoPtr(value)
108-
);
109-
Runtime.Py_SetPythonHome(_pythonHome);
115+
_pythonHome=UcsMarshaler.Py3UnicodePy2StringtoPtr(value);
116+
Runtime.TryUsingDll(()=>Runtime.Py_SetPythonHome(_pythonHome));
110117
}
111118
}
112119

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp