Commit 7b738353 authored by Luke Mewburn's avatar Luke Mewburn Committed by Luke Mewburn
Browse files

compat_asn1: fix non-UTF-8 commits

Handle commits that have UTF-8 BOM or are in Windows-1252 encoding.
parent 644faa03
Loading
Loading
Loading
Loading
+16 −2
Changes for testing/asn/compat_asn1.py: 16 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -22,6 +22,20 @@ TYPE_CONTAINER_OF = (pa_asnobj.TYPE_SEQ_OF, pa_asnobj.TYPE_SET_OF)
logger = logging.getLogger(__name__)


def bytes_to_utf8(data: bytes) -> str:
    """Ensure data is a UTF-8 encoded string.

    Handles issues in legacy commits:
    - Removes UTF-8 BOM.
    - Handles Windows-1252 encoding.
    """
    try:
        raw = data.decode("utf-8-sig")
    except UnicodeDecodeError:
        raw = data.decode("cp1252")  # Fall-back to decode as Windows-1252
    return raw


def dict_swap(dictionary: dict) -> dict:
    """Swap keys and values in `dictionary`.

@@ -168,13 +182,13 @@ class CompatCheck:
            ["git", "cat-file", "-p", f"{commit}:{filename}"],
            capture_output=True,
            check=False,
            text=True,
            text=False,
        )
        if p.returncode != 0:
            raise CompatCheckException(
                f"Error reading git commit '{commit}' for file '{filename}': {p.stderr.strip()}"
            )
        return p.stdout
        return bytes_to_utf8(p.stdout)

    def _read_file(self, filename: str) -> str:
        """Read file `filename` and return text contents."""