Skip to content

Commit 5f7cbfb

Browse files
committed
Add a few type hints and docstrings
1 parent 10c8e29 commit 5f7cbfb

File tree

10 files changed

+65
-50
lines changed

10 files changed

+65
-50
lines changed

clr_loader/__init__.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,81 @@
11
from os.path import basename
2+
from typing import Optional, Any
23
from .ffi import ffi
34

45

56
__all__ = ["get_mono", "get_netfx", "get_coreclr"]
67

78

9+
RuntimeImpl = Any
10+
11+
812
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+
):
1016
self._assembly = assembly
1117
self._class = typename
1218
self._name = func_name
1319

1420
self._callable = runtime.get_callable(assembly, typename, func_name)
1521

16-
def __call__(self, buffer):
22+
def __call__(self, buffer: bytes) -> int:
1723
buf_arr = ffi.from_buffer("char[]", buffer)
1824
return self._callable(ffi.cast("void*", buf_arr), len(buf_arr))
1925

20-
def __repr__(self):
26+
def __repr__(self) -> str:
2127
return f"<ClrFunction {self._class}.{self._name} in {basename(self._assembly)}>"
2228

2329

2430
class Assembly:
25-
def __init__(self, runtime, path):
31+
def __init__(self, runtime: RuntimeImpl, path: str):
2632
self._runtime = runtime
2733
self._path = path
2834

29-
def get_function(self, name, func=None):
35+
def get_function(self, name: str, func: Optional[str] = None) -> ClrFunction:
3036
if func is None:
3137
name, func = name.rsplit(".", 1)
3238

3339
return ClrFunction(self._runtime, self._path, name, func)
3440

35-
def __getitem__(self, name):
41+
def __getitem__(self, name: str) -> ClrFunction:
3642
return self.get_function(name)
3743

38-
def __repr__(self):
44+
def __repr__(self) -> str:
3945
return f"<Assembly {self._path} in {self._runtime}>"
4046

4147

4248
class Runtime:
43-
def __init__(self, impl):
49+
def __init__(self, impl: RuntimeImpl):
4450
self._impl = impl
4551

46-
def get_assembly(self, path):
52+
def get_assembly(self, path: str) -> Assembly:
4753
return Assembly(self._impl, path)
4854

49-
def __getitem__(self, path):
55+
def __getitem__(self, path: str) -> Assembly:
5056
return self.get_assembly(path)
5157

5258

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:
5465
from .mono import Mono
5566

5667
impl = Mono(domain=domain, config_file=config_file, path=path, gc=gc)
5768
return Runtime(impl)
5869

5970

60-
def get_coreclr(runtime_config, dotnet_root=None):
71+
def get_coreclr(runtime_config: str, dotnet_root: Optional[str] = None) -> Runtime:
6172
from .hostfxr import HostFxr
6273

6374
impl = HostFxr(runtime_config=runtime_config, dotnet_root=dotnet_root)
6475
return Runtime(impl)
6576

6677

67-
def get_netfx(name=None, config_file=None):
78+
def get_netfx(name: Optional[str] = None, config_file: Optional[str] = None) -> Runtime:
6879
from .netfx import NetFx
6980

7081
impl = NetFx(name=name, config_file=config_file)

clr_loader/ffi/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import glob
22
import os
3-
import shutil
43
import sys
4+
from typing import Optional
55

6-
import cffi
6+
import cffi # type: ignore
77

88
from . import hostfxr, mono, netfx
99

@@ -15,7 +15,7 @@
1515
ffi.cdef(cdef)
1616

1717

18-
def load_hostfxr(dotnet_root):
18+
def load_hostfxr(dotnet_root: str):
1919
hostfxr_name = _get_dll_name("hostfxr")
2020
hostfxr_path = os.path.join(dotnet_root, "host", "fxr", "?.*", hostfxr_name)
2121

