1
1
import sys
2
2
from pathlib import Path
3
- from typing import Dict ,Optional ,Union
3
+ from typing import Dict ,Optional ,Union , Any
4
4
import clr_loader
5
5
6
6
__all__ = ["set_runtime" ,"set_runtime_from_env" ,"load" ]
7
7
8
8
_RUNTIME :Optional [clr_loader .Runtime ]= None
9
- _LOADER_ASSEMBLY :Optional [clr_loader .wrappers . Assembly ]= None
9
+ _LOADER_ASSEMBLY :Optional [clr_loader .Assembly ]= None
10
10
_LOADED :bool = False
11
11
12
12
@@ -27,6 +27,13 @@ def set_runtime(runtime: Union[clr_loader.Runtime, str], **params: str) -> None:
27
27
_RUNTIME = runtime
28
28
29
29
30
+ def get_runtime_info ()-> Optional [clr_loader .RuntimeInfo ]:
31
+ if _RUNTIME is None :
32
+ return None
33
+ else :
34
+ return _RUNTIME .info ()
35
+
36
+
30
37
def _get_params_from_env (prefix :str )-> Dict [str ,str ]:
31
38
from os import environ
32
39
@@ -43,7 +50,7 @@ def _get_params_from_env(prefix: str) -> Dict[str, str]:
43
50
44
51
45
52
def _create_runtime_from_spec (
46
- spec :str ,params :Optional [Dict [str ,str ]]= None
53
+ spec :str ,params :Optional [Dict [str ,Any ]]= None
47
54
)-> clr_loader .Runtime :
48
55
if spec == "default" :
49
56
if sys .platform == "win32" :
@@ -109,9 +116,9 @@ def load(
109
116
110
117
dll_path = Path (__file__ ).parent / "runtime" / "Python.Runtime.dll"
111
118
112
- _LOADER_ASSEMBLY = _RUNTIME .get_assembly (str (dll_path ))
119
+ _LOADER_ASSEMBLY = assembly = _RUNTIME .get_assembly (str (dll_path ))
120
+ func = assembly .get_function ("Python.Runtime.Loader.Initialize" )
113
121
114
- func = _LOADER_ASSEMBLY ["Python.Runtime.Loader.Initialize" ]
115
122
if func (b"" )!= 0 :
116
123
raise RuntimeError ("Failed to initialize Python.Runtime.dll" )
117
124
@@ -125,12 +132,12 @@ def unload() -> None:
125
132
126
133
global _RUNTIME ,_LOADER_ASSEMBLY
127
134
if _LOADER_ASSEMBLY is not None :
128
- func = _LOADER_ASSEMBLY [ "Python.Runtime.Loader.Shutdown" ]
135
+ func = _LOADER_ASSEMBLY . get_function ( "Python.Runtime.Loader.Shutdown" )
129
136
if func (b"full_shutdown" )!= 0 :
130
137
raise RuntimeError ("Failed to call Python.NET shutdown" )
131
138
132
139
_LOADER_ASSEMBLY = None
133
140
134
141
if _RUNTIME is not None :
135
- # TODO: Add explicit `close` to clr_loader
142
+ _RUNTIME . shutdown ()
136
143
_RUNTIME = None