Skip to content

Commit 7671e2d

Browse files
committed
Make it work on windows
1 parent 9bc06ed commit 7671e2d

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

Lib/traceback.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ def print_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, \
133133
BUILTIN_EXCEPTION_LIMIT = object()
134134

135135
def _can_colorize():
136+
if sys.platform == "win32":
137+
try:
138+
import nt
139+
if not nt._supports_virtual_terminal():
140+
return False
141+
except (ImportError, AttributeError):
142+
return False
143+
136144
if not _COLORIZE:
137145
return False
138146
if os.environ.get("PY_COLORS") == "1":

Modules/clinic/posixmodule.c.h

Lines changed: 27 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/posixmodule.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16073,6 +16073,26 @@ os_waitstatus_to_exitcode_impl(PyObject *module, PyObject *status_obj)
1607316073
}
1607416074
#endif
1607516075

16076+
#if defined(MS_WINDOWS)
16077+
/*[clinic input]
16078+
os._supports_virtual_terminal
16079+
16080+
Checks if virtual terminal is supported in windows
16081+
[clinic start generated code]*/
16082+
16083+
static PyObject *
16084+
os__supports_virtual_terminal_impl(PyObject *module)
16085+
/*[clinic end generated code: output=bd0556a6d9d99fe6 input=0752c98e5d321542]*/
16086+
{
16087+
DWORD mode = 0;
16088+
HANDLE handle = GetStdHandle(STD_ERROR_HANDLE);
16089+
if (!GetConsoleMode(handle, &mode)) {
16090+
Py_RETURN_FALSE;
16091+
}
16092+
return PyBool_FromLong(mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING);
16093+
}
16094+
#endif
16095+
1607616096

1607716097
static PyMethodDef posix_methods[] = {
1607816098

@@ -16277,6 +16297,8 @@ static PyMethodDef posix_methods[] = {
1627716297
OS__PATH_ISFILE_METHODDEF
1627816298
OS__PATH_ISLINK_METHODDEF
1627916299
OS__PATH_EXISTS_METHODDEF
16300+
16301+
OS__SUPPORTS_VIRTUAL_TERMINAL_METHODDEF
1628016302
{NULL, NULL} /* Sentinel */
1628116303
};
1628216304

0 commit comments

Comments
 (0)