|
1 | 1 | from os.path import basename |
| 2 | +from typing import Optional, Any |
2 | 3 | from .ffi import ffi |
3 | 4 |
|
4 | 5 |
|
5 | 6 | __all__ = ["get_mono", "get_netfx", "get_coreclr"] |
6 | 7 |
|
7 | 8 |
|
| 9 | +RuntimeImpl = Any |
| 10 | + |
| 11 | + |
8 | 12 | class ClrFunction: |
9 | | - def __init__(self, runtime, assembly, typename, func_name): |
| 13 | + def __init__( |
| 14 | + self, runtime: RuntimeImpl, assembly: str, typename: str, func_name: str |
| 15 | + ): |
10 | 16 | self._assembly = assembly |
11 | 17 | self._class = typename |
12 | 18 | self._name = func_name |
13 | 19 |
|
14 | 20 | self._callable = runtime.get_callable(assembly, typename, func_name) |
15 | 21 |
|
16 | | - def __call__(self, buffer): |
| 22 | + def __call__(self, buffer: bytes) -> int: |
17 | 23 | buf_arr = ffi.from_buffer("char[]", buffer) |
18 | 24 | return self._callable(ffi.cast("void*", buf_arr), len(buf_arr)) |
19 | 25 |
|
20 | | - def __repr__(self): |
| 26 | + def __repr__(self) -> str: |
21 | 27 | return f"<ClrFunction {self._class}.{self._name} in {basename(self._assembly)}>" |
22 | 28 |
|
23 | 29 |
|
24 | 30 | class Assembly: |
25 | | - def __init__(self, runtime, path): |
| 31 | + def __init__(self, runtime: RuntimeImpl, path: str): |
26 | 32 | self._runtime = runtime |
27 | 33 | self._path = path |
28 | 34 |
|
29 | | - def get_function(self, name, func=None): |
| 35 | + def get_function(self, name: str, func: Optional[str] = None) -> ClrFunction: |
30 | 36 | if func is None: |
31 | 37 | name, func = name.rsplit(".", 1) |
32 | 38 |
|
33 | 39 | return ClrFunction(self._runtime, self._path, name, func) |
34 | 40 |
|
35 | | - def __getitem__(self, name): |
| 41 | + def __getitem__(self, name: str) -> ClrFunction: |
36 | 42 | return self.get_function(name) |
37 | 43 |
|
38 | | - def __repr__(self): |
| 44 | + def __repr__(self) -> str: |
39 | 45 | return f"<Assembly {self._path} in {self._runtime}>" |
40 | 46 |
|
41 | 47 |
|
42 | 48 | class Runtime: |
43 | | - def __init__(self, impl): |
| 49 | + def __init__(self, impl: RuntimeImpl): |
44 | 50 | self._impl = impl |
45 | 51 |
|
46 | | - def get_assembly(self, path): |
| 52 | + def get_assembly(self, path: str) -> Assembly: |
47 | 53 | return Assembly(self._impl, path) |
48 | 54 |
|
49 | | - def __getitem__(self, path): |
| 55 | + def __getitem__(self, path: str) -> Assembly: |
50 | 56 | return self.get_assembly(path) |
51 | 57 |
|
52 | 58 |
|
53 | | -def get_mono(domain=None, config_file=None, path=None, gc=None): |
| 59 | +def get_mono( |
| 60 | + domain: Optional[str] = None, |
| 61 | + config_file: Optional[str] = None, |
| 62 | + path: Optional[str] = None, |
| 63 | + gc: Optional[str] = None, |
| 64 | +) -> Runtime: |
54 | 65 | from .mono import Mono |
55 | 66 |
|
56 | 67 | impl = Mono(domain=domain, config_file=config_file, path=path, gc=gc) |
57 | 68 | return Runtime(impl) |
58 | 69 |
|
59 | 70 |
|
60 | | -def get_coreclr(runtime_config, dotnet_root=None): |
| 71 | +def get_coreclr(runtime_config: str, dotnet_root: Optional[str] = None) -> Runtime: |
61 | 72 | from .hostfxr import HostFxr |
62 | 73 |
|
63 | 74 | impl = HostFxr(runtime_config=runtime_config, dotnet_root=dotnet_root) |
64 | 75 | return Runtime(impl) |
65 | 76 |
|
66 | 77 |
|
67 | | -def get_netfx(name=None, config_file=None): |
| 78 | +def get_netfx(name: Optional[str] = None, config_file: Optional[str] = None) -> Runtime: |
68 | 79 | from .netfx import NetFx |
69 | 80 |
|
70 | 81 | impl = NetFx(name=name, config_file=config_file) |
|
0 commit comments