Skip to content

Commit

Permalink
WIP: use the warnings module instead of a global variable
Browse files Browse the repository at this point in the history
the unit tests still fail and I don't know how to fix them...

Signed-off-by: Andreas Lauser <[email protected]>
Signed-off-by: Christian Hackenbeck <[email protected]>
Co-authored-by: Ayoub Kaanich <[email protected]>
  • Loading branch information
andlaus and kayoub5 committed Jul 31, 2023
1 parent 4047505 commit 79d7d9a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 32 deletions.
12 changes: 6 additions & 6 deletions odxtools/ecu_variant_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .diaglayer import DiagLayer
from .diaglayertype import DiagLayerType
from .ecu_variant_patterns import MatchingParameter
from .exceptions import OdxError, odxassert
from .exceptions import odxassert, odxraise
from .odxtypes import ParameterValue
from .service import DiagService
from .structures import Response
Expand Down Expand Up @@ -88,7 +88,7 @@ def decode_ident_response(
if isinstance(decoded_val, str) or isinstance(decoded_val, int):
return str(decoded_val)

raise OdxError(f"The snref or snpathref '{matching_param.out_param_if}' is cannot be \
odxraise(f"The snref or snpathref '{matching_param.out_param_if}' is cannot be \
resolved for any positive or negative response.")

def __init__(self, ecu_variant_candidates: List[DiagLayer], use_cache: bool = True):
Expand Down Expand Up @@ -153,8 +153,8 @@ def has_match(self) -> bool:
Raises a runtime error if the matcher is pending.
"""
if self.is_pending():
raise RuntimeError(
"EcuVariantMatcher is pending. Run the request_loop to determine the active ecu variant."
odxraise(
"EcuVariantMatcher is pending. Run the request_loop to determine the active ecu variant.", RuntimeError
)
return self._state == EcuVariantMatcher.State.MATCH

Expand All @@ -169,6 +169,6 @@ def _update_cache(self, req_bytes: bytes, resp_bytes: bytes) -> None:

def _get_ident_response(self) -> bytes:
if not self._recent_ident_response:
raise RuntimeError(
"No response available. Did you forget to call 'evaluate()' in a loop?")
odxraise(
"No response available. Did you forget to call 'evaluate()' in a loop?", RuntimeError)
return self._recent_ident_response
7 changes: 2 additions & 5 deletions odxtools/ecu_variant_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import TYPE_CHECKING, List, Optional
from xml.etree import ElementTree

from .exceptions import OdxError, odxassert, odxrequire, strict_mode
from .exceptions import odxassert, odxraise, odxrequire
from .odxlink import OdxDocFragment
from .utils import is_short_name, is_short_name_path

Expand Down Expand Up @@ -44,10 +44,7 @@ def from_et(et_element: ElementTree.Element,
elif out_param_snpathref_el is not None:
out_param_if = out_param_snpathref_el.get("SHORT-NAME-PATH")
if out_param_if is None:
if TYPE_CHECKING:
raise OdxError("Output parameter must not left unspecified")
if strict_mode:
raise OdxError("Output parameter must not left unspecified")
odxraise("Output parameter must not left unspecified")

return MatchingParameter(
expected_value=expected_value,
Expand Down
28 changes: 12 additions & 16 deletions odxtools/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,26 @@
from typing import TYPE_CHECKING, NoReturn, Optional, Type, TypeVar


class OdxError(Exception):
class OdxError(Warning):
"""Any error that happens during interacting with diagnostic objects."""


class EncodeError(Warning, OdxError):
class EncodeError(OdxError):
"""Encoding of a message to raw data failed."""


class DecodeError(Warning, OdxError):
class DecodeError(OdxError):
"""Decoding raw data failed."""


class OdxWarning(Warning):
"""Any warning that happens during interacting with diagnostic objects."""


# Mark DecodeError and EncodeErrpr warnings as errors by default
# Handle DecodeError and EncodeError as errors by default
warnings.simplefilter("error", DecodeError)
warnings.simplefilter("error", EncodeError)

#: Specify whether violations of the ODX specification or failed
#: assumptions in the ODX library code should cause the routine in
#: question to abort or whether it should continue anyway. Be aware that
#: in non-strict mode, the behavior of odxtools is undefined and it
#: might start eating children...
strict_mode = True
warnings.simplefilter("error", OdxError)


def odxraise(message: Optional[str] = None, error_type: Type[Exception] = OdxError) -> NoReturn:
Expand All @@ -38,11 +32,13 @@ def odxraise(message: Optional[str] = None, error_type: Type[Exception] = OdxErr
Also, convince type checkers that the exception is always raised.
"""
if TYPE_CHECKING or strict_mode:
if message is None:
raise error_type()
else:
raise error_type(message)
if TYPE_CHECKING:
raise error_type()

if message is None:
warnings.warn("ODX conformity error detected", error_type, stacklevel=2)
else:
warnings.warn(message, error_type, stacklevel=2)


def odxassert(condition: bool,
Expand Down
5 changes: 0 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,3 @@ indent_width = 4
split_before_dict_set_generator = 1
split_before_first_argument = 0
split_penalty_after_opening_bracket=1000

[tool.pytest.ini_options]
filterwarnings = [
"error::odxtools.exceptions.DecodeError",
]

0 comments on commit 79d7d9a

Please sign in to comment.