From 50bece383b3aaf54f563098d512a5df5532b1418 Mon Sep 17 00:00:00 2001 From: Schamper <1254028+Schamper@users.noreply.github.com> Date: Thu, 31 Oct 2024 10:40:47 +0100 Subject: [PATCH] Remove Python 2 compatible exception code --- dissect/target/container.py | 2 +- dissect/target/exceptions.py | 5 +++-- dissect/target/filesystem.py | 4 ++-- dissect/target/filesystems/btrfs.py | 8 ++++---- dissect/target/filesystems/extfs.py | 8 ++++---- dissect/target/filesystems/fat.py | 6 +++--- dissect/target/filesystems/ffs.py | 8 ++++---- dissect/target/filesystems/jffs.py | 8 ++++---- dissect/target/filesystems/ntfs.py | 8 ++++---- dissect/target/filesystems/smb.py | 6 +++--- dissect/target/filesystems/squashfs.py | 8 ++++---- dissect/target/filesystems/vmfs.py | 8 ++++---- dissect/target/filesystems/xfs.py | 14 +++++++------- dissect/target/helpers/regutil.py | 9 +++++---- dissect/target/plugin.py | 2 +- dissect/target/target.py | 13 ++++++------- dissect/target/tools/utils.py | 6 +++--- dissect/target/volume.py | 8 ++++---- 18 files changed, 66 insertions(+), 65 deletions(-) diff --git a/dissect/target/container.py b/dissect/target/container.py index 2e24af3e5..90a5d60c2 100644 --- a/dissect/target/container.py +++ b/dissect/target/container.py @@ -239,7 +239,7 @@ def open(item: Union[list, str, BinaryIO, Path], *args, **kwargs): log.info("Failed to import %s", container) log.debug("", exc_info=e) except Exception as e: - raise ContainerError(f"Failed to open container {item}", cause=e) + raise ContainerError(f"Failed to open container {item}") from e finally: if first_fh_opened: first_fh.close() diff --git a/dissect/target/exceptions.py b/dissect/target/exceptions.py index aea713290..e52d23f25 100644 --- a/dissect/target/exceptions.py +++ b/dissect/target/exceptions.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import os import sys import traceback @@ -7,13 +9,12 @@ class Error(Exception): """Generic dissect.target error""" - def __init__(self, message=None, cause=None, extra=None): + def __init__(self, message: str | None = None, extra: list[Exception] | None = None): if extra: exceptions = "\n\n".join(["".join(traceback.format_exception_only(type(e), e)) for e in extra]) message = f"{message}\n\nAdditionally, the following exceptions occurred:\n\n{exceptions}" super().__init__(message) - self.__cause__ = cause self.__extra__ = extra diff --git a/dissect/target/filesystem.py b/dissect/target/filesystem.py index e22c099ec..9e1d8c166 100644 --- a/dissect/target/filesystem.py +++ b/dissect/target/filesystem.py @@ -1142,7 +1142,7 @@ def get(self, path: str, relentry: Optional[FilesystemEntry] = None) -> Filesyst try: return entry.top.get(fsutil.join(*parts[i:], alt_separator=self.alt_separator)) except FilesystemError as e: - raise FileNotFoundError(full_path, cause=e) + raise FileNotFoundError(full_path) from e else: raise FileNotFoundError(full_path) @@ -1715,7 +1715,7 @@ def open(fh: BinaryIO, *args, **kwargs) -> Filesystem: log.info("Failed to import %s", filesystem) log.debug("", exc_info=e) except Exception as e: - raise FilesystemError(f"Failed to open filesystem for {fh}", cause=e) + raise FilesystemError(f"Failed to open filesystem for {fh}") from e finally: fh.seek(offset) diff --git a/dissect/target/filesystems/btrfs.py b/dissect/target/filesystems/btrfs.py index e24ad08bc..2d1fbc55e 100644 --- a/dissect/target/filesystems/btrfs.py +++ b/dissect/target/filesystems/btrfs.py @@ -79,13 +79,13 @@ def _get_node(self, path: str, node: Optional[btrfs.INode] = None) -> btrfs.INod try: return self.subvolume.get(path, node) except btrfs.FileNotFoundError as e: - raise FileNotFoundError(path, cause=e) + raise FileNotFoundError(path) from e except btrfs.NotADirectoryError as e: - raise NotADirectoryError(path, cause=e) + raise NotADirectoryError(path) from e except btrfs.NotASymlinkError as e: - raise NotASymlinkError(path, cause=e) + raise NotASymlinkError(path) from e except btrfs.Error as e: - raise FileNotFoundError(path, cause=e) + raise FileNotFoundError(path) from e class BtrfsFilesystemEntry(FilesystemEntry): diff --git a/dissect/target/filesystems/extfs.py b/dissect/target/filesystems/extfs.py index e9334e9cb..d015a83bf 100644 --- a/dissect/target/filesystems/extfs.py +++ b/dissect/target/filesystems/extfs.py @@ -33,13 +33,13 @@ def _get_node(self, path: str, node: Optional[extfs.INode] = None) -> extfs.INod try: return self.extfs.get(path, node) except extfs.FileNotFoundError as e: - raise FileNotFoundError(path, cause=e) + raise FileNotFoundError(path) from e except extfs.NotADirectoryError as e: - raise NotADirectoryError(path, cause=e) + raise NotADirectoryError(path) from e except extfs.NotASymlinkError as e: - raise NotASymlinkError(path, cause=e) + raise NotASymlinkError(path) from e except extfs.Error as e: - raise FileNotFoundError(path, cause=e) + raise FileNotFoundError(path) from e class ExtFilesystemEntry(FilesystemEntry): diff --git a/dissect/target/filesystems/fat.py b/dissect/target/filesystems/fat.py index a204345c3..739528833 100644 --- a/dissect/target/filesystems/fat.py +++ b/dissect/target/filesystems/fat.py @@ -41,11 +41,11 @@ def _get_entry( try: return self.fatfs.get(path, dirent=entry) except fat_exc.FileNotFoundError as e: - raise FileNotFoundError(path, cause=e) + raise FileNotFoundError(path) from e except fat_exc.NotADirectoryError as e: - raise NotADirectoryError(path, cause=e) + raise NotADirectoryError(path) from e except fat_exc.Error as e: - raise FileNotFoundError(path, cause=e) + raise FileNotFoundError(path) from e class FatFilesystemEntry(FilesystemEntry): diff --git a/dissect/target/filesystems/ffs.py b/dissect/target/filesystems/ffs.py index c0912dc42..acc54a702 100644 --- a/dissect/target/filesystems/ffs.py +++ b/dissect/target/filesystems/ffs.py @@ -39,13 +39,13 @@ def _get_node(self, path: str, node: Optional[ffs.INode] = None) -> ffs.INode: try: return self.ffs.get(path, node) except ffs.FileNotFoundError as e: - raise FileNotFoundError(path, cause=e) + raise FileNotFoundError(path) from e except ffs.NotADirectoryError as e: - raise NotADirectoryError(path, cause=e) + raise NotADirectoryError(path) from e except ffs.NotASymlinkError as e: - raise NotASymlinkError(path, cause=e) + raise NotASymlinkError(path) from e except ffs.Error as e: - raise FileNotFoundError(path, cause=e) + raise FileNotFoundError(path) from e class FfsFilesystemEntry(FilesystemEntry): diff --git a/dissect/target/filesystems/jffs.py b/dissect/target/filesystems/jffs.py index 2d0122940..cb2e7e3d7 100644 --- a/dissect/target/filesystems/jffs.py +++ b/dissect/target/filesystems/jffs.py @@ -35,13 +35,13 @@ def _get_node(self, path: str, node: Optional[jffs2.INode] = None) -> jffs2.INod try: return self.jffs2.get(path, node) except jffs2.FileNotFoundError as e: - raise FileNotFoundError(path, cause=e) + raise FileNotFoundError(path) from e except jffs2.NotADirectoryError as e: - raise NotADirectoryError(path, cause=e) + raise NotADirectoryError(path) from e except jffs2.NotASymlinkError as e: - raise NotASymlinkError(path, cause=e) + raise NotASymlinkError(path) from e except jffs2.Error as e: - raise FileNotFoundError(path, cause=e) + raise FileNotFoundError(path) from e class JFFSFilesystemEntry(FilesystemEntry): diff --git a/dissect/target/filesystems/ntfs.py b/dissect/target/filesystems/ntfs.py index e384a6908..a54442cbe 100644 --- a/dissect/target/filesystems/ntfs.py +++ b/dissect/target/filesystems/ntfs.py @@ -48,12 +48,12 @@ def _get_record(self, path: str, root: Optional[MftRecord] = None) -> MftRecord: try: path = path.rsplit(":", maxsplit=1)[0] return self.ntfs.mft.get(path, root=root) - except NtfsFileNotFoundError: - raise FileNotFoundError(path) + except NtfsFileNotFoundError as e: + raise FileNotFoundError(path) from e except NtfsNotADirectoryError as e: - raise NotADirectoryError(path, cause=e) + raise NotADirectoryError(path) from e except NtfsError as e: - raise FileNotFoundError(path, cause=e) + raise FileNotFoundError(path) from e class NtfsFilesystemEntry(FilesystemEntry): diff --git a/dissect/target/filesystems/smb.py b/dissect/target/filesystems/smb.py index d00de8537..f90e9da68 100644 --- a/dissect/target/filesystems/smb.py +++ b/dissect/target/filesystems/smb.py @@ -58,10 +58,10 @@ def _get_entry(self, path: str) -> SharedFile: except SessionError as e: if e.error == STATUS_NOT_A_DIRECTORY: # STATUS_NOT_A_DIRECTORY - raise NotADirectoryError(path, cause=e) + raise NotADirectoryError(path) from e else: # 0xC000000F is STATUS_NO_SUCH_FILE, but everything else should raise a FileNotFoundError anyway - raise FileNotFoundError(path, cause=e) + raise FileNotFoundError(path) from e if len(result) != 1: raise FileNotFoundError(path) @@ -106,7 +106,7 @@ def open(self) -> SmbStream: try: return SmbStream(self.fs.conn, self.fs.share_name, self.path, self.entry.get_filesize()) except SessionError as e: - raise FilesystemError(f"Failed to open file: {self.path}", cause=e) + raise FilesystemError(f"Failed to open file: {self.path}") from e def is_dir(self, follow_symlinks: bool = True) -> bool: try: diff --git a/dissect/target/filesystems/squashfs.py b/dissect/target/filesystems/squashfs.py index af378a242..d0323b3bc 100644 --- a/dissect/target/filesystems/squashfs.py +++ b/dissect/target/filesystems/squashfs.py @@ -32,13 +32,13 @@ def _get_node(self, path: str, node: Optional[INode] = None) -> INode: try: return self.squashfs.get(path, node) except exceptions.FileNotFoundError as e: - raise FileNotFoundError(path, cause=e) + raise FileNotFoundError(path) from e except exceptions.NotADirectoryError as e: - raise NotADirectoryError(path, cause=e) + raise NotADirectoryError(path) from e except exceptions.NotASymlinkError as e: - raise NotASymlinkError(path, cause=e) + raise NotASymlinkError(path) from e except exceptions.Error as e: - raise FileNotFoundError(path, cause=e) + raise FileNotFoundError(path) from e class SquashFSFilesystemEntry(FilesystemEntry): diff --git a/dissect/target/filesystems/vmfs.py b/dissect/target/filesystems/vmfs.py index 64503b43a..66d001f81 100644 --- a/dissect/target/filesystems/vmfs.py +++ b/dissect/target/filesystems/vmfs.py @@ -41,13 +41,13 @@ def _get_node(self, path: str, node: Optional[FileDescriptor] = None) -> FileDes try: return self.vmfs.get(path, node) except vmfs.FileNotFoundError as e: - raise FileNotFoundError(path, cause=e) + raise FileNotFoundError(path) from e except vmfs.NotADirectoryError as e: - raise NotADirectoryError(path, cause=e) + raise NotADirectoryError(path) from e except vmfs.NotASymlinkError as e: - raise NotASymlinkError(path, cause=e) + raise NotASymlinkError(path) from e except vmfs.Error as e: - raise FileNotFoundError(path, cause=e) + raise FileNotFoundError(path) from e class VmfsFilesystemEntry(FilesystemEntry): diff --git a/dissect/target/filesystems/xfs.py b/dissect/target/filesystems/xfs.py index d63af1c2d..68099399b 100644 --- a/dissect/target/filesystems/xfs.py +++ b/dissect/target/filesystems/xfs.py @@ -35,14 +35,14 @@ def get(self, path: str) -> FilesystemEntry: def _get_node(self, path: str, node: Optional[xfs.INode] = None) -> xfs.INode: try: return self.xfs.get(path, node) - except xfs.FileNotFoundError: - raise FileNotFoundError(path) - except xfs.NotADirectoryError: - raise NotADirectoryError(path) - except xfs.NotASymlinkError: - raise NotASymlinkError(path) + except xfs.FileNotFoundError as e: + raise FileNotFoundError(path) from e + except xfs.NotADirectoryError as e: + raise NotADirectoryError(path) from e + except xfs.NotASymlinkError as e: + raise NotASymlinkError(path) from e except xfs.Error as e: - raise FileNotFoundError(path, cause=e) + raise FileNotFoundError(path) from e class XfsFilesystemEntry(FilesystemEntry): diff --git a/dissect/target/helpers/regutil.py b/dissect/target/helpers/regutil.py index 24dbf1beb..f55fcb8cc 100644 --- a/dissect/target/helpers/regutil.py +++ b/dissect/target/helpers/regutil.py @@ -1,4 +1,5 @@ -""" Registry related abstractions """ +"""Registry related abstractions""" + from __future__ import annotations import fnmatch @@ -645,7 +646,7 @@ def key(self, key: str) -> RegistryKey: try: return RegfKey(self, self.hive.open(key)) except regf.RegistryKeyNotFoundError as e: - raise RegistryKeyNotFoundError(key, cause=e) + raise RegistryKeyNotFoundError(key) from e class RegfKey(RegistryKey): @@ -675,7 +676,7 @@ def subkey(self, subkey: str) -> RegistryKey: try: return RegfKey(self.hive, self.key.subkey(subkey)) except regf.RegistryKeyNotFoundError as e: - raise RegistryKeyNotFoundError(subkey, cause=e) + raise RegistryKeyNotFoundError(subkey) from e def subkeys(self) -> list[RegistryKey]: return [RegfKey(self.hive, k) for k in self.key.subkeys()] @@ -684,7 +685,7 @@ def value(self, value: str) -> RegistryValue: try: return RegfValue(self.hive, self.key.value(value)) except regf.RegistryValueNotFoundError as e: - raise RegistryValueNotFoundError(value, cause=e) + raise RegistryValueNotFoundError(value) from e def values(self) -> list[RegistryValue]: return [RegfValue(self.hive, v) for v in self.key.values()] diff --git a/dissect/target/plugin.py b/dissect/target/plugin.py index b15c91e8f..7b4fcaab5 100644 --- a/dissect/target/plugin.py +++ b/dissect/target/plugin.py @@ -807,7 +807,7 @@ def load(plugin_desc: PluginDescriptor) -> Type[Plugin]: module = importlib.import_module(module) return getattr(module, plugin_desc["class"]) except Exception as e: - raise PluginError(f"An exception occurred while trying to load a plugin: {module}", cause=e) + raise PluginError(f"An exception occurred while trying to load a plugin: {module}") from e def failed() -> list[dict[str, Any]]: diff --git a/dissect/target/target.py b/dissect/target/target.py index 9d581e26a..c3a2ab516 100644 --- a/dissect/target/target.py +++ b/dissect/target/target.py @@ -236,7 +236,7 @@ def open(cls, path: Union[str, Path]) -> Target: try: loader_instance = loader_cls(path, parsed_path=parsed_path) except Exception as e: - raise TargetError(f"Failed to initiate {loader_cls.__name__} for target {path}: {e}", cause=e) + raise TargetError(f"Failed to initiate {loader_cls.__name__} for target {path}: {e}") from e return cls._load(path, loader_instance) return cls.open_raw(path) @@ -428,7 +428,7 @@ def _load(cls, path: Union[str, Path], ldr: loader.Loader) -> Target: target.apply() return target except Exception as e: - raise TargetError(f"Failed to load target: {path}", cause=e) + raise TargetError(f"Failed to load target: {path}") from e def _init_os(self) -> None: """Internal function that attemps to load an OSPlugin for this target.""" @@ -541,7 +541,7 @@ def add_plugin( except PluginError: raise except Exception as e: - raise PluginError(f"An exception occurred while trying to initialize a plugin: {plugin_cls}", cause=e) + raise PluginError(f"An exception occurred while trying to initialize a plugin: {plugin_cls}") from e else: p = plugin_cls @@ -556,8 +556,8 @@ def add_plugin( raise except Exception as e: raise UnsupportedPluginError( - f"An exception occurred while checking for plugin compatibility: {plugin_cls}", cause=e - ) + f"An exception occurred while checking for plugin compatibility: {plugin_cls}" + ) from e self._register_plugin_functions(p) @@ -614,9 +614,8 @@ def get_function(self, function: str) -> FunctionTuple: # Just take the last known cause for now raise UnsupportedPluginError( f"Unsupported function `{function}` for target with OS plugin {self._os_plugin}", - cause=causes[0] if causes else None, extra=causes[1:] if len(causes) > 1 else None, - ) + ) from causes[0] if causes else None # We still ended up with no compatible plugins if function not in self._functions: diff --git a/dissect/target/tools/utils.py b/dissect/target/tools/utils.py index eddb8d5c0..8dcac009a 100644 --- a/dissect/target/tools/utils.py +++ b/dissect/target/tools/utils.py @@ -230,15 +230,15 @@ def get_target_attribute(target: Target, func: PluginFunction) -> Union[Plugin, target.add_plugin(plugin_class) except UnsupportedPluginError as e: raise UnsupportedPluginError( - f"Unsupported function `{func.method_name}` for target with plugin {func.class_object}", cause=e - ) + f"Unsupported function `{func.method_name}` for target with plugin {func.class_object}" + ) from e _, target_attr = plugin_factory(target, plugin_class, func.method_name, func.plugin_desc["namespace"]) return target_attr def plugin_function_with_argparser( - target_attr: Union[Plugin, Callable] + target_attr: Union[Plugin, Callable], ) -> tuple[Optional[Iterator], Optional[argparse.ArgumentParser]]: """Resolves which plugin function to execute, and creates the argument parser for said plugin.""" plugin_method = None diff --git a/dissect/target/volume.py b/dissect/target/volume.py index 93b61f0ac..76dffcaf6 100644 --- a/dissect/target/volume.py +++ b/dissect/target/volume.py @@ -334,7 +334,7 @@ def open(fh: BinaryIO, *args, **kwargs) -> DissectVolumeSystem: try: return disk.DissectVolumeSystem(fh) except Exception as e: - raise VolumeSystemError(f"Failed to load volume system for {fh}", cause=e) + raise VolumeSystemError(f"Failed to load volume system for {fh}") from e finally: fh.seek(offset) @@ -353,7 +353,7 @@ def is_lvm_volume(volume: BinaryIO) -> bool: log.info("Failed to import %s", logical_vs) log.debug("", exc_info=e) except Exception as e: - raise VolumeSystemError(f"Failed to detect logical volume for {volume}", cause=e) + raise VolumeSystemError(f"Failed to detect logical volume for {volume}") from e return False @@ -372,7 +372,7 @@ def is_encrypted(volume: BinaryIO) -> bool: log.info("Failed to import %s", manager) log.debug("", exc_info=e) except Exception as e: - raise VolumeSystemError(f"Failed to detect encrypted volume for {volume}", cause=e) + raise VolumeSystemError(f"Failed to detect encrypted volume for {volume}") from e return False @@ -422,4 +422,4 @@ def open_lvm(volumes: list[BinaryIO], *args, **kwargs) -> Iterator[VolumeSystem] log.info("Failed to import %s", logical_vs) log.debug("", exc_info=e) except Exception as e: - raise VolumeSystemError(f"Failed to load logical volume system for {volumes}", cause=e) + raise VolumeSystemError(f"Failed to load logical volume system for {volumes}") from e