Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structlog overwrites sys.stdout through Colorama #210

Closed
gwtwod opened this issue May 9, 2019 · 2 comments

Comments

@gwtwod
Copy link

@gwtwod gwtwod commented May 9, 2019

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'>
@hynek

This comment has been minimized.

Copy link
Owner

@hynek hynek commented May 17, 2019

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

@hynek

This comment has been minimized.

Copy link
Owner

@hynek hynek commented Aug 15, 2019

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

@hynek hynek closed this Sep 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.