8000 Adjust code a bit and skip PythonHome tests for empty strings · pythonnet/pythonnet@096f50a · GitHub
[go: up one dir, main page]

Skip to content

Commit 096f50a

Browse files
committed
Adjust code a bit and skip PythonHome tests for empty strings
1 parent cc97b8a commit 096f50a

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
public static void GetPythonHomeDefault()
104101
{
@@ -114,22 +111,19 @@ public static void GetPythonHomeDefault()
114111
[Test]
115112
public void SetPythonHome()
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+
var pythonHomeBackup = PythonEngine.PythonHome;
124116
PythonEngine.Shutdown();
125117

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

128121
var pythonHome = "/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
public void SetPythonHomeTwice()
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+
var pythonHomeBackup = PythonEngine.PythonHome;
148138
PythonEngine.Shutdown();
149139

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

152143
var pythonHome = "/dummypath/";
153144

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

155+
[Test]
156+
[Ignore("Currently buggy in Python")]
157+
public void SetPythonHomeEmptyString()
158+
{
159+
PythonEngine.Initialize();
160+
161+
var backup = 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
public void SetProgramName()
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 { return initialized; }
4848
}
4949

50+
private static void EnsureInitialized()
51+
{
52+
if (!IsInitialized)
53+
throw new InvalidOperationException(
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
public static bool DebugGIL { get; set; } = false;
5260

@@ -96,17 +104,16 @@ public static string PythonHome
96104
{
97105
get
98106
{
107+
EnsureInitialized();
99108
IntPtr p = Runtime.TryUsingDll(() => Runtime.Py_GetPythonHome());
100109
return UcsMarshaler.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)
0