Skip to content

Commit bc3fd29

Browse files
committed
Simplify enabling VT100 mode on Windows.
Arguably, this relies on a bug, but: * The current implementation was vulnerable to platform-specific bugs, as highlighted in discussions of VT100 on Windows in the Python bug tracker, while the new one is clearly not going to cause harm. * If the bug in Python is fixed, hopefully we will gain a proper way to enable VT100.
1 parent 963f13f commit bc3fd29

File tree

1 file changed

+5
-44
lines changed

1 file changed

+5
-44
lines changed

src/websockets/__main__.py

+5-44
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,14 @@
99

1010
try:
1111
import readline # noqa: F401
12-
except ImportError: # Windows has no `readline` normally
12+
except ImportError: # readline isn't available on all platforms
1313
pass
1414

1515
from .frames import Close
1616
from .sync.client import ClientConnection, connect
1717
from .version import version as websockets_version
1818

1919

20-
if sys.platform == "win32":
21-
22-
def win_enable_vt100() -> None:
23-
"""
24-
Enable VT-100 for console output on Windows.
25-
26-
See also https://github.com/python/cpython/issues/73245.
27-
28-
"""
29-
import ctypes
30-
31-
STD_OUTPUT_HANDLE = ctypes.c_uint(-11)
32-
INVALID_HANDLE_VALUE = ctypes.c_uint(-1)
33-
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x004
34-
35-
handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
36-
if handle == INVALID_HANDLE_VALUE:
37-
raise RuntimeError("unable to obtain stdout handle")
38-
39-
cur_mode = ctypes.c_uint()
40-
if ctypes.windll.kernel32.GetConsoleMode(handle, ctypes.byref(cur_mode)) == 0:
41-
raise RuntimeError("unable to query current console mode")
42-
43-
# ctypes ints lack support for the required bit-OR operation.
44-
# Temporarily convert to Py int, do the OR and convert back.
45-
py_int_mode = int.from_bytes(cur_mode, sys.byteorder)
46-
new_mode = ctypes.c_uint(py_int_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
47-
48-
if ctypes.windll.kernel32.SetConsoleMode(handle, new_mode) == 0:
49-
raise RuntimeError("unable to set console mode")
50-
51-
5220
def print_during_input(string: str) -> None:
5321
sys.stdout.write(
5422
# Save cursor position
@@ -116,17 +84,10 @@ def main() -> None:
11684
if args.uri is None:
11785
parser.error("the following arguments are required: <uri>")
11886

119-
# If we're on Windows, enable VT100 terminal support.
120-
if sys.platform == "win32":
121-
try:
122-
win_enable_vt100()
123-
except RuntimeError as exc:
124-
sys.stderr.write(
125-
f"Unable to set terminal to VT100 mode. This is only "
126-
f"supported since Win10 anniversary update. Expect "
127-
f"weird symbols on the terminal.\nError: {exc}\n"
128-
)
129-
sys.stderr.flush()
87+
# Enable VT100 to support ANSI escape codes in Command Prompt on Windows.
88+
# See https://github.com/python/cpython/issues/74261 for why this works.
89+
if sys.platform == "win32": # pragma: no cover
90+
os.system("")
13091

13192
try:
13293
websocket = connect(args.uri)

0 commit comments

Comments
 (0)