| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -290,7 +290,10 @@ internal static void ScanAssembly(Assembly assembly) | |||
| 290 | 290 | for (var n = 0; n < names.Length; n++) | |
| 291 | 291 | { | |
| 292 | 292 | s = n == 0 ? names[0] : s + "." + names[n]; | |
| 293 | - namespaces.TryAdd(s, new ConcurrentDictionary<Assembly, string>()); | ||
| 293 | + if (namespaces.TryAdd(s, new ConcurrentDictionary<Assembly, string>())) | ||
| 294 | + { | ||
| 295 | + ImportHook.AddNamespace(s); | ||
| 296 | + } | ||
| 294 | 297 | } | |
| 295 | 298 | } | |
| 296 | 299 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,5 @@ | |||
| 1 | 1 | using System; | |
| 2 | - using System.Collections.Generic; | ||
| 3 | - using System.Runtime.InteropServices; | ||
| 2 | + using System.Collections.Concurrent; | ||
| 4 | 3 | ||
| 5 | 4 | namespace Python.Runtime | |
| 6 | 5 | { | |
@@ -37,6 +36,9 @@ def find_spec(klass, fullname, paths=None, target=None): | |||
| 37 | 36 | if 'clr' not in sys.modules: | |
| 38 | 37 | return None | |
| 39 | 38 | clr = sys.modules['clr'] | |
| 39 | + | ||
| 40 | + clr._add_pending_namespaces() | ||
| 41 | + | ||
| 40 | 42 | if clr._available_namespaces and fullname in clr._available_namespaces: | |
| 41 | 43 | return importlib.machinery.ModuleSpec(fullname, DotNetLoader(), is_package=True) | |
| 42 | 44 | return None | |
@@ -169,12 +171,26 @@ static void TeardownNameSpaceTracking() | |||
| 169 | 171 | Runtime.PyDict_SetItemString(root.dict, availableNsKey, Runtime.PyNone); | |
| 170 | 172 | } | |
| 171 | 173 | ||
| 172 | - public static void AddNamespace(string name) | ||
| 174 | + static readonly ConcurrentQueue<string> addPending = new(); | ||
| 175 | + public static void AddNamespace(string name) => addPending.Enqueue(name); | ||
| 176 | + | ||
| 177 | + internal static int AddPendingNamespaces() | ||
| 178 | + { | ||
| 179 | + int added = 0; | ||
| 180 | + while (addPending.TryDequeue(out string ns)) | ||
| 181 | + { | ||
| 182 | + AddNamespaceWithGIL(ns); | ||
| 183 | + added++; | ||
| 184 | + } | ||
| 185 | + return added; | ||
| 186 | + } | ||
| 187 | + | ||
| 188 | + internal static void AddNamespaceWithGIL(string name) | ||
| 173 | 189 | { | |
| 174 | 190 | var pyNs = Runtime.PyString_FromString(name); | |
| 175 | 191 | try | |
| 176 | 192 | { | |
| 177 | - var nsSet = Runtime.PyDict_GetItemString(new BorrowedReference(root.dict), availableNsKey); | ||
| 193 | + var nsSet = Runtime.PyDict_GetItemString(root.DictRef, availableNsKey); | ||
| 178 | 194 | if (!(nsSet.IsNull || nsSet.DangerousGetAddress() == Runtime.PyNone)) | |
| 179 | 195 | { | |
| 180 | 196 | if (Runtime.PySet_Add(nsSet, new BorrowedReference(pyNs)) != 0) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -535,8 +535,9 @@ public static Assembly AddReference(string name) | |||
| 535 | 535 | // method because it may be called from other threads, leading to deadlocks | |
| 536 | 536 | // if it is called while Python code is executing. | |
| 537 | 537 | var currNs = AssemblyManager.GetNamespaces().Except(origNs); | |
| 538 | - foreach(var ns in currNs){ | ||
| 539 | - ImportHook.AddNamespace(ns); | ||
| 538 | + foreach(var ns in currNs) | ||
| 539 | + { | ||
| 540 | + ImportHook.AddNamespaceWithGIL(ns); | ||
| 540 | 541 | } | |
| 541 | 542 | return assembly; | |
| 542 | 543 | } | |
@@ -602,5 +603,9 @@ public static ModuleObject _load_clr_module(PyObject spec) | |||
| 602 | 603 | mod = ImportHook.Import(modname.ToString()); | |
| 603 | 604 | return mod; | |
| 604 | 605 | } | |
| 606 | + | ||
| 607 | + [ModuleFunction] | ||
| 608 | + [ForbidPythonThreads] | ||
| 609 | + public static int _add_pending_namespaces() => ImportHook.AddPendingNamespaces(); | ||
| 605 | 610 | } | |
| 606 | 611 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -161,7 +161,8 @@ static void ValidateRequiredOffsetsPresent(PropertyInfo[] offsetProperties) | |||
| 161 | 161 | "Initialize", | |
| 162 | 162 | "InitializeSlots", | |
| 163 | 163 | "ListAssemblies", | |
| 164 | - "_load_clr_module", | ||
| 164 | + nameof(CLRModule._load_clr_module), | ||
| 165 | + nameof(CLRModule._add_pending_namespaces), | ||
| 165 | 166 | "Release", | |
| 166 | 167 | "Reset", | |
| 167 | 168 | "set_SuppressDocs", | |
| Back | FazBrowse Home | New Git URL |
0 commit comments