diff --git a/astrbot/core/utils/pip_installer.py b/astrbot/core/utils/pip_installer.py index 6076a114a..663afc081 100644 --- a/astrbot/core/utils/pip_installer.py +++ b/astrbot/core/utils/pip_installer.py @@ -1,10 +1,29 @@ import asyncio +import locale import logging import sys logger = logging.getLogger("astrbot") +def _robust_decode(line: bytes) -> str: + """解码字节流,兼容不同平台的编码""" + try: + return line.decode("utf-8").strip() + except UnicodeDecodeError: + pass + try: + return line.decode(locale.getpreferredencoding(False)).strip() + except UnicodeDecodeError: + pass + if sys.platform.startswith("win"): + try: + return line.decode("gbk").strip() + except UnicodeDecodeError: + pass + return line.decode("utf-8", errors="replace").strip() + + class PipInstaller: def __init__(self, pip_install_arg: str, pypi_index_url: str | None = None): self.pip_install_arg = pip_install_arg @@ -42,7 +61,7 @@ async def install( assert process.stdout is not None async for line in process.stdout: - logger.info(line.decode().strip()) + logger.info(_robust_decode(line)) await process.wait()