Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Python 2 compatible exception code #930

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dissect/target/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions dissect/target/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import os
import sys
import traceback
Expand All @@ -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


Expand Down
4 changes: 2 additions & 2 deletions dissect/target/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,7 @@
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)

Expand Down Expand Up @@ -1715,7 +1715,7 @@
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

Check warning on line 1718 in dissect/target/filesystem.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystem.py#L1718

Added line #L1718 was not covered by tests
finally:
fh.seek(offset)

Expand Down
8 changes: 4 additions & 4 deletions dissect/target/filesystems/btrfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@
try:
return self.subvolume.get(path, node)
except btrfs.FileNotFoundError as e:
raise FileNotFoundError(path, cause=e)
raise FileNotFoundError(path) from e

Check warning on line 82 in dissect/target/filesystems/btrfs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/btrfs.py#L82

Added line #L82 was not covered by tests
except btrfs.NotADirectoryError as e:
raise NotADirectoryError(path, cause=e)
raise NotADirectoryError(path) from e

Check warning on line 84 in dissect/target/filesystems/btrfs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/btrfs.py#L84

Added line #L84 was not covered by tests
except btrfs.NotASymlinkError as e:
raise NotASymlinkError(path, cause=e)
raise NotASymlinkError(path) from e

Check warning on line 86 in dissect/target/filesystems/btrfs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/btrfs.py#L86

Added line #L86 was not covered by tests
except btrfs.Error as e:
raise FileNotFoundError(path, cause=e)
raise FileNotFoundError(path) from e

Check warning on line 88 in dissect/target/filesystems/btrfs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/btrfs.py#L88

Added line #L88 was not covered by tests


class BtrfsFilesystemEntry(FilesystemEntry):
Expand Down
8 changes: 4 additions & 4 deletions dissect/target/filesystems/extfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
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

Check warning on line 38 in dissect/target/filesystems/extfs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/extfs.py#L38

Added line #L38 was not covered by tests
except extfs.NotASymlinkError as e:
raise NotASymlinkError(path, cause=e)
raise NotASymlinkError(path) from e

Check warning on line 40 in dissect/target/filesystems/extfs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/extfs.py#L40

Added line #L40 was not covered by tests
except extfs.Error as e:
raise FileNotFoundError(path, cause=e)
raise FileNotFoundError(path) from e

Check warning on line 42 in dissect/target/filesystems/extfs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/extfs.py#L42

Added line #L42 was not covered by tests


class ExtFilesystemEntry(FilesystemEntry):
Expand Down
6 changes: 3 additions & 3 deletions dissect/target/filesystems/fat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions dissect/target/filesystems/ffs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
try:
return self.ffs.get(path, node)
except ffs.FileNotFoundError as e:
raise FileNotFoundError(path, cause=e)
raise FileNotFoundError(path) from e

Check warning on line 42 in dissect/target/filesystems/ffs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/ffs.py#L42

Added line #L42 was not covered by tests
except ffs.NotADirectoryError as e:
raise NotADirectoryError(path, cause=e)
raise NotADirectoryError(path) from e

Check warning on line 44 in dissect/target/filesystems/ffs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/ffs.py#L44

Added line #L44 was not covered by tests
except ffs.NotASymlinkError as e:
raise NotASymlinkError(path, cause=e)
raise NotASymlinkError(path) from e

Check warning on line 46 in dissect/target/filesystems/ffs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/ffs.py#L46

Added line #L46 was not covered by tests
except ffs.Error as e:
raise FileNotFoundError(path, cause=e)
raise FileNotFoundError(path) from e

Check warning on line 48 in dissect/target/filesystems/ffs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/ffs.py#L48

Added line #L48 was not covered by tests


class FfsFilesystemEntry(FilesystemEntry):
Expand Down
8 changes: 4 additions & 4 deletions dissect/target/filesystems/jffs.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
try:
return self.jffs2.get(path, node)
except jffs2.FileNotFoundError as e:
raise FileNotFoundError(path, cause=e)
raise FileNotFoundError(path) from e

