This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author joseph.hackman
Recipients joseph.hackman, paul.moore, steve.dower, tim.golden, zach.ware
Date 2016-12-24.01:33:08
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1482543191.02.0.407458515739.issue29059@psf.upfronthosting.co.za>
In-reply-to
Content
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)
History
Date User Action Args
2016-12-24 01:33:11joseph.hackmansetrecipients: + joseph.hackman, paul.moore, tim.golden, zach.ware, steve.dower
2016-12-24 01:33:11joseph.hackmansetmessageid: <1482543191.02.0.407458515739.issue29059@psf.upfronthosting.co.za>
2016-12-24 01:33:10joseph.hackmanlinkissue29059 messages
2016-12-24 01:33:08joseph.hackmancreate