File tree Expand file tree Collapse file tree 3 files changed +9
-31
lines changed Expand file tree Collapse file tree 3 files changed +9
-31
lines changed Original file line number Diff line number Diff line change @@ -37,10 +37,6 @@ internal class AssemblyManager
37
37
// modified from event handlers below, potentially triggered from different .NET threads
38
38
private static ConcurrentQueue < Assembly > assemblies ;
39
39
internal static List < string > pypath ;
40
-
41
- // Triggered when a new namespace is added to the namespaces dictionary
42
- // public static event Action<string> namespaceAdded;
43
-
44
40
private AssemblyManager ( )
45
41
{
46
42
}
@@ -287,17 +283,6 @@ internal static void ScanAssembly(Assembly assembly)
287
283
if ( ns != null )
288
284
{
289
285
namespaces [ ns ] . TryAdd ( assembly , string . Empty ) ;
290
- // try
291
- // {
292
- // namespaceAdded?.Invoke(ns);
293
- // }
294
- // catch (Exception e)
295
- // {
296
- // // For some reason, exceptions happening here does... nothing.
297
- // // Even System.AccessViolationExceptions gets ignored.
298
- // Console.WriteLine($"Namespace added callback failed with: {e}");
299
- // throw;
300
- // }
301
286
}
302
287
303
288
if ( ns != null && t . IsGenericTypeDefinition )
Original file line number Diff line number Diff line change @@ -176,30 +176,21 @@ static void SetupNamespaceTracking()
176
176
newset . Dispose ( ) ;
177
177
}
178
178
179
- // AssemblyManager.namespaceAdded += OnNamespaceAdded;
180
- // PythonEngine.AddShutdownHandler(() => AssemblyManager.namespaceAdded -= OnNamespaceAdded);
181
179
}
182
180
183
181
/// <summary>
184
- /// Removes the set of available namespaces from the clr module and
185
- /// removes the callback on the OnNamespaceAdded event.
182
+ /// Removes the set of available namespaces from the clr module.
186
183
/// </summary>
187
184
static void TeardownNameSpaceTracking ( )
188
185
{
189
- // AssemblyManager.namespaceAdded -= OnNamespaceAdded;
190
186
// If the C# runtime isn't loaded, then there are no namespaces available
191
187
Runtime . PyDict_SetItemString ( root . dict , availableNsKey , Runtime . PyNone ) ;
192
188
}
193
189
194
- public static void OnNamespaceAdded ( string name )
190
+ public static void AddNamespace ( string name )
195
191
{
196
- Console . WriteLine ( System . Environment . StackTrace ) ;
197
- Console . WriteLine ( "OnNamespaceAdded: acquiring" ) ;
198
- Console . Out . Flush ( ) ;
199
192
using ( Py . GIL ( ) )
200
193
{
201
- Console . WriteLine ( "OnNamespaceAdded: acquired" ) ;
202
- Console . Out . Flush ( ) ;
203
194
var pyNs = Runtime . PyString_FromString ( name ) ;
204
195
try
205
196
{
@@ -217,8 +208,6 @@ public static void OnNamespaceAdded(string name)
217
208
Runtime . XDecref ( pyNs ) ;
218
209
}
219
210
}
220
- Console . WriteLine ( "OnNamespaceAdded: released" ) ;
221
- Console . Out . Flush ( ) ;
222
211
}
223
212
224
213
Original file line number Diff line number Diff line change @@ -509,6 +509,7 @@ public static bool SuppressOverloads
509
509
public static Assembly AddReference ( string name )
510
510
{
511
511
AssemblyManager . UpdatePath ( ) ;
512
+ var origNs = AssemblyManager . GetNamespaces ( ) ;
512
513
Assembly assembly = null ;
513
514
assembly = AssemblyManager . FindLoadedAssembly ( name ) ;
514
515
if ( assembly == null )
@@ -530,9 +531,12 @@ public static Assembly AddReference(string name)
530
531
// Classes that are not in a namespace needs an extra nudge to be found.
531
532
ImportHook . UpdateCLRModuleDict ( ) ;
532
533
533
- // Heavyhanded but otherwise we'd need a "addedSinceLastCall".
534
- foreach ( var ns in AssemblyManager . GetNamespaces ( ) ) {
535
- ImportHook . OnNamespaceAdded ( ns ) ;
534
+ // A bit heavyhanded, but we can't use the AssemblyManager's AssemblyLoadHandler
535
+ // method because it may be called from other threads, leading to deadlocks
536
+ // if it is called while Python code is executing.
537
+ var currNs = AssemblyManager . GetNamespaces ( ) . Except ( origNs ) ;
538
+ foreach ( var ns in currNs ) {
539
+ ImportHook . AddNamespace ( ns ) ;
536
540
}
537
541
return assembly ;
538
542
}
You can’t perform that action at this time.
0 commit comments