Check warning on line 38 in dissect/target/filesystems/jffs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/jffs.py#L38

Added line #L38 was not covered by tests
except jffs2.NotADirectoryError as e:
raise NotADirectoryError(path, cause=e)
raise NotADirectoryError(path) from e

Check warning on line 40 in dissect/target/filesystems/jffs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/jffs.py#L40

Added line #L40 was not covered by tests
except jffs2.NotASymlinkError as e:
raise NotASymlinkError(path, cause=e)
raise NotASymlinkError(path) from e

Check warning on line 42 in dissect/target/filesystems/jffs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/jffs.py#L42

Added line #L42 was not covered by tests
except jffs2.Error as e:
raise FileNotFoundError(path, cause=e)
raise FileNotFoundError(path) from e

Check warning on line 44 in dissect/target/filesystems/jffs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/jffs.py#L44

Added line #L44 was not covered by tests


class JFFSFilesystemEntry(FilesystemEntry):
Expand Down
8 changes: 4 additions & 4 deletions dissect/target/filesystems/ntfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
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

Check warning on line 52 in dissect/target/filesystems/ntfs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/ntfs.py#L52

Added line #L52 was not covered by tests
except NtfsNotADirectoryError as e:
raise NotADirectoryError(path, cause=e)
raise NotADirectoryError(path) from e

Check warning on line 54 in dissect/target/filesystems/ntfs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/ntfs.py#L54

Added line #L54 was not covered by tests
except NtfsError as e:
raise FileNotFoundError(path, cause=e)
raise FileNotFoundError(path) from e


class NtfsFilesystemEntry(FilesystemEntry):
Expand Down
6 changes: 3 additions & 3 deletions dissect/target/filesystems/smb.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
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

Check warning on line 61 in dissect/target/filesystems/smb.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/smb.py#L61

Added line #L61 was not covered by tests
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

Check warning on line 64 in dissect/target/filesystems/smb.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/smb.py#L64

Added line #L64 was not covered by tests

if len(result) != 1:
raise FileNotFoundError(path)
Expand Down Expand Up @@ -106,7 +106,7 @@
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

Check warning on line 109 in dissect/target/filesystems/smb.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/smb.py#L109

Added line #L109 was not covered by tests

def is_dir(self, follow_symlinks: bool = True) -> bool:
try:
Expand Down
8 changes: 4 additions & 4 deletions dissect/target/filesystems/squashfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
try:
return self.squashfs.get(path, node)
except exceptions.FileNotFoundError as e:
raise FileNotFoundError(path, cause=e)
raise FileNotFoundError(path) from e

Check warning on line 35 in dissect/target/filesystems/squashfs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/squashfs.py#L35

Added line #L35 was not covered by tests
except exceptions.NotADirectoryError as e:
raise NotADirectoryError(path, cause=e)
raise NotADirectoryError(path) from e

Check warning on line 37 in dissect/target/filesystems/squashfs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/squashfs.py#L37

Added line #L37 was not covered by tests
except exceptions.NotASymlinkError as e:
raise NotASymlinkError(path, cause=e)
raise NotASymlinkError(path) from e

Check warning on line 39 in dissect/target/filesystems/squashfs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/squashfs.py#L39

Added line #L39 was not covered by tests
except exceptions.Error as e:
raise FileNotFoundError(path, cause=e)
raise FileNotFoundError(path) from e

Check warning on line 41 in dissect/target/filesystems/squashfs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/squashfs.py#L41

Added line #L41 was not covered by tests


class SquashFSFilesystemEntry(FilesystemEntry):
Expand Down
8 changes: 4 additions & 4 deletions dissect/target/filesystems/vmfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@
try:
return self.vmfs.get(path, node)
except vmfs.FileNotFoundError as e:
raise FileNotFoundError(path, cause=e)
raise FileNotFoundError(path) from e

Check warning on line 44 in dissect/target/filesystems/vmfs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/vmfs.py#L44

