Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a321daa

Browse files
committed
(WIP) modernize the import hook
Implement a meta path loader instead
1 parent 7d8f754 commit a321daa

File tree

5 files changed

+160
-214
lines changed

5 files changed

+160
-214
lines changed

src/runtime/assemblymanager.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ 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;
4043

4144
private AssemblyManager()
4245
{
@@ -284,6 +287,17 @@ internal static void ScanAssembly(Assembly assembly)
284287
if (ns != null)
285288
{
286289
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+
}
287301
}
288302

289303
if (ns != null && t.IsGenericTypeDefinition)
@@ -312,6 +326,15 @@ public static bool IsValidNamespace(string name)
312326
return !string.IsNullOrEmpty(name) && namespaces.ContainsKey(name);
313327
}
314328

329+
/// <summary>
330+
/// Returns an IEnumerable<string> containing the namepsaces exported
331+
/// by loaded assemblies in the current app domain.
332+
/// </summary>
333+
public static IEnumerable<string> GetNamespaces ()
334+
{
335+
return namespaces.Keys;
336+
}
337+
315338
/// <summary>
316339
/// Returns list of assemblies that declare types in a given namespace
317340
/// </summary>

src/runtime/extensiontype.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static int tp_setattro(IntPtr ob, IntPtr key, IntPtr val)
8383
{
8484
message = "readonly attribute";
8585
}
86-
Exceptions.SetError(Exceptions.TypeError, message);
86+
Exceptions.SetError(Exceptions.AttributeError, message);
8787
return -1;
8888
}
8989

0 commit comments

Comments
 (0)