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

Skip to content

Commit 744f991

Browse files
committed
Modernize the import hook
Implement a meta path loader instead Add the loaded namespaces tracking Fix a bug where clr wasn't in sys.modules after reload Further refinements to setattr logic on ModuleObjects
1 parent 0d7498b commit 744f991

File tree

7 files changed

+262
-224
lines changed

7 files changed

+262
-224
lines changed

src/domain_tests/TestRunner.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,29 @@ assert sys.my_obj is not None
10921092
foo = sys.my_obj.Inner()
10931093
print(foo)
10941094
1095+
",
1096+
},
1097+
new TestCase
1098+
{
1099+
// The C# code for this test doesn't matter; we're testing
1100+
// that the import hook behaves properly after a domain reload
1101+
Name = "import_after_reload",
1102+
DotNetBefore = "",
1103+
DotNetAfter = "",
1104+
PythonCode = @"
1105+
import sys
1106+
1107+
def before_reload():
1108+
import clr
1109+
import System
1110+
1111+
1112+
def after_reload():
1113+
assert 'System' in sys.modules
1114+
assert 'clr' in sys.modules
1115+
import clr
1116+
import System
1117+
10951118
",
10961119
},
10971120
};

src/domain_tests/test_domain_reload.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,7 @@ def test_in_to_ref_param():
9898
@pytest.mark.skipif(platform.system() == 'Darwin', reason='FIXME: macos can\'t find the python library')
9999
def test_nested_type():
100100
_run_test("nested_type")
101+
102+
@pytest.mark.skipif(platform.system() == 'Darwin', reason='FIXME: macos can\'t find the python library')
103+
def test_import_after_reload():
104+
_run_test("import_after_reload")

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
@@ -76,7 +76,7 @@ public static int tp_setattro(IntPtr ob, IntPtr key, IntPtr val)
7676
{
7777
message = "readonly attribute";
7878
}
79-
Exceptions.SetError(Exceptions.TypeError, message);
79+
Exceptions.SetError(Exceptions.AttributeError, message);
8080
return -1;
8181
}
8282

0 commit comments

Comments
 (0)