🌐 AI搜索 & 代理 主页
Skip to content

Commit 2f24a35

Browse files
committed
Move tests that require reinit and only run on .NET Framework
1 parent a38eddc commit 2f24a35

File tree

7 files changed

+177
-145
lines changed

7 files changed

+177
-145
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Python.Runtime;
2+
using NUnit.Framework;
3+
4+
namespace Python.EmbeddingTest.NeedsReinit;
5+
6+
public class StopAndRestartEngine
7+
{
8+
bool WasInitialized = false;
9+
10+
[OneTimeSetUp]
11+
public void Setup()
12+
{
13+
WasInitialized = PythonEngine.IsInitialized;
14+
if (WasInitialized)
15+
{
16+
PythonEngine.Shutdown();
17+
}
18+
}
19+
20+
[OneTimeTearDown]
21+
public void Teardown()
22+
{
23+
if (WasInitialized && !PythonEngine.IsInitialized)
24+
{
25+
PythonEngine.Initialize();
26+
}
27+
}
28+
}

src/embed_tests/TestDomainReload.cs renamed to src/embed_tests/NeedsReinit/TestDomainReload.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
// Unfortunately this means no continuous integration testing for this case.
1616
//
1717
#if NETFRAMEWORK
18-
namespace Python.EmbeddingTest
18+
namespace Python.EmbeddingTest.NeedsReinit
1919
{
20-
class TestDomainReload
20+
[Category("NeedsReinit")]
21+
class TestDomainReload : StopAndRestartEngine
2122
{
23+
24+
2225
abstract class CrossCaller : MarshalByRefObject
2326
{
2427
public abstract ValueType Execute(ValueType arg);

src/embed_tests/pyinitialize.cs renamed to src/embed_tests/NeedsReinit/TestPyInitialize.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
using NUnit.Framework;
33
using Python.Runtime;
44

5-
namespace Python.EmbeddingTest
5+
namespace Python.EmbeddingTest.NeedsReinit
66
{
7-
[Ignore("Only works if we can re-initialize the Python engine")]
8-
public class PyInitializeTest
7+
[Category("NeedsReinit")]
8+
public class TestPyInitialize : StopAndRestartEngine
99
{
1010
/// <summary>
1111
/// Tests issue with multiple simple Initialize/Shutdowns.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
using System;
2+
using NUnit.Framework;
3+
using Python.Runtime;
4+
5+
namespace Python.EmbeddingTest.NeedsReinit
6+
{
7+
[Category("NeedsReinit")]
8+
public class TestPythonEngineProperties : StopAndRestartEngine
9+
{
10+
[Test]
11+
public void SetPythonHome()
12+
{
13+
PythonEngine.Initialize();
14+
var pythonHomeBackup = PythonEngine.PythonHome;
15+
PythonEngine.Shutdown();
16+
17+
if (pythonHomeBackup == "")
18+
Assert.Inconclusive("Can't reset PythonHome to empty string, skipping");
19+
20+
var pythonHome = "/dummypath/";
21+
22+
PythonEngine.PythonHome = pythonHome;
23+
PythonEngine.Initialize();
24+
25+
Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
26+
PythonEngine.Shutdown();
27+
28+
// Restoring valid pythonhome.
29+
PythonEngine.PythonHome = pythonHomeBackup;
30+
}
31+
32+
[Test]
33+
public void SetPythonHomeTwice()
34+
{
35+
PythonEngine.Initialize();
36+
var pythonHomeBackup = PythonEngine.PythonHome;
37+
PythonEngine.Shutdown();
38+
39+
if (pythonHomeBackup == "")
40+
Assert.Inconclusive("Can't reset PythonHome to empty string, skipping");
41+
42+
var pythonHome = "/dummypath/";
43+
44+
PythonEngine.PythonHome = "/dummypath2/";
45+
PythonEngine.PythonHome = pythonHome;
46+
PythonEngine.Initialize();
47+
48+
Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
49+
PythonEngine.Shutdown();
50+
51+
PythonEngine.PythonHome = pythonHomeBackup;
52+
}
53+
54+
[Test]
55+
[Ignore("Currently buggy in Python")]
56+
public void SetPythonHomeEmptyString()
57+
{
58+
PythonEngine.Initialize();
59+
60+
var backup = PythonEngine.PythonHome;
61+
if (backup == "")
62+
{
63+
PythonEngine.Shutdown();
64+
Assert.Inconclusive("Can't reset PythonHome to empty string, skipping");
65+
}
66+
PythonEngine.PythonHome = "";
67+
68+
Assert.AreEqual("", PythonEngine.PythonHome);
69+
70+
PythonEngine.PythonHome = backup;
71+
PythonEngine.Shutdown();
72+
}
73+
74+
[Test]
75+
public void SetProgramName()
76+
{
77+
if (PythonEngine.IsInitialized)
78+
{
79+
PythonEngine.Shutdown();
80+
}
81+
82+
var programNameBackup = PythonEngine.ProgramName;
83+
84+
var programName = "FooBar";
85+
86+
PythonEngine.ProgramName = programName;
87+
PythonEngine.Initialize();
88+
89+
Assert.AreEqual(programName, PythonEngine.ProgramName);
90+
PythonEngine.Shutdown();
91+
92+
PythonEngine.ProgramName = programNameBackup;
93+
}
94+
95+
[Test]
96+
public void SetPythonPath()
97+
{
98+
PythonEngine.Initialize();
99+
100+
const string moduleName = "pytest";
101+
bool importShouldSucceed;
102+
try
103+
{
104+
Py.Import(moduleName);
105+
importShouldSucceed = true;
106+
}
107+
catch
108+
{
109+
importShouldSucceed = false;
110+
}
111+
112+
string[] paths = Py.Import("sys").GetAttr("path").As<string[]>();
113+
string path = string.Join(System.IO.Path.PathSeparator.ToString(), paths);
114+
115+
// path should not be set to PythonEngine.PythonPath here.
116+
// PythonEngine.PythonPath gets the default module search path, not the full search path.
117+
// The list sys.path is initialized with this value on interpreter startup;
118+
// it can be (and usually is) modified later to change the search path for loading modules.
119+
// See https://docs.python.org/3/c-api/init.html#c.Py_GetPath
120+
// After PythonPath is set, then PythonEngine.PythonPath will correctly return the full search path.
121+
122+
PythonEngine.Shutdown();
123+
124+
PythonEngine.PythonPath = path;
125+
PythonEngine.Initialize();
126+
127+
Assert.AreEqual(path, PythonEngine.PythonPath);
128+
if (importShouldSucceed) Py.Import(moduleName);
129+
130+
PythonEngine.Shutdown();
131+
}
132+
}
133+
}

src/embed_tests/TestRuntime.cs renamed to src/embed_tests/NeedsReinit/TestRuntime.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,11 @@
33
using NUnit.Framework;
44
using Python.Runtime;
55

6-
namespace Python.EmbeddingTest
6+
namespace Python.EmbeddingTest.NeedsReinit
77
{
8-
[Ignore("Only works if we can shutdown and re-initialize the Python runtime")]
9-
public class TestRuntime
8+
[Ignore("Tests for low-level Runtime functions, crashing currently")]
9+
public class TestRuntime : StopAndRestartEngine
1010
{
11-
[OneTimeSetUp]
12-
public void SetUp()
13-
{
14-
// We needs to ensure that no any engines are running.
15-
if (PythonEngine.IsInitialized)
16-
{
17-
PythonEngine.Shutdown();
18-
}
19-
}
20-
2111
[Test]
2212
public static void Py_IsInitializedValue()
2313
{

src/embed_tests/Python.EmbeddingTest.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
<None Include="fixtures/**/*.py" CopyToOutputDirectory="PreserveNewest" />
1515
</ItemGroup>
1616

17+
<!-- Drop Reinit test source files for net8.0 and above -->
18+
<ItemGroup Condition="'$(TargetFramework)' != 'net472'">
19+
<Compile Remove="NeedsReinit/*.cs" />
20+
</ItemGroup>
21+
1722
<PropertyGroup>
1823
<DefineConstants>$(DefineConstants);$(ConfiguredConstants)</DefineConstants>
1924
</PropertyGroup>

src/embed_tests/TestPythonEngineProperties.cs

Lines changed: 0 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -96,132 +96,5 @@ public static void GetPythonHomeDefault()
9696

9797
Assert.AreEqual(envPythonHome, enginePythonHome);
9898
}
99-
100-
[Ignore("Only works if we can shutdown and re-init the interpreter")]
101-
[Test]
102-
public void SetPythonHome()
103-
{
104-
PythonEngine.Initialize();
105-
var pythonHomeBackup = PythonEngine.PythonHome;
106-
PythonEngine.Shutdown();
107-
108-
if (pythonHomeBackup == "")
109-
Assert.Inconclusive("Can't reset PythonHome to empty string, skipping");
110-
111-
var pythonHome = "/dummypath/";
112-
113-
PythonEngine.PythonHome = pythonHome;
114-
PythonEngine.Initialize();
115-
116-
Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
117-
PythonEngine.Shutdown();
118-
119-
// Restoring valid pythonhome.
120-
PythonEngine.PythonHome = pythonHomeBackup;
121-
}
122-
123-
[Ignore("Only works if we can shutdown and re-init the interpreter")]
124-
[Test]
125-
public void SetPythonHomeTwice()
126-
{
127-
PythonEngine.Initialize();
128-
var pythonHomeBackup = PythonEngine.PythonHome;
129-
PythonEngine.Shutdown();
130-
131-
if (pythonHomeBackup == "")
132-
Assert.Inconclusive("Can't reset PythonHome to empty string, skipping");
133-
134-
var pythonHome = "/dummypath/";
135-
136-
PythonEngine.PythonHome = "/dummypath2/";
137-
PythonEngine.PythonHome = pythonHome;
138-
PythonEngine.Initialize();
139-
140-
Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
141-
PythonEngine.Shutdown();
142-
143-
PythonEngine.PythonHome = pythonHomeBackup;
144-
}
145-
146-
[Test]
147-
[Ignore("Currently buggy in Python")]
148-
public void SetPythonHomeEmptyString()
149-
{
150-
PythonEngine.Initialize();
151-
152-
var backup = PythonEngine.PythonHome;
153-
if (backup == "")
154-
{
155-
PythonEngine.Shutdown();
156-
Assert.Inconclusive("Can't reset PythonHome to empty string, skipping");
157-
}
158-
PythonEngine.PythonHome = "";
159-
160-
Assert.AreEqual("", PythonEngine.PythonHome);
161-
162-
PythonEngine.PythonHome = backup;
163-
PythonEngine.Shutdown();
164-
}
165-
166-
[Ignore("Only works if we can shutdown and re-init the interpreter")]
167-
[Test]
168-
public void SetProgramName()
169-
{
170-
if (PythonEngine.IsInitialized)
171-
{
172-
PythonEngine.Shutdown();
173-
}
174-
175-
var programNameBackup = PythonEngine.ProgramName;
176-
177-
var programName = "FooBar";
178-
179-
PythonEngine.ProgramName = programName;
180-
PythonEngine.Initialize();
181-
182-
Assert.AreEqual(programName, PythonEngine.ProgramName);
183-
PythonEngine.Shutdown();
184-
185-
PythonEngine.ProgramName = programNameBackup;
186-
}
187-
188-
[Ignore("Only works if we can shutdown and re-init the interpreter")]
189-
[Test]
190-
public void SetPythonPath()
191-
{
192-
PythonEngine.Initialize();
193-
194-
const string moduleName = "pytest";
195-
bool importShouldSucceed;
196-
try
197-
{
198-
Py.Import(moduleName);
199-
importShouldSucceed = true;
200-
}
201-
catch
202-
{
203-
importShouldSucceed = false;
204-
}
205-
206-
string[] paths = Py.Import("sys").GetAttr("path").As<string[]>();
207-
string path = string.Join(System.IO.Path.PathSeparator.ToString(), paths);
208-
209-
// path should not be set to PythonEngine.PythonPath here.
210-
// PythonEngine.PythonPath gets the default module search path, not the full search path.
211-
// The list sys.path is initialized with this value on interpreter startup;
212-
// it can be (and usually is) modified later to change the search path for loading modules.
213-
// See https://docs.python.org/3/c-api/init.html#c.Py_GetPath
214-
// After PythonPath is set, then PythonEngine.PythonPath will correctly return the full search path.
215-
216-
PythonEngine.Shutdown();
217-
218-
PythonEngine.PythonPath = path;
219-
PythonEngine.Initialize();
220-
221-
Assert.AreEqual(path, PythonEngine.PythonPath);
222-
if (importShouldSucceed) Py.Import(moduleName);
223-
224-
PythonEngine.Shutdown();
225-
}
22699
}
227100
}

0 commit comments

Comments
 (0)