Skip to content
Merged
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
29 changes: 25 additions & 4 deletions web/pgadmin/misc/bgprocess/process_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import sys
import os
import subprocess
import locale
from datetime import datetime, timedelta, tzinfo, timezone
from subprocess import Popen, PIPE
from threading import Thread
Expand All @@ -45,6 +46,7 @@
_fs_encoding = None
_out_dir = None
_log_file = None
_subprocess_encoding = None


def _log(msg):
Expand Down Expand Up @@ -191,7 +193,7 @@ def log(self, msg):
This function will update log file

Args:
msg: message
msg: message (bytes from subprocess)

Returns:
None
Expand All @@ -205,9 +207,22 @@ def log(self, msg):
).encode('utf-8')
)
self.logger.write(b',')
self.logger.write(
msg.lstrip(b'\r\n' if _IS_WIN else b'\n')
)

# Convert subprocess output from system encoding to UTF-8
# This fixes garbled text on Windows with non-UTF-8 locales
# (e.g., Japanese CP932, Chinese GBK)
msg = msg.lstrip(b'\r\n' if _IS_WIN else b'\n')
if _subprocess_encoding and \
_subprocess_encoding.lower() not in ('utf-8', 'utf8'):
try:
msg = msg.decode(
_subprocess_encoding, 'replace'
).encode('utf-8')
except (UnicodeDecodeError, LookupError):
# If decoding fails, write as-is
pass

self.logger.write(msg)
self.logger.write(os.linesep.encode('utf-8'))

return True
Expand Down Expand Up @@ -427,6 +442,12 @@ def convert_environment_variables(env):
# encoding or 'ascii'.
_fs_encoding = 'utf-8'

# Detect subprocess output encoding (important for Windows with non-UTF-8
# locales like Japanese CP932, Chinese GBK, etc.)
_subprocess_encoding = locale.getpreferredencoding(False)
if not _subprocess_encoding or _subprocess_encoding == 'ascii':
_subprocess_encoding = 'utf-8'

_out_dir = os.environ['OUTDIR']
_log_file = os.path.join(_out_dir, ('log_%s' % os.getpid()))

Expand Down
Loading