Skip to content
This repository was archived by the owner on Jul 22, 2023. It is now read-only.

Commit 3cb56f1

Browse files
committed
Avoid Domain tests influence other tests when the default is not Soft or Reload
1 parent 670bd74 commit 3cb56f1

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

src/embed_tests/TestDomainReload.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,20 @@ public static void DomainReloadAndGC()
5252
{
5353
Assert.IsFalse(PythonEngine.IsInitialized);
5454
RunAssemblyAndUnload("test1");
55-
5655
Assert.That(Runtime.Runtime.Py_IsInitialized() != 0,
5756
"On soft-shutdown mode, Python runtime should still running");
5857

5958
RunAssemblyAndUnload("test2");
59+
Assert.That(Runtime.Runtime.Py_IsInitialized() != 0,
60+
"On soft-shutdown mode, Python runtime should still running");
61+
62+
if (PythonEngine.DefaultShutdownMode == ShutdownMode.Normal)
63+
{
64+
// The default mode is a normal mode,
65+
// it should shutdown the Python VM avoiding influence other tests.
66+
Runtime.Runtime.PyGILState_Ensure();
67+
Runtime.Runtime.Py_Finalize();
68+
}
6069
}
6170

6271
[Test]
@@ -268,17 +277,12 @@ public static void ShutdownPythonCompletely()
268277
// if it used a another mode(the default mode) in other tests,
269278
// when other tests trying to access these reserved objects, it may cause Domain exception,
270279
// thus it needs to reduct to Soft mode to make sure all clr objects remove from Python.
271-
if (PythonEngine.DefaultShutdownMode != ShutdownMode.Reload)
280+
var defaultMode = PythonEngine.DefaultShutdownMode;
281+
if (defaultMode != ShutdownMode.Reload)
272282
{
273-
PythonEngine.ShutdownMode = ShutdownMode.Soft;
283+
PythonEngine.ShutdownMode = defaultMode;
274284
}
275285
PythonEngine.Shutdown();
276-
if (PythonEngine.DefaultShutdownMode == ShutdownMode.Normal)
277-
{
278-
// Normal mode will shutdown the VM, so it needs to be shutdown
279-
// for avoiding influence with other tests.
280-
Runtime.Runtime.Shutdown();
281-
}
282286
}
283287

284288
public static IntPtr GetTestObject()

src/runtime/runtime.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
using System.Collections.Generic;
77
using Python.Runtime.Platform;
88
using System.Linq;
9-
using System.IO;
10-
using System.Runtime.Serialization.Formatters.Binary;
119

1210
namespace Python.Runtime
1311
{
@@ -157,6 +155,14 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
157155
{
158156
RuntimeState.Save();
159157
}
158+
#if !NETSTANDARD
159+
// XXX: Reload mode may reduct to Soft mode,
160+
// so even on Reload mode it still needs to save the RuntimeState
161+
else if (mode == ShutdownMode.Reload)
162+
{
163+
RuntimeState.Save();
164+
}
165+
#endif
160166
MainManagedThreadId = Thread.CurrentThread.ManagedThreadId;
161167
}
162168
else

src/runtime/runtime_state.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static void Restore()
9898
private static void ResotreModules(IntPtr dummyGC)
9999
{
100100
var intialModules = PySys_GetObject("initial_modules");
101-
Debug.Assert(intialModules != null);
101+
Debug.Assert(intialModules != IntPtr.Zero);
102102
var modules = PyImport_GetModuleDict();
103103
foreach (var name in GetModuleNames())
104104
{

0 commit comments

Comments
 (0)