From 2e7fc1720d5acd6a426bf607add4e51cf358a4ff Mon Sep 17 00:00:00 2001 From: imSp4rky Date: Sun, 12 Apr 2026 22:19:44 +0000 Subject: [PATCH] fix(drm): handle non-UTF-8 output from shaka-packager stderr Shaka-packager can emit non-UTF-8 bytes in its log output, causing UnicodeDecodeError when reading stderr in text mode. Use explicit errors="replace" encoding. Also harden try_ensure_utf8 fallback paths to always return valid UTF-8 instead of raw bytes. --- unshackle/core/drm/playready.py | 4 +++- unshackle/core/utilities.py | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/unshackle/core/drm/playready.py b/unshackle/core/drm/playready.py index 4b444a9..cbf4a96 100644 --- a/unshackle/core/drm/playready.py +++ b/unshackle/core/drm/playready.py @@ -421,7 +421,9 @@ class PlayReady: [binaries.ShakaPackager, *arguments], stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, - universal_newlines=True, + text=True, + encoding="utf-8", + errors="replace", ) stream_skipped = False diff --git a/unshackle/core/utilities.py b/unshackle/core/utilities.py index 572ee4e..fad62d9 100644 --- a/unshackle/core/utilities.py +++ b/unshackle/core/utilities.py @@ -516,10 +516,10 @@ def try_ensure_utf8(data: bytes) -> bytes: # last ditch effort to detect encoding detection_result = chardet.detect(data) if not detection_result["encoding"]: - return data - return data.decode(detection_result["encoding"]).encode("utf8") - except UnicodeDecodeError: - return data + return data.decode("utf-8", errors="replace").encode("utf-8") + return data.decode(detection_result["encoding"], errors="replace").encode("utf8") + except (UnicodeDecodeError, LookupError): + return data.decode("utf-8", errors="replace").encode("utf-8") def get_free_port() -> int: