Closed
Description
When i import structlog with colorama installed, my sys.stdout is overwritten by colorama.init() called here:
structlog/src/structlog/dev.py
Line 147 in cc063e0
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 commentedon May 17, 2019
Hm, that's very unfortunate indeed. Maybe we should make
colorama.init()
lazy?hynek commentedon 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