Skip to content

Commit daa81bb

Browse files
committed
Merge pull request #92087 from Repiteo/scons/windows-color-fix-maybe
SCons: Fix potential Windows ANSI exception
2 parents c63383f + a9810cf commit daa81bb

File tree

4 files changed

+67
-38
lines changed

4 files changed

+67
-38
lines changed

SConstruct

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ from collections import OrderedDict
1515
from importlib.util import spec_from_file_location, module_from_spec
1616
from SCons import __version__ as scons_raw_version
1717

18-
# Enable ANSI escape code support on Windows 10 and later (for colored console output).
19-
# <https://github.com/python/cpython/issues/73245>
20-
if sys.platform == "win32":
21-
from ctypes import windll, c_int, byref
22-
23-
stdout_handle = windll.kernel32.GetStdHandle(c_int(-11))
24-
mode = c_int(0)
25-
windll.kernel32.GetConsoleMode(c_int(stdout_handle), byref(mode))
26-
mode = c_int(mode.value | 4)
27-
windll.kernel32.SetConsoleMode(c_int(stdout_handle), mode)
2818

2919
# Explicitly resolve the helper modules, this is done to avoid clash with
3020
# modules of the same name that might be randomly added (e.g. someone adding
@@ -74,6 +64,24 @@ if ARGUMENTS.get("target", "editor") == "editor":
7464
_helper_module("editor.editor_builders", "editor/editor_builders.py")
7565
_helper_module("editor.template_builders", "editor/template_builders.py")
7666

67+
# Enable ANSI escape code support on Windows 10 and later (for colored console output).
68+
# <https://github.com/python/cpython/issues/73245>
69+
if sys.stdout.isatty() and sys.platform == "win32":
70+
try:
71+
from ctypes import windll, byref, WinError # type: ignore
72+
from ctypes.wintypes import DWORD # type: ignore
73+
74+
stdout_handle = windll.kernel32.GetStdHandle(DWORD(-11))
75+
mode = DWORD(0)
76+
if not windll.kernel32.GetConsoleMode(stdout_handle, byref(mode)):
77+
raise WinError()
78+
mode = DWORD(mode.value | 4)
79+
if not windll.kernel32.SetConsoleMode(stdout_handle, mode):
80+
raise WinError()
81+
except Exception as e:
82+
methods._colorize = False
83+
print_error(f"Failed to enable ANSI escape code support, disabling color output.\n{e}")
84+
7785
# Scan possible build platforms
7886

7987
platform_list = [] # list of platforms

doc/tools/make_rst.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -677,17 +677,6 @@ def add_hit(self, class_name: str, context: DefinitionBase, error: str, state: S
677677

678678
# Entry point for the RST generator.
679679
def main() -> None:
680-
# Enable ANSI escape code support on Windows 10 and later (for colored console output).
681-
# <https://bugs.python.org/issue29059>
682-
if platform.system().lower() == "windows":
683-
from ctypes import windll, c_int, byref # type: ignore
684-
685-
stdout_handle = windll.kernel32.GetStdHandle(c_int(-11))
686-
mode = c_int(0)
687-
windll.kernel32.GetConsoleMode(c_int(stdout_handle), byref(mode))
688-
mode = c_int(mode.value | 4)
689-
windll.kernel32.SetConsoleMode(c_int(stdout_handle), mode)
690-
691680
parser = argparse.ArgumentParser()
692681
parser.add_argument("path", nargs="+", help="A path to an XML file or a directory containing XML files to parse.")
693682
parser.add_argument("--filter", default="", help="The filepath pattern for XML files to filter.")
@@ -711,7 +700,25 @@ def main() -> None:
711700
)
712701
args = parser.parse_args()
713702

