Skip to content

str and repr can throw #108

@thirtytwobits

Description

@thirtytwobits

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)"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions