Skip to content

Commit 86ea1fd

Browse files
Use underlying library for error handling
1 parent 724952d commit 86ea1fd

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

src/ITS/ITU/PSeries/P2108/p2108.py

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,6 @@
44
from enum import IntEnum
55
from pathlib import Path
66

7-
ERROR_CODES = {
8-
0: "Successful execution",
9-
3100: "Frequency must be between 0.3 and 3 GHz, inclusive",
10-
3101: "Antenna heigh must be >= 0 meters",
11-
3102: "Street width must be > 0 meters",
12-
3103: "Representative clutter height must be > 0 meters",
13-
3104: "Invalid value for clutter type",
14-
3200: "Frequency must be between 2 and 67 GHz, inclusive",
15-
3201: "Path distance must be >= 0.25 km",
16-
3202: "Percentage must be between 0 and 100",
17-
3300: "Frequency must be between 10 and 100 GHz, inclusive",
18-
3301: "Elevation angle must be between 0 and 100 GHz, inclusive",
19-
3302: "Percentage must be between 0 and 100, inclusive",
20-
}
21-
227

238
class ClutterType(IntEnum):
249
WaterSea = 1
@@ -31,11 +16,6 @@ class ClutterType(IntEnum):
3116

3217
# Load the compiled library
3318
lib_name = "P2108"
34-
if sys.maxsize > 2**32:
35-
lib_name += "x64"
36-
else:
37-
# note: 32-bit python is needed to load 32-bit lib.
38-
lib_name += "x86"
3919
if platform.uname()[0] == "Windows":
4020
lib_name += ".dll"
4121
elif platform.uname()[0] == "Linux":
@@ -73,18 +53,30 @@ class ClutterType(IntEnum):
7353
c_int,
7454
POINTER(c_double),
7555
)
56+
lib.GetReturnStatusCharArray.restype = POINTER(c_char_p)
57+
lib.GetReturnStatusCharArray.argtypes = (c_int,)
58+
lib.FreeReturnStatusCharArray.restype = None
59+
lib.FreeReturnStatusCharArray.argtypes = (POINTER(c_char_p),)
60+
7661

62+
def err_check(rtn_code: int) -> None:
63+
"""Parse the library's return code and raise an error if one occurred.
7764
78-
def err_check(error_code: int) -> None:
79-
""" """
80-
ec = error_code
81-
if ec == 0:
65+
Returns immediately for `rtn_code == 0`, otherwise retrieves the
66+
status message string from the underlying library and raises a
67+
RuntimeError with the status message.
68+
69+
:param rtn_code: Integer return code from the underlying library.
70+
:raises RuntimeError: For any non-zero inputs.
71+
:return: None
72+
"""
73+
if rtn_code == 0:
8274
return
83-
if ec in ERROR_CODES:
84-
# All errors as of v1.0 pertain to inputs out-of-range
85-
raise ValueError(ec)
8675
else:
87-
raise Exception(f"ITS.ITU.PSeries.P2108 returned unknown error {ec}")
76+
msg = lib.GetReturnStatusCharArray(c_int(rtn_code))
77+
msg_str = cast(msg, c_char_p).value.decode("utf-8")
78+
lib.FreeReturnStatusCharArray(msg)
79+
raise RuntimeError(msg_str)
8880

8981

9082
def HeightGainTerminalCorrectionModel(

tests/test_p2108.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_AeronauticalStatisticalModel(inputs, rtn, expected):
2929
out = P2108.AeronauticalStatisticalModel(*inputs)
3030
assert out == pytest.approx(expected, abs=ABSTOL__DB)
3131
else:
32-
with pytest.raises(ValueError):
32+
with pytest.raises(RuntimeError):
3333
P2108.AeronauticalStatisticalModel(*inputs)
3434

3535

@@ -43,7 +43,7 @@ def test_HeightGainTerminalCorrection(inputs, rtn, expected):
4343
out = P2108.HeightGainTerminalCorrectionModel(*inputs[:-1], clutter_type)
4444
assert out == pytest.approx(expected, abs=ABSTOL__DB)
4545
else:
46-
with pytest.raises(ValueError):
46+
with pytest.raises(RuntimeError):
4747
P2108.HeightGainTerminalCorrectionModel(*inputs[:-1], clutter_type)
4848

4949

@@ -55,5 +55,5 @@ def test_TerrestrialStatisticalModel(inputs, rtn, expected):
5555
out = P2108.TerrestrialStatisticalModel(*inputs)
5656
assert out == pytest.approx(expected, abs=ABSTOL__DB)
5757
else:
58-
with pytest.raises(ValueError):
58+
with pytest.raises(RuntimeError):
5959
P2108.TerrestrialStatisticalModel(*inputs)

0 commit comments

Comments
 (0)