8000 fixed ImportHook not seeing namespaces in assemblies loaded via direc… · pythonnet/pythonnet@9c4a829 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c4a829

Browse files
committed
fixed ImportHook not seeing namespaces in assemblies loaded via direct call to Assembly.Load* methods
1 parent 9d5f579 commit 9c4a829

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

src/runtime/assemblymanager.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,10 @@ internal static void ScanAssembly(Assembly assembly)
290290
for (var n = 0; n < names.Length; n++)
291291
{
292292
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+
}
294297
}
295298
}
296299

src/runtime/importhook.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Runtime.InteropServices;
2+
using System.Collections.Concurrent;
43

54
namespace Python.Runtime
65
{
@@ -37,6 +36,9 @@ def find_spec(klass, fullname, paths=None, target=None):
3736
if 'clr' not in sys.modules:
3837
return None
3938
clr = sys.modules['clr']
39+
40+
clr._add_pending_namespaces()
41+
4042
if clr._available_namespaces and fullname in clr._available_namespaces:
4143
return importlib.machinery.ModuleSpec(fullname, DotNetLoader(), is_package=True)
4244
return None
@@ -169,12 +171,26 @@ static void TeardownNameSpaceTracking()
169171
Runtime.PyDict_SetItemString(root.dict, availableNsKey, Runtime.PyNone);
170172
}
171173

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)
173189
{
174190
var pyNs = Runtime.PyString_FromString(name);
175191
try
176192
{
177-
var nsSet = Runtime.PyDict_GetItemString(new BorrowedReference(root.dict), availableNsKey);
193+
var nsSet = Runtime.PyDict_GetItemString(root.DictRef, availableNsKey);
178194
if (!(nsSet.IsNull || nsSet.DangerousGetAddress() == Runtime.PyNone))
179195
{
180196
if (Runtime.PySet_Add(nsSet, new BorrowedReference(pyNs)) != 0)

src/runtime/moduleobject.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,9 @@ public static Assembly AddReference(string name)
535535
// method because it may be called from other threads, leading to deadlocks
536536
// if it is called while Python code is executing.
537537
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);
540541
}
541542
return assembly;
542543
}
@@ -602,5 +603,9 @@ public static ModuleObject _load_clr_module(PyObject spec)
602603
mod = ImportHook.Import(modname.ToString());
603604
return mod;
604605
}
606+
607+
[ModuleFunction]
608+
[ForbidPythonThreads]
609+
public static int _add_pending_namespaces() => ImportHook.AddPendingNamespaces();
605610
}
606611
}

src/runtime/native/TypeOffset.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ static void ValidateRequiredOffsetsPresent(PropertyInfo[] offsetProperties)
161161
"Initialize",
162162
"InitializeSlots",
163163
"ListAssemblies",
164-
"_load_clr_module",
164+
nameof(CLRModule._load_clr_module),
165+
nameof(CLRModule._add_pending_namespaces),
< 46EB /code>
165166
"Release",
166167
"Reset",
167168
"set_SuppressDocs",

0 commit comments

Comments
 (0)
0