Skip to content

Commit 488a1f0

Browse files
committed
Use the same API for command line and runtimeconfig hosting
1 parent d804f59 commit 488a1f0

File tree

3 files changed

+44
-71
lines changed

3 files changed

+44
-71
lines changed

clr_loader/__init__.py

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"get_mono",
1212
"get_netfx",
1313
"get_coreclr",
14-
"get_coreclr_command_line",
1514
"find_dotnet_root",
1615
"find_libmono",
1716
"find_runtimes",
@@ -92,6 +91,7 @@ def get_mono(
9291
def get_coreclr(
9392
*,
9493
runtime_config: Optional[StrOrPath] = None,
94+
entry_dll: Optional[StrOrPath] = None,
9595
dotnet_root: Optional[StrOrPath] = None,
9696
properties: Optional[Dict[str, str]] = None,
9797
runtime_spec: Optional[DotnetCoreRuntimeSpec] = None,
@@ -104,9 +104,13 @@ def get_coreclr(
104104
the first function object is retrieved.
105105
106106
:param runtime_config:
107-
Pass to a ``runtimeconfig.json`` as generated by
107+
Path to a ``runtimeconfig.json`` as generated by
108108
``dotnet publish``. If this parameter is not given, a temporary runtime
109109
config will be generated.
110+
:param entry_dll:
111+
The path to the entry dll. If this parameter is given, the runtime will
112+
be initialized using the command line hosting API instead of using the
113+
runtime config.
110114
:param dotnet_root:
111115
The root directory of the .NET Core installation. If this is not
112116
specified, we try to discover it using :py:func:`find_dotnet_root`.
@@ -117,6 +121,7 @@ def get_coreclr(
117121
If the ``runtime_config`` is not specified, the concrete runtime to use
118122
can be controlled by passing this parameter. Possible values can be
119123
retrieved using :py:func:`find_runtimes`."""
124+
120125
from .hostfxr import DotnetCoreRuntime
121126

122127
dotnet_root = _maybe_path(dotnet_root)
@@ -125,7 +130,9 @@ def get_coreclr(
125130

126131
temp_dir = None
127132
runtime_config = _maybe_path(runtime_config)
128-
if runtime_config is None:
133+
entry_dll = _maybe_path(entry_dll)
134+
135+
if runtime_config is None and entry_dll is None:
129136
if runtime_spec is None:
130137
candidates = [
131138
rt for rt in find_runtimes() if rt.name == "Microsoft.NETCore.App"
@@ -142,7 +149,12 @@ def get_coreclr(
142149
with open(runtime_config, "w") as f:
143150
runtime_spec.write_config(f)
144151

145-
impl = DotnetCoreRuntime(runtime_config=runtime_config, dotnet_root=dotnet_root)
152+
if entry_dll is not None:
153+
entry_dll = _maybe_path(entry_dll)
154+
impl = DotnetCoreRuntime(entry_dll=entry_dll, dotnet_root=dotnet_root)
155+
else:
156+
impl = DotnetCoreRuntime(runtime_config=runtime_config, dotnet_root=dotnet_root)
157+
146158
if properties:
147159
for key, value in properties.items():
148160
impl[key] = value
@@ -153,41 +165,6 @@ def get_coreclr(
153165
return impl
154166

155167

156-
def get_coreclr_command_line(
157-
*,
158-
entry_dll: StrOrPath,
159-
dotnet_root: Optional[StrOrPath] = None,
160-
properties: Optional[Dict[str, str]] = None,
161-
) -> Runtime:
162-
"""Get a CoreCLR (.NET Core) runtime instance
163-
The returned ``DotnetCoreRuntimeCommandLine`` also acts as a mapping of the config
164-
properties. They can be retrieved using the index operator and can be
165-
written until the runtime is initialized. The runtime is initialized when
166-
the first function object is retrieved.
167-
:param entry_dll:
168-
The path to the entry dll.
169-
:param dotnet_root:
170-
The root directory of the .NET Core installation. If this is not
171-
specified, we try to discover it using :py:func:`find_dotnet_root`.
172-
:param properties:
173-
Additional runtime properties. These can also be passed using the
174-
``configProperties`` section in the runtime config."""
175-
from .hostfxr import DotnetCoreCommandRuntime
176-
177-
dotnet_root = _maybe_path(dotnet_root)
178-
if dotnet_root is None:
179-
dotnet_root = find_dotnet_root()
180-
181-
impl = DotnetCoreCommandRuntime(
182-
entry_dll=_maybe_path(entry_dll), dotnet_root=dotnet_root
183-
)
184-
if properties:
185-
for key, value in properties.items():
186-
impl[key] = value
187-
188-
return impl
189-
190-
191168
def get_netfx(
192169
*, domain: Optional[str] = None, config_file: Optional[StrOrPath] = None
193170
) -> Runtime:

clr_loader/hostfxr.py

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@
1111
_IS_SHUTDOWN = False
1212

1313

14-
class DotnetCoreRuntimeBase(Runtime):
14+
class DotnetCoreRuntime(Runtime):
1515
_version: str
1616

17-
def __init__(self, dotnet_root: Path):
17+
def __init__(
18+
self,
19+
*,
20+
dotnet_root: Path,
21+
runtime_config: Optional[Path] = None,
22+
entry_dll: Optional[Path] = None,
23+
**params: str,
24+
):
1825
self._handle = None
1926

2027
if _IS_SHUTDOWN:
@@ -24,6 +31,23 @@ def __init__(self, dotnet_root: Path):
2431
self._dll = load_hostfxr(self._dotnet_root)
2532
self._load_func = None
2633

34+
if runtime_config is not None:
35+
self._handle = _get_handle_for_runtime_config(
36+
self._dll, self._dotnet_root, runtime_config
37+
)
38+
elif entry_dll is not None:
39+
self._handle = _get_handle_for_dotnet_command_line(
40+
self._dll, self._dotnet_root, entry_dll
41+
)
42+
else:
43+
raise ValueError("Either runtime_config or entry_dll must be provided")
44+
45+
for key, value in params.items():
46+
self[key] = value
47+
48+
# TODO: Get version
49+
self._version: str = "<undefined>"
50+
2751
@property
2852
def dotnet_root(self) -> Path:
2953
return self._dotnet_root
@@ -117,34 +141,6 @@ def info(self):
117141
)
118142

119143

120-
class DotnetCoreRuntime(DotnetCoreRuntimeBase):
121-
def __init__(self, runtime_config: Path, dotnet_root: Path, **params: str):
122-
super().__init__(dotnet_root)
123-
self._handle = _get_handle_for_runtime_config(
124-
self._dll, self._dotnet_root, runtime_config
125-
)
126-
127-
for key, value in params.items():
128-
self[key] = value
129-
130-
# TODO: Get version
131-
self._version = "<undefined>"
132-
133-
134-
class DotnetCoreCommandRuntime(DotnetCoreRuntimeBase):
135-
def __init__(self, entry_dll: Path, dotnet_root: Path, **params: str):
136-
super().__init__(dotnet_root)
137-
self._handle = _get_handle_for_dotnet_command_line(
138-
self._dll, self._dotnet_root, entry_dll
139-
)
140-
141-
for key, value in params.items():
142-
self[key] = value
143-
144-
# TODO: Get version
145-
self._version = "<undefined>"
146-
147-
148144
def _get_handle_for_runtime_config(
149145
dll, dotnet_root: StrOrPath, runtime_config: StrOrPath
150146
):

tests/test_common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ def test_coreclr_command_line(example_netcore: Path):
9797

9898

9999
def _do_test_coreclr_command_line(example_netcore: Path):
100-
from clr_loader import get_coreclr_command_line
100+
from clr_loader import get_coreclr
101101

102-
coreclr = get_coreclr_command_line(entry_dll=example_netcore / "example.dll")
102+
coreclr = get_coreclr(entry_dll=example_netcore / "example.dll")
103103
asm = coreclr.get_assembly(example_netcore / "example.dll")
104104

105105
run_tests(asm)

0 commit comments

Comments
 (0)