Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 598cb77

Browse files
committed
Disambiguate Initialization
between initializing from CPython and initializing after a domain reload Add an optional argument to signal to the initialization that it's coming from CPython
1 parent c8dee53 commit 598cb77

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/runtime/pythonengine.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ public static void Initialize()
155155
Initialize(setSysArgv: true);
156156
}
157157

158-
public static void Initialize(bool setSysArgv = true, bool initSigs = false, ShutdownMode mode = ShutdownMode.Default)
158+
public static void Initialize(bool setSysArgv = true, bool initSigs = false, ShutdownMode mode = ShutdownMode.Default, bool fromPython = false)
159159
{
160-
Initialize(Enumerable.Empty<string>(), setSysArgv: setSysArgv, initSigs: initSigs, mode);
160+
Initialize(Enumerable.Empty<string>(), setSysArgv: setSysArgv, initSigs: initSigs, mode, fromPython: fromPython);
161161
}
162162

163163
/// <summary>
@@ -170,7 +170,7 @@ public static void Initialize(bool setSysArgv = true, bool initSigs = false, Shu
170170
/// interpreter lock (GIL) to call this method.
171171
/// initSigs can be set to 1 to do default python signal configuration. This will override the way signals are handled by the application.
172172
/// </remarks>
173-
public static void Initialize(IEnumerable<string> args, bool setSysArgv = true, bool initSigs = false, ShutdownMode mode = ShutdownMode.Default)
173+
public static void Initialize(IEnumerable<string> args, bool setSysArgv = true, bool initSigs = false, ShutdownMode mode = ShutdownMode.Default, bool fromPython = false)
174174
{
175175
if (initialized)
176176
{
@@ -182,7 +182,7 @@ public static void Initialize(IEnumerable<string> args, bool setSysArgv = true,
182182
// during an initial "import clr", and the world ends shortly thereafter.
183183
// This is probably masking some bad mojo happening somewhere in Runtime.Initialize().
184184
delegateManager = new DelegateManager();
185-
Runtime.Initialize(initSigs, mode);
185+
Runtime.Initialize(initSigs, mode, fromPython);
186186
initialized = true;
187187
Exceptions.Clear();
188188

@@ -265,7 +265,7 @@ public static IntPtr InitExt()
265265
{
266266
try
267267
{
268-
Initialize(setSysArgv: false);
268+
Initialize(setSysArgv: false, fromPython: true);
269269

270270
// Trickery - when the import hook is installed into an already
271271
// running Python, the standard import machinery is still in

src/runtime/runtime.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ internal static Version PyVersion
129129
/// </summary>
130130
/// <remarks>When calling this method after a soft shutdown or a domain reload,
131131
/// this method acquires and releases the GIL. </remarks>
132-
internal static void Initialize(bool initSigs = false, ShutdownMode mode = ShutdownMode.Default)
132+
internal static void Initialize(bool initSigs = false, ShutdownMode mode = ShutdownMode.Default, bool fromPython = false)
133133
{
134134
if (_isInitialized)
135135
{
@@ -163,16 +163,15 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
163163
RuntimeState.Save();
164164
}
165165
#endif
166-
MainManagedThreadId = Thread.CurrentThread.ManagedThreadId;
167166
}
168-
else
167+
else if (!fromPython)
169168
{
170169
// If we're coming back from a domain reload or a soft shutdown,
171170
// we have previously released the thread state. Restore the main
172171
// thread state here.
173172
PyEval_RestoreThread(PyGILState_GetThisThreadState());
174-
MainManagedThreadId = Thread.CurrentThread.ManagedThreadId;
175173
}
174+
MainManagedThreadId = Thread.CurrentThread.ManagedThreadId;
176175

177176
IsFinalizing = false;
178177

0 commit comments

Comments
 (0)