Skip to content

Commit a88b6a1

Browse files
authoredAug 7, 2023
Merge pull request #42 from pythonnet/netfx-domain
.NET Framework domains
2 parents 000bc78 + 59f47f9 commit a88b6a1

File tree

3 files changed

+41
-29
lines changed

3 files changed

+41
-29
lines changed
 

‎clr_loader/netfx.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ def __init__(
1414
):
1515
initialize()
1616
if config_file is not None:
17-
config_file_s = str(config_file)
17+
config_file_s = str(config_file).encode("utf8")
1818
else:
1919
config_file_s = ffi.NULL
2020

21+
domain_s = domain.encode("utf8") if domain else ffi.NULL
22+
2123
self._domain_name = domain
2224
self._config_file = config_file
23-
self._domain = _FW.pyclr_create_appdomain(domain or ffi.NULL, config_file_s)
25+
self._domain = _FW.pyclr_create_appdomain(domain_s, config_file_s)
2426

2527
def info(self) -> RuntimeInfo:
2628
return RuntimeInfo(
@@ -41,6 +43,11 @@ def _get_callable(self, assembly_path: StrOrPath, typename: str, function: str):
4143
function.encode("utf8"),
4244
)
4345

46+
if func == ffi.NULL:
47+
raise RuntimeError(
48+
f"Failed to resolve {typename}.{function} from {assembly_path}"
49+
)
50+
4451
return func
4552

4653
def shutdown(self):

‎netfx_loader/ClrLoader.cs

+2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ public static IntPtr CreateAppDomain(
3232
{
3333
var setup = new AppDomainSetup
3434
{
35+
ApplicationBase = AppDomain.CurrentDomain.BaseDirectory,
3536
ConfigurationFile = configFile
3637
};
38+
Print($"Base: {AppDomain.CurrentDomain.BaseDirectory}");
3739
var domain = AppDomain.CreateDomain(name, null, setup);
3840

3941
Print($"Located domain {domain}");

‎tests/test_common.py

+30-27
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,15 @@ def test_coreclr(example_netcore: Path):
7676

7777

7878
def test_coreclr_properties(example_netcore: Path):
79-
from multiprocessing import get_context
80-
81-
p = get_context("spawn").Process(
82-
target=_do_test_coreclr_autogenerated_runtimeconfig,
83-
args=(example_netstandard,),
84-
kwargs=dict(properties=dict(APP_CONTEXT_BASE_DIRECTORY=str(example_netcore))),
79+
run_in_subprocess(
80+
_do_test_coreclr_autogenerated_runtimeconfig,
81+
example_netstandard,
82+
properties=dict(APP_CONTEXT_BASE_DIRECTORY=str(example_netcore)),
8583
)
86-
p.start()
87-
p.join()
88-
p.close()
8984

9085

9186
def test_coreclr_autogenerated_runtimeconfig(example_netstandard: Path):
92-
from multiprocessing import get_context
93-
94-
p = get_context("spawn").Process(
95-
target=_do_test_coreclr_autogenerated_runtimeconfig, args=(example_netstandard,)
96-
)
97-
p.start()
98-
p.join()
99-
p.close()
87+
run_in_subprocess(_do_test_coreclr_autogenerated_runtimeconfig, example_netstandard)
10088

10189

10290
def _do_test_coreclr_autogenerated_runtimeconfig(
@@ -114,25 +102,31 @@ def _do_test_coreclr_autogenerated_runtimeconfig(
114102
sys.platform != "win32", reason=".NET Framework only exists on Windows"
115103
)
116104
def test_netfx(example_netstandard: Path):
117-
from clr_loader import get_netfx
118-
119-
netfx = get_netfx()
120-
asm = netfx.get_assembly(example_netstandard / "example.dll")
121-
122-
run_tests(asm)
105+
run_in_subprocess(_do_test_netfx, example_netstandard)
123106

124107

125108
@pytest.mark.skipif(
126109
sys.platform != "win32", reason=".NET Framework only exists on Windows"
127110
)
128111
def test_netfx_chinese_path(example_netstandard: Path, tmpdir_factory):
129-
from clr_loader import get_netfx
130-
131112
tmp_path = Path(tmpdir_factory.mktemp("example-中国"))
132113
shutil.copytree(example_netstandard, tmp_path, dirs_exist_ok=True)
133114

134-
netfx = get_netfx()
135-
asm = netfx.get_assembly(os.path.join(example_netstandard, "example.dll"))
115+
run_in_subprocess(_do_test_netfx, tmp_path)
116+
117+
118+
@pytest.mark.skipif(
119+
sys.platform != "win32", reason=".NET Framework only exists on Windows"
120+
)
121+
def test_netfx_separate_domain(example_netstandard):
122+
run_in_subprocess(_do_test_netfx, example_netstandard, domain="some domain")
123+
124+
125+
def _do_test_netfx(example_netstandard, **kwargs):
126+
from clr_loader import get_netfx
127+
128+
netfx = get_netfx(**kwargs)
129+
asm = netfx.get_assembly(example_netstandard / "example.dll")
136130

137131
run_tests(asm)
138132

@@ -142,3 +136,12 @@ def run_tests(asm):
142136
test_data = b"testy mctestface"
143137
res = func(test_data)
144138
assert res == len(test_data)
139+
140+
141+
def run_in_subprocess(func, *args, **kwargs):
142+
from multiprocessing import get_context
143+
144+
p = get_context("spawn").Process(target=func, args=args, kwargs=kwargs)
145+
p.start()
146+
p.join()
147+
p.close()

0 commit comments

Comments
 (0)
Please sign in to comment.