Skip to content

Structlog overwrites sys.stdout through Colorama #210

Closed
@gwtwod

Description

@gwtwod

When i import structlog with colorama installed, my sys.stdout is overwritten by colorama.init() called here:

colorama.init()

If I then try to force printing of ANSI color codes in my script while piping the results somewhere, all color codes are stripped which I didn't expect.

Could this be avoided somehow? Perhaps by calling colorama.init(wrap=False), or something else if that breaks on windows?

Example to reproduce:

#!/usr/bin/env python3
import sys 
print('\033[92m' + 'Before structlog import' + '\033[0m sys.stdout:', sys.stdout)
import structlog
print('\033[92m' + 'After structlog import' + '\033[0m sys.stdout:', sys.stdout)
sys.stdout = sys.__stdout__
print('\033[92m' + 'After resetting' + '\033[0m sys.stdout:', sys.stdout)

Example output where 2nd line has no colors anymore:

/poc.py | cat
Before structlog import sys.stdout: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
After structlog import sys.stdout: <colorama.ansitowin32.StreamWrapper object at 0x7f1c0c981b70>
After resetting sys.stdout: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>

Activity

hynek

hynek commented on May 17, 2019

@hynek
Owner

Hm, that's very unfortunate indeed. Maybe we should make colorama.init() lazy?

hynek

hynek commented on Aug 15, 2019

@hynek
Owner

I hope I fixed it in 3b534af – would you mind checking if master fixes the problem for you before I release? TIA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @hynek@gwtwod

        Issue actions

          Structlog overwrites sys.stdout through Colorama · Issue #210 · hynek/structlog