UnicodeEncodeError in `rgthree-comfy` Custom Node on Windows prevents ComfyUI Desktop from starting

Error Log

[2024-11-21 16:37:13.031] [error] File "C:\Users\user\AppData\Local\Programs@comfyorgcomfyui-electron\resources\ComfyUI\nodes.py", line 2024, in load_custom_node module_spec.loader.exec_module(module) File "<frozen importlib._bootstrap_external>", line 995, in exec_module File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed Traceback (most recent call last): File "C:\Users\user\AppData\Local\Programs@comfyorgcomfyui-electron\resources\ComfyUI\nodes.py", line 2024, in load_custom_node module_spec.loader.exec_module(module) File "<frozen importlib._bootstrap_external>", line 995, in exec_module File "<frozen importlib._bootstrap>", line 488, in call_with_frames_removed File "D:/AI/ComfyUI\custom_nodes/rgthree-comfy_init.py", line 103, in <module> File "D:\AI/ComfyUI\custom_nodes/rgthree-comfy\py\log.py", line 81, in log print(msg) File "C:\Users\user\AppData\Local\Programs@comfyorgcomfyui-electron\resources\ComfyUI\app\logger.py", line 32, in write super().write(data) File "D:\AI\ComfyUI\uv-cache\cpython-3.12.4-windows-x86_64-none\Lib\encodings\cp1252.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] ^^^^^^^^^^^^^

Workaround

patch in log.py file within the rgthree-comfy custom node, which successfully resolved the issue:

import sys
import io

# Configure stdout and stderr to use UTF-8 encoding
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8', errors='replace')

COLORS = {
    'BLACK': '\033[30m',
    'RED': '\033[31m',
    'GREEN': '\033[32m',
    'YELLOW': '\033[33m',
    'BLUE': '\033[34m',
    'MAGENTA': '\033[35m',
    'CYAN': '\033[36m',
    'WHITE': '\033[37m',
    'GREY': '\033[90m',
    'BRIGHT_RED': '\033[91m',
    'BRIGHT_GREEN': '\033[92m',
    'BRIGHT_YELLOW': '\033[93m',
    'BRIGHT_BLUE': '\033[94m',
    'BRIGHT_MAGENTA': '\033[95m',
    'BRIGHT_CYAN': '\033[96m',
    'BRIGHT_WHITE': '\033[97m',
    'RESET': '\033[00m',
}

ENABLE_COLORS = True  # Set to False to disable colored output

def log(message, color=None, msg_color=None, prefix=None):
    """Basic logging with encoding compatibility and optional color support."""
    if ENABLE_COLORS:
        color_code = COLORS.get(color, COLORS["BRIGHT_GREEN"]) if color else COLORS["BRIGHT_GREEN"]
        msg_color_code = COLORS.get(msg_color, '') if msg_color else ''
    else:
        color_code = ''
        msg_color_code = ''

    prefix_str = f'[{prefix}]' if prefix else ''
    msg = f'{color_code}[rgthree-comfy]{prefix_str}'
    msg += f'{msg_color_code} {message}{COLORS["RESET"] if ENABLE_COLORS else ""}'

    try:
        print(msg)
    except UnicodeEncodeError:
        # Fallback: Replace unsupported characters
        safe_msg = msg.encode('ascii', errors='replace').decode('ascii')
        print(safe_msg)

→ all log messages are encoded in UTF-8, preventing the application from crashing due to unsupported Unicode characters.