Skip to content

Commit 9874cd1

Browse files
committed
Add ShutdownMode.Default refer to normal mode if no EnvironmentVariable control exists
1 parent 80d4fa0 commit 9874cd1

4 files changed

Lines changed: 35 additions & 16 deletions

File tree

src/embed_tests/DomainCode.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,23 @@ from Python.EmbeddingTest.Domain import MyClass
114114

115115
public static void RunTestObject(IntPtr handle)
116116
{
117-
using (Py.GIL())
117+
try
118118
{
119-
120-
using (PyObject obj = new PyObject(handle))
119+
using (Py.GIL())
121120
{
122-
obj.InvokeMethod("Method");
123-
obj.InvokeMethod("StaticMethod");
121+
122+
using (PyObject obj = new PyObject(handle))
123+
{
124+
obj.InvokeMethod("Method");
125+
obj.InvokeMethod("StaticMethod");
126+
}
124127
}
125128
}
129+
catch (Exception e)
130+
{
131+
Debug.WriteLine(e);
132+
throw;
133+
}
126134
}
127135

128136
public static void ReleaseTestObject(IntPtr handle)

src/embed_tests/TestRuntime.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public static void PlatformCache()
3939
[Test]
4040
public static void Py_IsInitializedValue()
4141
{
42+
if (Runtime.Runtime.Py_IsInitialized() == 1)
43+
{
44+
Runtime.Runtime.PyGILState_Ensure();
45+
}
4246
Runtime.Runtime.Py_Finalize();
4347
Assert.AreEqual(0, Runtime.Runtime.Py_IsInitialized());
4448
Runtime.Runtime.Py_Initialize();

src/runtime/pythonengine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public static void Initialize(bool setSysArgv = true, bool initSigs = false, Shu
172172
/// interpreter lock (GIL) to call this method.
173173
/// initSigs can be set to 1 to do default python signal configuration. This will override the way signals are handled by the application.
174174
/// </remarks>
175-
public static void Initialize(IEnumerable<string> args, bool setSysArgv = true, bool initSigs = false, ShutdownMode mode = ShutdownMode.Normal)
175+
public static void Initialize(IEnumerable<string> args, bool setSysArgv = true, bool initSigs = false, ShutdownMode mode = ShutdownMode.Default)
176176
{
177177
if (initialized)
178178
{

src/runtime/runtime.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,25 +132,31 @@ public class Runtime
132132
/// <summary>
133133
/// Initialize the runtime...
134134
/// </summary>
135-
internal static void Initialize(bool initSigs = false, ShutdownMode mode = ShutdownMode.Normal)
135+
internal static void Initialize(bool initSigs = false, ShutdownMode mode = ShutdownMode.Default)
136136
{
137137
if (_isInitialized)
138138
{
139139
return;
140140
}
141141
_isInitialized = true;
142142

143-
if (Environment.GetEnvironmentVariable("PYTHONNET_SOFT_SHUTDOWN") == "1")
143+
if (mode == ShutdownMode.Default)
144144
{
145-
mode = ShutdownMode.Soft;
146-
}
147-
#if !NETSTANDARD
148-
else if (Environment.GetEnvironmentVariable("PYTHONNET_RELOAD_SHUTDOWN") == "1")
149-
{
150-
mode = ShutdownMode.Reload;
151-
}
145+
if (Environment.GetEnvironmentVariable("PYTHONNET_SOFT_SHUTDOWN") == "1")
146+
{
147+
mode = ShutdownMode.Soft;
148+
}
149+
#if !NETSTANDARD
150+
else if (Environment.GetEnvironmentVariable("PYTHONNET_RELOAD_SHUTDOWN") == "1")
151+
{
152+
mode = ShutdownMode.Reload;
153+
}
152154
#endif
153-
//mode = ShutdownMode.Reload;
155+
else
156+
{
157+
mode = ShutdownMode.Normal;
158+
}
159+
}
154160

155161
ShutdownMode = mode;
156162
if (Py_IsInitialized() == 0)
@@ -2196,6 +2202,7 @@ internal static IntPtr GetBuiltins()
21962202

21972203
public enum ShutdownMode
21982204
{
2205+
Default,
21992206
Normal,
22002207
Soft,
22012208
#if !NETSTANDARD

0 commit comments

Comments
 (0)