File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -37,10 +37,6 @@ internal class AssemblyManager
3737 // modified from event handlers below, potentially triggered from different .NET threads
3838 private static ConcurrentQueue < Assembly > assemblies ;
3939 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-
4440 private AssemblyManager ( )
4541 {
4642 }
@@ -287,17 +283,6 @@ internal static void ScanAssembly(Assembly assembly)
287283 if ( ns != null )
288284 {
289285 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- // }
301286 }
302287
303288 if ( ns != null && t . IsGenericTypeDefinition )
Original file line number Diff line number Diff line change @@ -176,30 +176,21 @@ static void SetupNamespaceTracking()
176176 newset . Dispose ( ) ;
177177 }
178178
179- // AssemblyManager.namespaceAdded += OnNamespaceAdded;
180- // PythonEngine.AddShutdownHandler(() => AssemblyManager.namespaceAdded -= OnNamespaceAdded);
181179 }
182180
183181 /// <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.
186183 /// </summary>
187184 static void TeardownNameSpaceTracking ( )
188185 {
189- // AssemblyManager.namespaceAdded -= OnNamespaceAdded;
190186 // If the C# runtime isn't loaded, then there are no namespaces available
191187 Runtime . PyDict_SetItemString ( root . dict , availableNsKey , Runtime . PyNone ) ;
192188 }
193189
194- public static void OnNamespaceAdded ( string name )
190+ public static void AddNamespace ( string name )
195191 {
196- Console . WriteLine ( System . Environment . StackTrace ) ;
197- Console . WriteLine ( "OnNamespaceAdded: acquiring" ) ;
198- Console . Out . Flush ( ) ;
199192 using ( Py . GIL ( ) )
200193 {
201- Console . WriteLine ( "OnNamespaceAdded: acquired" ) ;
202- Console . Out . Flush ( ) ;
203194 var pyNs = Runtime . PyString_FromString ( name ) ;
204195 try
205196 {
@@ -217,8 +208,6 @@ public static void OnNamespaceAdded(string name)
217208 Runtime . XDecref ( pyNs ) ;
218209 }
219210 }
220- Console . WriteLine ( "OnNamespaceAdded: released" ) ;
221- Console . Out . Flush ( ) ;
222211 }
223212
224213
Original file line number Diff line number Diff line change @@ -509,6 +509,7 @@ public static bool SuppressOverloads
509509 public static Assembly AddReference ( string name )
510510 {
511511 AssemblyManager . UpdatePath ( ) ;
512+ var origNs = AssemblyManager . GetNamespaces ( ) ;
512513 Assembly assembly = null ;
513514 assembly = AssemblyManager . FindLoadedAssembly ( name ) ;
514515 if ( assembly == null )
@@ -530,9 +531,12 @@ public static Assembly AddReference(string name)
530531 // Classes that are not in a namespace needs an extra nudge to be found.
531532 ImportHook . UpdateCLRModuleDict ( ) ;
532533
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 ) ;
536540 }
537541 return assembly ;
538542 }
You can’t perform that action at this time.
0 commit comments