| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -56,8 +56,8 @@ public static void DomainReloadAndGC() | |||
| 56 | 56 | Assembly pythonRunner1 = BuildAssembly("test1"); | |
| 57 | 57 | RunAssemblyAndUnload(pythonRunner1, "test1"); | |
| 58 | 58 | ||
| 59 | - // Verify that python is not initialized even though we ran it. | ||
| 60 | - //Assert.That(Runtime.Runtime.Py_IsInitialized(), Is.Zero); | ||
| 59 | + Assert.That(Runtime.Runtime.Py_IsInitialized() != 0, | ||
| 60 | + "On soft-shutdown mode, Python runtime should still running"); | ||
| 61 | 61 | ||
| 62 | 62 | // This caused a crash because objects allocated in pythonRunner1 | |
| 63 | 63 | // still existed in memory, but the code to do python GC on those | |
@@ -83,7 +83,7 @@ public static void RunPython() { | |||
| 83 | 83 | AppDomain.CurrentDomain.DomainUnload += OnDomainUnload; | |
| 84 | 84 | string name = AppDomain.CurrentDomain.FriendlyName; | |
| 85 | 85 | Console.WriteLine(string.Format(""[{0} in .NET] In PythonRunner.RunPython"", name)); | |
| 86 | - //PythonEngine.Initialize(softShutdown: true); | ||
| 86 | + PythonEngine.Initialize(softShutdown: true); | ||
| 87 | 87 | using (Py.GIL()) { | |
| 88 | 88 | try { | |
| 89 | 89 | var pyScript = string.Format(""import clr\n"" | |
@@ -99,6 +99,7 @@ public static void RunPython() { | |||
| 99 | 99 | Console.WriteLine(string.Format(""[{0} in .NET] Caught exception: {1}"", name, e)); | |
| 100 | 100 | } | |
| 101 | 101 | } | |
| 102 | + PythonEngine.BeginAllowThreads(); | ||
| 102 | 103 | } | |
| 103 | 104 | static void OnDomainUnload(object sender, EventArgs e) { | |
| 104 | 105 | System.Console.WriteLine(string.Format(""[{0} in .NET] unloading"", AppDomain.CurrentDomain.FriendlyName)); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -132,21 +132,21 @@ internal static void Initialize() | |||
| 132 | 132 | /// </summary> | |
| 133 | 133 | internal static void Shutdown() | |
| 134 | 134 | { | |
| 135 | - if (Runtime.Py_IsInitialized() != 0) | ||
| 135 | + if (Runtime.Py_IsInitialized() == 0) | ||
| 136 | 136 | { | |
| 137 | - Type type = typeof(Exceptions); | ||
| 138 | - foreach (FieldInfo fi in type.GetFields(BindingFlags.Public | BindingFlags.Static)) | ||
| 137 | + return; | ||
| 138 | + } | ||
| 139 | + Type type = typeof(Exceptions); | ||
| 140 | + foreach (FieldInfo fi in type.GetFields(BindingFlags.Public | BindingFlags.Static)) | ||
| 141 | + { | ||
| 142 | + var op = (IntPtr)fi.GetValue(type); | ||
| 143 | + if (op != IntPtr.Zero) | ||
| 139 | 144 | { | |
| 140 | - var op = (IntPtr)fi.GetValue(type); | ||
| 141 | - if (op != IntPtr.Zero) | ||
| 142 | - { | ||
| 143 | - Runtime.XDecref(op); | ||
| 144 | - } | ||
| 145 | + Runtime.XDecref(op); | ||
| 145 | 146 | } | |
| 146 | - Runtime.XDecref(exceptions_module); | ||
| 147 | - Runtime.PyObject_HasAttrString(warnings_module, "xx"); | ||
| 148 | - Runtime.XDecref(warnings_module); | ||
| 149 | 147 | } | |
| 148 | + Runtime.Py_CLEAR(ref exceptions_module); | ||
| 149 | + Runtime.Py_CLEAR(ref warnings_module); | ||
| 150 | 150 | } | |
| 151 | 151 | ||
| 152 | 152 | /// <summary> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -324,18 +324,19 @@ public static void InitExt() | |||
| 324 | 324 | /// </remarks> | |
| 325 | 325 | public static void Shutdown() | |
| 326 | 326 | { | |
| 327 | - if (initialized) | ||
| 327 | + if (!initialized) | ||
| 328 | 328 | { | |
| 329 | - PyScopeManager.Global.Clear(); | ||
| 330 | - | ||
| 331 | - // If the shutdown handlers trigger a domain unload, | ||
| 332 | - // don't call shutdown again. | ||
| 333 | - AppDomain.CurrentDomain.DomainUnload -= OnDomainUnload; | ||
| 329 | + return; | ||
| 330 | + } | ||
| 331 | + // If the shutdown handlers trigger a domain unload, | ||
| 332 | + // don't call shutdown again. | ||
| 333 | + AppDomain.CurrentDomain.DomainUnload -= OnDomainUnload; | ||
| 334 | 334 | ||
| 335 | - ExecuteShutdownHandlers(); | ||
| 335 | + PyScopeManager.Global.Clear(); | ||
| 336 | + ExecuteShutdownHandlers(); | ||
| 336 | 337 | ||
| 337 | - initialized = false; | ||
| 338 | - } | ||
| 338 | + initialized = false; | ||
| 339 | + | ||
| 339 | 340 | } | |
| 340 | 341 | ||
| 341 | 342 | /// <summary> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -394,6 +394,8 @@ internal static void Shutdown(bool soft) | |||
| 394 | 394 | { | |
| 395 | 395 | return; | |
| 396 | 396 | } | |
| 397 | + PyGILState_Ensure(); | ||
| 398 | + | ||
| 397 | 399 | AssemblyManager.Shutdown(); | |
| 398 | 400 | Exceptions.Shutdown(); | |
| 399 | 401 | ImportHook.Shutdown(); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments