| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9912712 commit af6d37f
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,7 +16,13 @@ internal class ImportHook | |||
| 16 | 16 | ||
| 17 | 17 | #if PYTHON3 | |
| 18 | 18 | static IntPtr py_clr_module; | |
| 19 | - static IntPtr module_def; | ||
| 19 | + static IntPtr module_def = IntPtr.Zero; | ||
| 20 | + | ||
| 21 | + internal static void InitializeModuleDef() | ||
| 22 | + { | ||
| 23 | + if (module_def == IntPtr.Zero) | ||
| 24 | + module_def = ModuleDefOffset.AllocModuleDef("clr"); | ||
| 25 | + } | ||
| 20 | 26 | #endif | |
| 21 | 27 | ||
| 22 | 28 | //=================================================================== | |
@@ -44,8 +50,8 @@ internal static void Initialize() | |||
| 44 | 50 | root = new CLRModule(); | |
| 45 | 51 | ||
| 46 | 52 | #if PYTHON3 | |
| 47 | - // create a python module with the same methods as the clr module-like object | ||
| 48 | - module_def = ModuleDefOffset.AllocModuleDef("clr"); | ||
| 53 | + // create a python module with the same methods as the clr module-like object | ||
| 54 | + InitializeModuleDef(); | ||
| 49 | 55 | py_clr_module = Runtime.PyModule_Create2(module_def, 3); | |
| 50 | 56 | ||
| 51 | 57 | // both dicts are borrowed references | |
@@ -70,21 +76,13 @@ internal static void Initialize() | |||
| 70 | 76 | ||
| 71 | 77 | internal static void Shutdown() | |
| 72 | 78 | { | |
| 73 | - #if PYTHON3 | ||
| 74 | 79 | if (0 != Runtime.Py_IsInitialized()) { | |
| 80 | + #if PYTHON3 | ||
| 75 | 81 | Runtime.XDecref(py_clr_module); | |
| 76 | - Runtime.XDecref(root.pyHandle); | ||
| 77 | - } | ||
| 78 | - ModuleDefOffset.FreeModuleDef(module_def); | ||
| 79 | 82 | #elif PYTHON2 | |
| 80 | - if (0 != Runtime.Py_IsInitialized()) | ||
| 81 | - { | ||
| 82 | - Runtime.XDecref(root.pyHandle); | ||
| 83 | 83 | Runtime.XDecref(root.pyHandle); | |
| 84 | - } | ||
| 85 | 84 | #endif | |
| 86 | - if (0 != Runtime.Py_IsInitialized()) | ||
| 87 | - { | ||
| 85 | + Runtime.XDecref(root.pyHandle); | ||
| 88 | 86 | Runtime.XDecref(py_import); | |
| 89 | 87 | } | |
| 90 | 88 | } | |
| 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)); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments