Skip to content
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: 2 additions & 0 deletions dissect/cstruct/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from dissect.cstruct.bitbuffer import BitBuffer
from dissect.cstruct.cstruct import cstruct, ctypes, ctypes_type
from dissect.cstruct.exceptions import (
Expand Down
4 changes: 3 additions & 1 deletion dissect/cstruct/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from dissect.cstruct.bitbuffer import BitBuffer
from dissect.cstruct.types import (
Array,
BaseType,
Char,
CharArray,
Flag,
Expand All @@ -34,6 +33,9 @@
from types import MethodType

from dissect.cstruct.cstruct import cstruct
from dissect.cstruct.types import (
BaseType,
)
from dissect.cstruct.types.structure import Field

SUPPORTED_TYPES = (
Expand Down
7 changes: 5 additions & 2 deletions dissect/cstruct/cstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
from dissect.cstruct.parser import CStyleParser, TokenParser
from dissect.cstruct.types import (
LEB128,
Array,
BaseArray,
BaseType,
Char,
Enum,
Field,
Flag,
Int,
Packed,
Expand All @@ -32,6 +30,11 @@
from collections.abc import Iterable
from typing import TypeAlias

from dissect.cstruct.types import (
Array,
Field,
)


T = TypeVar("T", bound=BaseType)

Expand Down
3 changes: 3 additions & 0 deletions dissect/cstruct/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from __future__ import annotations


class Error(Exception):
pass

Expand Down
1 change: 0 additions & 1 deletion dissect/cstruct/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ def is_number(self, token: str) -> bool:

def evaluate(self, cs: cstruct, context: dict[str, int] | None = None) -> int:
"""Evaluates an expression using a Shunting-Yard implementation."""

self.stack = []
self.queue = []
operators = set(self.binary_operators.keys()) | set(self.unary_operators.keys())
Expand Down
11 changes: 6 additions & 5 deletions dissect/cstruct/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
ParserError,
)
from dissect.cstruct.expression import Expression
from dissect.cstruct.types import BaseArray, BaseType, Field, Structure
from dissect.cstruct.types import BaseArray, Field, Structure

if TYPE_CHECKING:
from dissect.cstruct import cstruct
from dissect.cstruct.types import BaseType


class Parser:
Expand All @@ -37,7 +38,8 @@ def parse(self, data: str) -> None:


class TokenParser(Parser):
"""
"""Definition parser for C-like structure syntax.

Args:
cs: An instance of cstruct.
compiled: Whether structs should be compiled or not.
Expand Down Expand Up @@ -421,8 +423,7 @@ def _replacer(match: re.Match) -> str:

@staticmethod
def _lineno(tok: Token) -> int:
"""Quick and dirty line number calculator"""

"""Quick and dirty line number calculator."""
match = tok.match
return match.string.count("\n", 0, match.start()) + 1

Expand Down Expand Up @@ -474,7 +475,7 @@ def parse(self, data: str) -> None:


class CStyleParser(Parser):
"""Definition parser for C-like structure syntax.
"""Definition parser for C-like structure syntax (legacy parser).

Args:
cs: An instance of cstruct
Expand Down
2 changes: 2 additions & 0 deletions dissect/cstruct/types/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from dissect.cstruct.types.base import Array, BaseArray, BaseType, MetaType
from dissect.cstruct.types.char import Char, CharArray
from dissect.cstruct.types.enum import Enum
Expand Down
3 changes: 2 additions & 1 deletion dissect/cstruct/types/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
from enum import EnumMeta, IntEnum, IntFlag
from typing import TYPE_CHECKING, Any, BinaryIO, TypeVar, overload

from dissect.cstruct.types.base import Array, BaseType, MetaType
from dissect.cstruct.types.base import BaseType, MetaType

if TYPE_CHECKING:
from typing_extensions import Self

from dissect.cstruct.cstruct import cstruct
from dissect.cstruct.types.base import Array


PY_311 = sys.version_info >= (3, 11, 0)
Expand Down
6 changes: 3 additions & 3 deletions dissect/cstruct/types/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io
from collections import ChainMap
from collections.abc import Callable, MutableMapping
from collections.abc import MutableMapping
from contextlib import contextmanager
from enum import Enum
from functools import lru_cache
Expand All @@ -23,7 +23,7 @@
from dissect.cstruct.types.pointer import Pointer

if TYPE_CHECKING:
from collections.abc import Iterator, Mapping
from collections.abc import Callable, Iterator, Mapping
from types import FunctionType

from typing_extensions import Self
Expand Down Expand Up @@ -139,7 +139,7 @@ def _update_fields(

if cls.__compiled__:
# If the previous class was compiled try to compile this too
from dissect.cstruct import compiler # noqa: PLC0415
from dissect.cstruct import compiler

try:
classdict["_read"] = compiler.Compiler(cls.cs).compile_read(fields, cls.__name__, align=cls.__align__)
Expand Down
25 changes: 22 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,43 @@ select = [
"SLOT",
"SIM",
"TID",
"TCH",
"TC",
"PTH",
"PLC",
"TRY",
"FLY",
"PERF",
"FURB",
"RUF",
"D"
]
ignore = ["E203", "B904", "UP024", "ANN002", "ANN003", "ANN204", "ANN401", "SIM105", "TRY003"]

ignore = [
"E203", "B904", "UP024", "ANN002", "ANN003", "ANN204", "ANN401", "SIM105", "TRY003", "PLC0415",
# Ignore some pydocstyle rules for now as they require a larger cleanup
"D1",
"D205",
"D301",
"D417",
# Seems bugged: https://github.com/astral-sh/ruff/issues/16824
"D402",
]
future-annotations = true

[tool.ruff.lint.pydocstyle]
convention = "google"

[tool.ruff.lint.flake8-type-checking]
strict = true

[tool.ruff.lint.per-file-ignores]
"tests/**" = ["S101", "PLC0415"]
"tests/**" = ["S101"]
"tests/_docs/**" = ["INP001"]

[tool.ruff.lint.isort]
known-first-party = ["dissect.cstruct"]
known-third-party = ["dissect"]
required-imports = ["from __future__ import annotations"]

[tool.setuptools.packages.find]
include = ["dissect.*"]
Expand Down
2 changes: 2 additions & 0 deletions tests/_docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

project = "dissect.cstruct"

extensions = [
Expand Down
6 changes: 3 additions & 3 deletions tests/test_types_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def test_union_default(cs: cstruct) -> None:


def test_union_default_dynamic(cs: cstruct) -> None:
"""initialization of a dynamic union is not yet supported"""
"""Initialization of a dynamic union is not yet supported."""
cdef = """
union test {
uint8 x;
Expand Down Expand Up @@ -472,7 +472,7 @@ def test_union_default_dynamic(cs: cstruct) -> None:


def test_union_partial_initialization(cs: cstruct) -> None:
"""partial initialization of a union should fill in the rest with appropriate values"""
"""Partial initialization of a union should fill in the rest with appropriate values."""
cdef = """
union test {
uint8 a;
Expand Down Expand Up @@ -518,7 +518,7 @@ def test_union_partial_initialization(cs: cstruct) -> None:


def test_union_partial_initialization_dynamic(cs: cstruct) -> None:
"""partial initialization of a dynamic union should fill in the rest with appropriate values"""
"""Partial initialization of a dynamic union should fill in the rest with appropriate values."""
cdef = """
union test {
uint8 x;
Expand Down
Loading