#34768: Calling `colorama.init()` on module load can have unwanted side effects
-------------------------------------+-------------------------------------
               Reporter:  Charlie    |          Owner:  nobody
  DeTar                              |
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:  Core       |        Version:  4.2
  (Management commands)              |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 `django.core.management.color` calls
 
[https://github.com/django/django/blob/aa3cb3f37265be37d892e2b391ff023e9caee2a4/django/core/management/color.py#L12-L14
 `colorama.init()`] on module load if the module is present in the system.
 This causes `sys.stdout` to be replaced by an instance of
 `colorama.ansitowin32.StreamWrapper`.

 This, in turn, can cause problems in other contexts that do not expect
 `sys.stdout` to be a colorama wrapper.  For example, using `pytest` with
 `pytest-xdist` in `--looponfail` mode (which uses
 [https://pypi.org/project/execnet/ execnet] to further wrap its output)
 results in ansi color escapes getting dropped from the output.

 In discussion of #32740, a maintainer commented "There's no reason to have
 colorama installed at all if you're not on windows".  However, colorama
 now [https://github.com/tartley/colorama describes itself] as providing a
 "simple cross-platform API for printing colored terminal text from
 Python"; and indeed, [https://github.com/Riverside-Healthcare/djlint
 djlint] installs colorama as a dependency for all platforms, using other
 color utilities within the project rather than the stream wrapper.  In
 short: it is probably not right to assume that `colorama` will only be
 present on win32, and thus Django probably shouldn't be replacing
 `sys.stdout` with it on module import without at a platform check.

 To summarize: a linux environment which installs:
 - Django v3.2 -> dev
 - djlint v1.32.x
 - pytest-xdist v3.3.x
 will lose colorized output while running `pytest` with `--looponfail` due
 to Django's call to `colorama.init()`, which is an unnecessary call on
 that platform.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34768>
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070189d69848e4-996d71cd-2cd1-40a3-822d-6dfb443c2f19-000000%40eu-central-1.amazonses.com.

Reply via email to