From b89c43eb0f8863293d4db2ad11dadc10039f5b89 Mon Sep 17 00:00:00 2001 From: jack1142 <6032823+jack1142@users.noreply.github.com> Date: Thu, 20 May 2021 10:31:27 +0200 Subject: [PATCH] Ensure nothing initializes colorama when it isn't needed (#5063) --- redbot/__init__.py | 23 +++++++++++++++++++++++ redbot/__main__.py | 4 ++-- redbot/setup.py | 4 ++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/redbot/__init__.py b/redbot/__init__.py index 4440ad2d3..aaa5defc3 100644 --- a/redbot/__init__.py +++ b/redbot/__init__.py @@ -191,6 +191,29 @@ def _update_event_loop_policy(): _asyncio.set_event_loop_policy(_uvloop.EventLoopPolicy()) +def _ensure_no_colorama(): + # a hacky way to ensure that nothing initialises colorama + # if we're not running with legacy Windows command line mode + from rich.console import detect_legacy_windows + + if not detect_legacy_windows(): + import colorama + import colorama.initialise + + colorama.deinit() + + def _colorama_wrap_stream(stream, *args, **kwargs): + return stream + + colorama.wrap_stream = _colorama_wrap_stream + colorama.initialise.wrap_stream = _colorama_wrap_stream + + +def _early_init(): + _update_event_loop_policy() + _ensure_no_colorama() + + __version__ = "3.4.10.dev1" version_info = VersionInfo.from_str(__version__) diff --git a/redbot/__main__.py b/redbot/__main__.py index 8810ef544..1af3486ea 100644 --- a/redbot/__main__.py +++ b/redbot/__main__.py @@ -22,9 +22,9 @@ import discord # Set the event loop policies here so any subsequent `new_event_loop()` # calls, in particular those as a result of the following imports, # return the correct loop object. -from redbot import _update_event_loop_policy, __version__ +from redbot import _early_init, __version__ -_update_event_loop_policy() +_early_init() import redbot.logging from redbot.core.bot import Red, ExitCodes diff --git a/redbot/setup.py b/redbot/setup.py index 20e9ba2ac..454b2bbe7 100644 --- a/redbot/setup.py +++ b/redbot/setup.py @@ -9,6 +9,10 @@ from copy import deepcopy from pathlib import Path from typing import Dict, Any, Optional, Union +from redbot import _early_init + +_early_init() + import appdirs import click