714-
should_color = args.color or (hasattr(sys.stdout, "isatty") and sys.stdout.isatty())
703+
should_color = bool(args.color or sys.stdout.isatty() or os.environ.get("CI"))
704+
705+
# Enable ANSI escape code support on Windows 10 and later (for colored console output).
706+
# <https://github.com/python/cpython/issues/73245>
707+
if should_color and sys.stdout.isatty() and sys.platform == "win32":
708+
try:
709+
from ctypes import windll, byref, WinError # type: ignore
710+
from ctypes.wintypes import DWORD # type: ignore
711+
712+
stdout_handle = windll.kernel32.GetStdHandle(DWORD(-11))
713+
mode = DWORD(0)
714+
if not windll.kernel32.GetConsoleMode(stdout_handle, byref(mode)):
715+
raise WinError()
716+
mode = DWORD(mode.value | 4)
717+
if not windll.kernel32.SetConsoleMode(stdout_handle, mode):
718+
raise WinError()
719+
except Exception:
720+
should_color = False
721+
715722
STYLES["red"] = "\x1b[91m" if should_color else ""
716723
STYLES["green"] = "\x1b[92m" if should_color else ""
717724
STYLES["yellow"] = "\x1b[93m" if should_color else ""

modules/text_server_adv/gdextension_build/SConstruct

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@ import time
66

77
# Enable ANSI escape code support on Windows 10 and later (for colored console output).
88
# <https://github.com/python/cpython/issues/73245>
9-
if sys.platform == "win32":
10-
from ctypes import windll, c_int, byref
11-
12-
stdout_handle = windll.kernel32.GetStdHandle(c_int(-11))
13-
mode = c_int(0)
14-
windll.kernel32.GetConsoleMode(c_int(stdout_handle), byref(mode))
15-
mode = c_int(mode.value | 4)
16-
windll.kernel32.SetConsoleMode(c_int(stdout_handle), mode)
9+
if sys.stdout.isatty() and sys.platform == "win32":
10+
try:
11+
from ctypes import windll, byref, WinError # type: ignore
12+
from ctypes.wintypes import DWORD # type: ignore
13+
14+
stdout_handle = windll.kernel32.GetStdHandle(DWORD(-11))
15+
mode = DWORD(0)
16+
if not windll.kernel32.GetConsoleMode(stdout_handle, byref(mode)):
17+
raise WinError()
18+
mode = DWORD(mode.value | 4)
19+
if not windll.kernel32.SetConsoleMode(stdout_handle, mode):
20+
raise WinError()
21+
except Exception as e:
22+
methods._colorize = False
23+
methods.print_error(f"Failed to enable ANSI escape code support, disabling color output.\n{e}")
1724

1825
# For the reference:
1926
# - CCFLAGS are compilation flags shared between C and C++

modules/text_server_fb/gdextension_build/SConstruct

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@ import time
66

77
# Enable ANSI escape code support on Windows 10 and later (for colored console output).
88
# <https://github.com/python/cpython/issues/73245>
9-
if sys.platform == "win32":
10-
from ctypes import windll, c_int, byref
11-
12-
stdout_handle = windll.kernel32.GetStdHandle(c_int(-11))
13-
mode = c_int(0)
14-
windll.kernel32.GetConsoleMode(c_int(stdout_handle), byref(mode))
15-
mode = c_int(mode.value | 4)
16-
windll.kernel32.SetConsoleMode(c_int(stdout_handle), mode)
9+
if sys.stdout.isatty() and sys.platform == "win32":
10+
try:
11+
from ctypes import windll, byref, WinError # type: ignore
12+
from ctypes.wintypes import DWORD # type: ignore
13+
14+
stdout_handle = windll.kernel32.GetStdHandle(DWORD(-11))
15+
mode = DWORD(0)
16+
if not windll.kernel32.GetConsoleMode(stdout_handle, byref(mode)):
17+
raise WinError()
18+
mode = DWORD(mode.value | 4)
19+
if not windll.kernel32.SetConsoleMode(stdout_handle, mode):
20+
raise WinError()
21+
except Exception as e:
22+
methods._colorize = False
23+
methods.print_error(f"Failed to enable ANSI escape code support, disabling color output.\n{e}")
1724

1825
# For the reference:
1926
# - CCFLAGS are compilation flags shared between C and C++

0 commit comments

Comments
 (0)