8000 Fix a bug where clr wasn't in sys.modules after reload · pythonnet/pythonnet@afffc18 · GitHub
[go: up one dir, main page]

Skip to content

Commit afffc18

Browse files
committed
Fix a bug where clr wasn't in sys.modules after reload
1 parent f92e95b commit afffc18

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/runtime/importhook.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ internal static void RestoreRuntimeData(RuntimeDataStorage storage)
109109
storage.GetValue("py_clr_module", out py_clr_module);
110110
var rootHandle = storage.GetValue<IntPtr>("root");
111111
root = (CLRModule)ManagedType.GetManagedObject(rootHandle);
112+
IntPtr dict = Runtime.PyImport_GetModuleDict();
113+
Runtime.PyDict_SetItemString(dict, "CLR", py_clr_module);
114+
Runtime.PyDict_SetItemString(dict, "clr", py_clr_module);
112115
SetupNamespaceTracking();
113116
}
114117

tests/domain_tests/TestRunner.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,46 @@ 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.
1100+
Name = "import_after_reload",
1101+
DotNetBefore = @"
1102+
namespace TestNamespace
1103+
{
1104+
[System.Serializable]
1105+
public class Cls
1106+
{
1107+
}
1108+
}",
1109+
DotNetAfter = @"
1110+
namespace TestNamespace
1111+
{
1112+
[System.Serializable]
1113+
public class WithNestedType
1114+
{
1115+
[System.Serializable]
1116+
public class Cls
1117+
{
1118+
}
1119+
}
1120+
}",
1121+
PythonCode = @"
1122+
import sys
1123+
1124+
def before_reload():
1125+
import clr
1126+
import System
1127+
1128+
1129+
def after_reload():
1130+
assert 'System' in sys.modules
1131+
assert 'clr' in sys.modules
1132+
import clr
1133+
import System
1134+
10951135
",
10961136
},
10971137
};

tests/domain_tests/test_domain_reload.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,7 @@ def test_in_to_ref_param():
8888

8989
def test_nested_type():
9090
_run_test("nested_type")
91+
92+
@pytest.mark.skipif(platform.system() == 'Darwin', reason='FIXME: macos can\'t find the python library')
93+
def test_import_after_reload():
94+
_run_test("import_after_reload")

0 commit comments

Comments
 (0)
0