Skip to content

Commit e44284c

Browse files
authored
Merge pull request #2611 from segevfiner/patch-1
Early import colorama so that it get's the correct terminal
2 parents 81ad185 + d0ecfdf commit e44284c

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

_pytest/capture.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def pytest_load_initial_conftests(early_config, parser, args):
3737
ns = early_config.known_args_namespace
3838
if ns.capture == "fd":
3939
_py36_windowsconsoleio_workaround()
40+
_colorama_workaround()
4041
_readline_workaround()
4142
pluginmanager = early_config.pluginmanager
4243
capman = CaptureManager(ns.capture)
@@ -473,6 +474,24 @@ def buffer(self):
473474
raise AttributeError('redirected stdin has no attribute buffer')
474475

475476

477+
def _colorama_workaround():
478+
"""
479+
Ensure colorama is imported so that it attaches to the correct stdio
480+
handles on Windows.
481+
482+
colorama uses the terminal on import time. So if something does the
483+
first import of colorama while I/O capture is active, colorama will
484+
fail in various ways.
485+
"""
486+
487+
if not sys.platform.startswith('win32'):
488+
return
489+
try:
490+
import colorama # noqa
491+
except ImportError:
492+
pass
493+
494+
476495
def _readline_workaround():
477496
"""
478497
Ensure readline is imported so that it attaches to the correct stdio

changelog/2510.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix terminal color changing to black on Windows if ``colorama`` is imported in a ``conftest.py`` file.

0 commit comments

Comments
 (0)