Skip to content

Commit

Permalink
Don't be lazy on Windows
Browse files Browse the repository at this point in the history
Fixes #232
  • Loading branch information
hynek committed Jan 25, 2020
1 parent 5072cac commit 6b596f3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -36,6 +36,8 @@ Changes:
`#239 <https://github.com/hynek/structlog/pull/239>`_
- The logger created by ``structlog.get_logger()`` is not detected as an abstract method anymore, when attached to an abstract base class.
`#229 <https://github.com/hynek/structlog/issues/229>`_
- ``colorama`` isn't initialized lazily on Windows anymore because it breaks rendering.
`#232 <https://github.com/hynek/structlog/issues/232>`_,


----
Expand Down
29 changes: 20 additions & 9 deletions src/structlog/dev.py
Expand Up @@ -8,6 +8,8 @@

from __future__ import absolute_import, division, print_function

import sys

from six import PY2, StringIO, string_types


Expand All @@ -19,6 +21,7 @@

__all__ = ["ConsoleRenderer"]

_IS_WINDOWS = sys.platform == "win32"

_MISSING = "{who} requires the {package} package installed. "
_EVENT_WIDTH = 30 # pad the event name to so many characters
Expand Down Expand Up @@ -125,6 +128,8 @@ class ConsoleRenderer(object):
``colorama`` now initializes lazily to avoid unwanted initializations as
``ConsoleRenderer`` is used by default.
.. versionchanged:: 19.2 Can be pickled now.
.. versionchanged:: 20.1 ``colorama`` does not initialize lazily on Windows
anymore because it breaks rendering.
"""

def __init__(
Expand All @@ -145,9 +150,12 @@ def __init__(
)
)

self._init_colorama = True
if force_colors:
self._force_colors = True
if _IS_WINDOWS: # pragma: no cover
_init_colorama(self._force_colors)
else:
self._init_colorama = True
if force_colors:
self._force_colors = True

styles = _ColorfulStyles
else:
Expand Down Expand Up @@ -185,12 +193,7 @@ def _repr(self, val):
def __call__(self, _, __, event_dict):
# Initialize lazily to prevent import side-effects.
if self._init_colorama:
if self._force_colors:
colorama.deinit()
colorama.init(strip=False)
else:
colorama.init()

_init_colorama(self._force_colors)
self._init_colorama = False
sio = StringIO()

Expand Down Expand Up @@ -291,6 +294,14 @@ def get_default_level_styles(colors=True):
}


def _init_colorama(force):
if force:
colorama.deinit()
colorama.init(strip=False)
else:
colorama.init()


_SENTINEL = object()


Expand Down

0 comments on commit 6b596f3

Please sign in to comment.