File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -312,6 +312,34 @@ private static IntPtr Get_PyObject_NextNotImplemented()
312312 return iternext ;
313313 }
314314
315+ /// <summary>
316+ /// Tries to downgrade the shutdown mode, if possible.
317+ /// The only possibles downgrades are:
318+ /// Soft -> Normal
319+ /// Reload -> Soft
320+ /// Reload -> Normal
321+ /// </summary>
322+ /// <param name="mode">The desired shutdown mode</param>
323+ /// <returns>The `mode` parameter if the downgrade is supported, the ShutdownMode
324+ /// set at initialization otherwise.</returns>
325+ static ShutdownMode TryDowngradeShutdown ( ShutdownMode mode )
326+ {
327+ if (
328+ mode == Runtime . ShutdownMode
329+ || mode == ShutdownMode . Normal
330+ #if ! NETSTANDARD
331+ || ( mode == ShutdownMode . Soft && Runtime . ShutdownMode == ShutdownMode . Reload )
332+ #endif
333+ )
334+ {
335+ return mode ;
336+ }
337+ else // we can't downgrade
338+ {
339+ return Runtime . ShutdownMode ;
340+ }
341+ }
342+
315343 internal static void Shutdown ( ShutdownMode mode )
316344 {
317345 if ( Py_IsInitialized ( ) == 0 || ! _isInitialized )
@@ -320,6 +348,11 @@ internal static void Shutdown(ShutdownMode mode)
320348 }
321349 _isInitialized = false ;
322350
351+ // If the shutdown mode specified is not the the same as the one specified
352+ // during Initialization, we need to validate it; we can only downgrade,
353+ // not upgrade the shutdown mode.
354+ mode = TryDowngradeShutdown ( mode ) ;
355+
323356 var state = PyGILState_Ensure ( ) ;
324357
325358 if ( mode == ShutdownMode . Soft )
You can’t perform that action at this time.
0 commit comments