| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ba5127a commit c1190f4
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,6 +23,16 @@ internal static void InitializeModuleDef() | |||
| 23 | 23 | module_def = ModuleDefOffset.AllocModuleDef("clr"); | |
| 24 | 24 | } | |
| 25 | 25 | } | |
| 26 | + | ||
| 27 | + internal static void ReleaseModuleDef() | ||
| 28 | + { | ||
| 29 | + if (module_def == IntPtr.Zero) | ||
| 30 | + { | ||
| 31 | + return; | ||
| 32 | + } | ||
| 33 | + ModuleDefOffset.FreeModuleDef(module_def); | ||
| 34 | + module_def = IntPtr.Zero; | ||
| 35 | + } | ||
| 26 | 36 | #endif | |
| 27 | 37 | ||
| 28 | 38 | /// <summary> | |
@@ -56,9 +66,12 @@ static void InitImport() | |||
| 56 | 66 | // Python __import__. | |
| 57 | 67 | IntPtr builtins = GetNewRefToBuiltins(); | |
| 58 | 68 | py_import = Runtime.PyObject_GetAttrString(builtins, "__import__"); | |
| 69 | + PythonException.ThrowIfIsNull(py_import); | ||
| 70 | + | ||
| 59 | 71 | hook = new MethodWrapper(typeof(ImportHook), "__import__", "TernaryFunc"); | |
| 60 | - Runtime.PyObject_SetAttrString(builtins, "__import__", hook.ptr); | ||
| 61 | - Runtime.XDecref(hook.ptr); | ||
| 72 | + int res = Runtime.PyObject_SetAttrString(builtins, "__import__", hook.ptr); | ||
| 73 | + PythonException.ThrowIfIsNotZero(res); | ||
| 74 | + | ||
| 62 | 75 | Runtime.XDecref(builtins); | |
| 63 | 76 | } | |
| 64 | 77 | ||
@@ -69,10 +82,14 @@ static void RestoreImport() | |||
| 69 | 82 | { | |
| 70 | 83 | IntPtr builtins = GetNewRefToBuiltins(); | |
| 71 | 84 | ||
| 72 | - Runtime.PyObject_SetAttrString(builtins, "__import__", py_import); | ||
| 85 | + int res = Runtime.PyObject_SetAttrString(builtins, "__import__", py_import); | ||
| 86 | + PythonException.ThrowIfIsNotZero(res); | ||
| 73 | 87 | Runtime.XDecref(py_import); | |
| 74 | 88 | py_import = IntPtr.Zero; | |
| 75 | 89 | ||
| 90 | + hook.Release(); | ||
| 91 | + hook = null; | ||
| 92 | + | ||
| 76 | 93 | Runtime.XDecref(builtins); | |
| 77 | 94 | } | |
| 78 | 95 | ||
@@ -112,13 +129,26 @@ internal static void Initialize() | |||
| 112 | 129 | /// </summary> | |
| 113 | 130 | internal static void Shutdown() | |
| 114 | 131 | { | |
| 115 | - if (Runtime.Py_IsInitialized() != 0) | ||
| 132 | + if (Runtime.Py_IsInitialized() == 0) | ||
| 116 | 133 | { | |
| 117 | - RestoreImport(); | ||
| 134 | + return; | ||
| 135 | + } | ||
| 118 | 136 | ||
| 119 | - Runtime.XDecref(py_clr_module); | ||
| 120 | - Runtime.XDecref(root.pyHandle); | ||
| 137 | + RestoreImport(); | ||
| 138 | + | ||
| 139 | + bool shouldFreeDef = Runtime.Refcount(py_clr_module) == 1; | ||
| 140 | + Runtime.XDecref(py_clr_module); | ||
| 141 | + py_clr_module = IntPtr.Zero; | ||
| 142 | + #if PYTHON3 | ||
| 143 | + if (shouldFreeDef) | ||
| 144 | + { | ||
| 145 | + ReleaseModuleDef(); | ||
| 121 | 146 | } | |
| 147 | + #endif | ||
| 148 | + | ||
| 149 | + Runtime.XDecref(root.pyHandle); | ||
| 150 | + root = null; | ||
| 151 | + CLRModule.Reset(); | ||
| 122 | 152 | } | |
| 123 | 153 | ||
| 124 | 154 | /// <summary> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -227,7 +227,7 @@ public static IntPtr AllocModuleDef(string modulename) | |||
| 227 | 227 | byte[] ascii = Encoding.ASCII.GetBytes(modulename); | |
| 228 | 228 | int size = name + ascii.Length + 1; | |
| 229 | 229 | IntPtr ptr = Marshal.AllocHGlobal(size); | |
| 230 | - for (int i = 0; i < m_free; i += IntPtr.Size) | ||
| 230 | + for (int i = 0; i <= m_free; i += IntPtr.Size) | ||
| 231 | 231 | Marshal.WriteIntPtr(ptr, i, IntPtr.Zero); | |
| 232 | 232 | Marshal.Copy(ascii, 0, (IntPtr)(ptr + name), ascii.Length); | |
| 233 | 233 | Marshal.WriteIntPtr(ptr, m_name, (IntPtr)(ptr + name)); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,6 +12,7 @@ internal class MethodWrapper | |||
| 12 | 12 | { | |
| 13 | 13 | public IntPtr mdef; | |
| 14 | 14 | public IntPtr ptr; | |
| 15 | + private bool _disposed = false; | ||
| 15 | 16 | ||
| 16 | 17 | public MethodWrapper(Type type, string name, string funcType = null) | |
| 17 | 18 | { | |
@@ -31,5 +32,21 @@ public IntPtr Call(IntPtr args, IntPtr kw) | |||
| 31 | 32 | { | |
| 32 | 33 | return Runtime.PyCFunction_Call(ptr, args, kw); | |
| 33 | 34 | } | |
| 35 | + | ||
| 36 | + public void Release() | ||
| 37 | + { | ||
| 38 | + if (_disposed) | ||
| 39 | + { | ||
| 40 | + return; | ||
| 41 | + } | ||
| 42 | + _disposed = true; | ||
| 43 | + bool freeDef = Runtime.Refcount(ptr) == 1; | ||
| 44 | + Runtime.XDecref(ptr); | ||
| 45 | + if (freeDef && mdef != IntPtr.Zero) | ||
| 46 | + { | ||
| 47 | + Runtime.PyMem_Free(mdef); | ||
| 48 | + mdef = IntPtr.Zero; | ||
| 49 | + } | ||
| 50 | + } | ||
| 34 | 51 | } | |
| 35 | 52 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -316,6 +316,11 @@ internal class CLRModule : ModuleObject | |||
| 316 | 316 | internal static bool _SuppressDocs = false; | |
| 317 | 317 | internal static bool _SuppressOverloads = false; | |
| 318 | 318 | ||
| 319 | + static CLRModule() | ||
| 320 | + { | ||
| 321 | + Reset(); | ||
| 322 | + } | ||
| 323 | + | ||
| 319 | 324 | public CLRModule() : base("clr") | |
| 320 | 325 | { | |
| 321 | 326 | _namespace = string.Empty; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -187,7 +187,6 @@ internal static void Initialize(bool initSigs = false) | |||
| 187 | 187 | ||
| 188 | 188 | IsFinalizing = false; | |
| 189 | 189 | ||
| 190 | - CLRModule.Reset(); | ||
| 191 | 190 | GenericUtil.Reset(); | |
| 192 | 191 | PyScopeManager.Reset(); | |
| 193 | 192 | ClassManager.Reset(); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments