Handle mixed command output encodings
This commit is contained in:
@@ -837,13 +837,23 @@ def _run_process(
|
||||
stdin=input_file,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
text=True,
|
||||
shell=False,
|
||||
)
|
||||
assert process.stdout is not None
|
||||
for line in process.stdout:
|
||||
on_output(line.rstrip())
|
||||
for raw_line in process.stdout:
|
||||
on_output(_decode_process_output(raw_line).rstrip())
|
||||
return process.wait()
|
||||
finally:
|
||||
if input_file:
|
||||
input_file.close()
|
||||
|
||||
|
||||
def _decode_process_output(value: bytes | str) -> str:
|
||||
if isinstance(value, str):
|
||||
return value
|
||||
for encoding in ("utf-8", "gb18030", "mbcs"):
|
||||
try:
|
||||
return value.decode(encoding)
|
||||
except (LookupError, UnicodeDecodeError):
|
||||
continue
|
||||
return value.decode("utf-8", errors="replace")
|
||||
|
||||
Reference in New Issue
Block a user