@@ -28,7 +28,7 @@ def load_hostfxr(dotnet_root):
2828
raise RuntimeError(f"Could not find a suitable hostfxr library in {dotnet_root}")
2929

3030

31-
def load_mono(path=None, gc=None):
31+
def load_mono(path: Optional[str] = None, gc: Optional[str] = None):
3232
# Preload C++ standard library, Mono needs that and doesn't properly link against it
3333
if sys.platform.startswith("linux"):
3434
ffi.dlopen("stdc++", ffi.RTLD_GLOBAL)
@@ -58,7 +58,7 @@ def load_netfx():
5858
return ffi.dlopen(path)
5959

6060

61-
def _get_dll_name(name):
61+
def _get_dll_name(name: str) -> str:
6262
if sys.platform == "win32":
6363
return f"{name}.dll"
6464
elif sys.platform == "darwin":

clr_loader/ffi/hostfxr.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# flake8: noqa
2+
13
import sys
24

35
cdef = []

clr_loader/ffi/mono.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# flake8: noqa
2+
13
cdef = []
24

35
cdef.append(

clr_loader/ffi/netfx.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# flake8: noqa
2+
13
cdef = [
24
"""
35
typedef void* pyclr_domain;

clr_loader/util/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
from .hostfxr_errors import get_hostfxr_error
44

55

6-
def check_result(err_code):
6+
def check_result(err_code: int) -> None:
7+
"""Check the error code of a .NET hosting API function and raise a
8+
converted exception.
9+
10+
:raises ClrError: If the error code is `< 0`
11+
"""
12+
713
if err_code < 0:
8-
hresult = err_code & 0xFFFFFFFF
14+
hresult = err_code & 0xFFFF_FFFF
915

1016
error = get_coreclr_error(hresult)
1117
if not error:

clr_loader/util/clr_error.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
from typing import Optional
2+
3+
14
class ClrError(Exception):
2-
def __init__(self, hresult, name=None, message=None, comment=None):
5+
def __init__(
6+
self,
7+
hresult: int,
8+
name: Optional[str] = None,
9+
message: Optional[str] = None,
10+
comment: Optional[str] = None,
11+
):
312
self.hresult = hresult
413
self.name = name
514
self.message = message

clr_loader/util/coreclr_errors.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
from typing import Dict, Optional
2+
13
from .clr_error import ClrError
24

35

4-
def get_coreclr_error(hresult):
6+
def get_coreclr_error(hresult: int) -> Optional[ClrError]:
57
name = SymbolicName.get(hresult)
68
if not name:
79
return None
@@ -14,9 +16,9 @@ def get_coreclr_error(hresult):
1416
)
1517

1618

17-
Comment = {}
18-
SymbolicName = {}
19-
Message = {}
19+
Comment: Dict[int, str] = {}
20+
SymbolicName: Dict[int, str] = {}
21+
Message: Dict[int, str] = {}
2022

2123

2224
if __name__ == "__main__":
@@ -48,8 +50,7 @@ def get_coreclr_error(hresult):
4850

4951
for row in tree.findall(".//HRESULT"):
5052
try:
51-
numeric_value = row.attrib["NumericValue"]
52-
numeric_value = int(numeric_value, base=16)
53+
numeric_value = int(row.attrib["NumericValue"], base=16)
5354

5455
for child in row:
5556
if child.text:

clr_loader/util/hostfxr_errors.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
from typing import Optional
2+
13
from .clr_error import ClrError
24

35
__all__ = ["get_hostfxr_error"]
46

57

6-
def get_hostfxr_error(hresult):
8+
def get_hostfxr_error(hresult: int) -> Optional[ClrError]:
79
if hresult in HOSTFXR_ERRORS:
810
return ClrError(hresult, HOSTFXR_ERRORS[hresult])
11+
else:
12+
return None
913

1014

1115
_ERRORS = dict(

clr_loader/util/runtime.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)