Added line #L44 was not covered by tests
except vmfs.NotADirectoryError as e:
raise NotADirectoryError(path, cause=e)
raise NotADirectoryError(path) from e

Check warning on line 46 in dissect/target/filesystems/vmfs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/vmfs.py#L46

Added line #L46 was not covered by tests
except vmfs.NotASymlinkError as e:
raise NotASymlinkError(path, cause=e)
raise NotASymlinkError(path) from e

Check warning on line 48 in dissect/target/filesystems/vmfs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/vmfs.py#L48

Added line #L48 was not covered by tests
except vmfs.Error as e:
raise FileNotFoundError(path, cause=e)
raise FileNotFoundError(path) from e

Check warning on line 50 in dissect/target/filesystems/vmfs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/vmfs.py#L50

Added line #L50 was not covered by tests


class VmfsFilesystemEntry(FilesystemEntry):
Expand Down
14 changes: 7 additions & 7 deletions dissect/target/filesystems/xfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
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

Check warning on line 43 in dissect/target/filesystems/xfs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/xfs.py#L38-L43

Added lines #L38 - L43 were not covered by tests
except xfs.Error as e:
raise FileNotFoundError(path, cause=e)
raise FileNotFoundError(path) from e

Check warning on line 45 in dissect/target/filesystems/xfs.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/xfs.py#L45

Added line #L45 was not covered by tests


class XfsFilesystemEntry(FilesystemEntry):
Expand Down
9 changes: 5 additions & 4 deletions dissect/target/helpers/regutil.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Registry related abstractions """
"""Registry related abstractions"""

from __future__ import annotations

import fnmatch
Expand Down Expand Up @@ -645,7 +646,7 @@
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):
Expand Down Expand Up @@ -675,7 +676,7 @@
try:
return RegfKey(self.hive, self.key.subkey(subkey))
except regf.RegistryKeyNotFoundError as e:
raise RegistryKeyNotFoundError(subkey, cause=e)
raise RegistryKeyNotFoundError(subkey) from e

Check warning on line 679 in dissect/target/helpers/regutil.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/regutil.py#L679

Added line #L679 was not covered by tests

def subkeys(self) -> list[RegistryKey]:
return [RegfKey(self.hive, k) for k in self.key.subkeys()]
Expand All @@ -684,7 +685,7 @@
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()]
Expand Down
2 changes: 1 addition & 1 deletion dissect/target/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@
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

Check warning on line 810 in dissect/target/plugin.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/plugin.py#L810

Added line #L810 was not covered by tests


def failed() -> list[dict[str, Any]]:
Expand Down
13 changes: 6 additions & 7 deletions dissect/target/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@
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

Check warning on line 239 in dissect/target/target.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/target.py#L239

Added line #L239 was not covered by tests
return cls._load(path, loader_instance)
return cls.open_raw(path)

Expand Down Expand Up @@ -428,7 +428,7 @@
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

Check warning on line 431 in dissect/target/target.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/target.py#L431

Added line #L431 was not covered by tests

def _init_os(self) -> None:
"""Internal function that attemps to load an OSPlugin for this target."""
Expand Down Expand Up @@ -541,7 +541,7 @@
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

Expand All @@ -556,8 +556,8 @@
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)

Expand Down Expand Up @@ -614,9 +614,8 @@
# 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:
Expand Down
6 changes: 3 additions & 3 deletions dissect/target/tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions dissect/target/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@
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)

Expand All @@ -353,7 +353,7 @@
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

Check warning on line 356 in dissect/target/volume.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/volume.py#L356

Added line #L356 was not covered by tests

return False

Expand All @@ -372,7 +372,7 @@
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

Check warning on line 375 in dissect/target/volume.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/volume.py#L375

Added line #L375 was not covered by tests
return False


Expand Down Expand Up @@ -422,4 +422,4 @@
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

Check warning on line 425 in dissect/target/volume.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/volume.py#L425

Added line #L425 was not covered by tests
Loading