#34768: Calling `colorama.init()` on module load can have unwanted side effects
-------------------------------------+-------------------------------------
     Reporter:  Charlie DeTar        |                    Owner:  nobody
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    Component:  Core (Management     |                  Version:  4.2
  commands)                          |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Charlie DeTar):

 @LufafaJoshua passing `wrap=False` would solve my problem - but it would
 likely break Django's usecase for windows, since it would then be
 necessary for Django to write to `AnsiToWin32(sys.stdout).stream`
 explicitly when it wants colorized output on windows.  I believe the whole
 reason for Django to use colorama at all is to keep colorized output on
 windows consoles without having to explicitly switch or feature detect
 them throughout the codebase.

 Colorama's newer `colorama.just_fix_windows_console()` initializer (which
 wasn't around when colorama was first added to Django) may be a better
 fit. That will cause colorama to attempt to detect if the console it's
 writing to is a windows console which does not support ANSI color
 sequences, and wrap it only if that's the case.  The caveat here is that
 its detection may not be foolproof, so there could be edgecases for
 particular windows console configurations that could lose colorized
 output.  In my not unbiased opinion, it'd be better to err on the side of
 leaving `sys.stdout` alone, rather than erring on the side of always
 replacing it (with the attendant edge case failures that can cause).

 I managed to work around my issue by adding a `conftest.py` file with the
 following hook to de-initialize colorama while pytest is loading, and
 monkeypatch it to prevent subsequent initialization:
 {{{
 #!python
 def pytest_configure():
     """
     Hack to retain colorized output in pytest-xdist, working around this
 bug:
     https://code.djangoproject.com/ticket/34768
     """
     try:
         import colorama

         # Undo any previously called colorama.init()
         colorama.deinit()
         # Prevent any future call to colorama.init() from having an effect
         colorama.init = lambda: None
     except ImportError:
         pass
 }}}

 I imagine any edge-case windows console users whose configuration isn't
 properly detected by `just_fix_windows_console()` could add a similar hack
 (but in reverse) to force a call to `colorama.init()` instead if they
 wished to restore colorized output.  That, or open a ticket with colorama
 to improve feature-detection of their configuration.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34768#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018a31812a64-09c9fcb2-6863-494c-a365-74a3a2f26fc0-000000%40eu-central-1.amazonses.com.

Reply via email to