[issue29059] Windows: Python not using ANSI compatible console

Joseph Hackman report at bugs.python.org
Fri Dec 23 20:33:11 EST 2016


New submission from Joseph Hackman:

On windows, Python does not request that Windows enable VT100 for console output for Python.

That is to say that ANSI codes such as \033[91m will function on Mac and Linux Pythons, but not Windows.

As there is no good interface in core Python to the kernel32 console operations (and there probably shouldn't be, it would be better to be consistent), I suggest just flipping the bit at startup on Windows.

I would be happy to submit a patch, but seek guidance on the best location for addition of code to 'at startup iff a tty is attached'.

The following code solves the issue:
import platform
if platform.system().lower() == 'windows':
    from ctypes import windll, c_int, byref
    stdout_handle = windll.kernel32.GetStdHandle(c_int(-11))
    mode = c_int(0)
    windll.kernel32.GetConsoleMode(c_int(stdout_handle), byref(mode))
    mode = c_int(mode.value | 4)
    windll.kernel32.SetConsoleMode(c_int(stdout_handle), mode)

Please see:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms686033(v=vs.85).aspx (Search for ENABLE_VIRTUAL_TERMINAL_PROCESSING)
https://msdn.microsoft.com/en-us/library/windows/desktop/ms683231(v=vs.85).aspx (As for why stdout is -11 on Windows)

----------
components: Windows
messages: 283913
nosy: joseph.hackman, paul.moore, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
status: open
title: Windows: Python not using ANSI compatible console
type: behavior
versions: Python 2.7, Python 3.3, Python 3.4, Python 3.5, Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29059>
_______________________________________


More information about the Python-bugs-list mailing list