OpenCyphal / pydsdl

Cyphal DSDL processing front end implemented in Python
https://opencyphal.org
MIT License
10 stars 9 forks source link

str and repr can throw #108

Open thirtytwobits opened 2 months ago

thirtytwobits commented 2 months ago

Some objects in pydsdl can throw attribute errors when repr() or str() is called on them This leads to confusing error messaging when tools attempt to log exceptions thrown in initializers. For example, DSDLDefinition throws if the provided arguments are malformed. Seen in the wild is this debug output:

../pydsdl/pydsdl/_namespace.py:204: in read_files
    target_dsdl_definitions = _construct_dsdl_definitions_from_files(
../pydsdl/pydsdl/_namespace.py:345: in _construct_dsdl_definitions_from_files
    output.add(_dsdl_definition.DSDLDefinition.from_first_in(resolved_fp, list(valid_roots)))
../pydsdl/pydsdl/_dsdl_definition.py:116: in from_first_in
    return cls(dsdl_path, cls._infer_path_to_root_from_first_found(dsdl_path, valid_dsdl_roots))
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <[AttributeError("'DSDLDefinition' object has no attribute '_name'") raised in repr()] DSDLDefinition object at 0x102d46b10>

    def __init__(self, file_path: Path, root_namespace_path: Path):
        """ """
        # Normalizing the path and reading the definition text

...

Suggested change:

def __str__(self) -> str:
  try:
      return "DSDLDefinition(full_name=%r, version=%r, fixed_port_id=%r, file_path=%s)" % (
          self.full_name,
          self.version,
          self.fixed_port_id,
          self.file_path,
      )
  except AttributeError:
      return "DSDLDefinition(UNINITIALIZED)"
thirtytwobits commented 1 month ago

DSDLDefinition example was fixed along with change to fix #109. Keeping this issue open to scrub the code base for similar issues.