Opened 8 years ago

Last modified 8 years ago

#24068 closed Bug

management commands' OutputWrapper adds newline wrapped inside style — at Version 1

Reported by: Markus Holtermann Owned by: Markus Holtermann
Component: Core (Management commands) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Markus Holtermann)

According to the docs the self.stdout.write() adds a newline (or explicitly defined ending) add the end of the message, if it's not already there. However, if the output is wrapped in some kind of style the message, including the ending, is wrapped.

Instead of

'\x1b[31;1mHello, world!\x1b[0m\n'

one gets

'\x1b[31;1mHello, world!\n\x1b[0m'

The issue came up while investigating https://github.com/django/django/pull/3153#issuecomment-68471839

A potential patch would be:

  • django/core/management/base.py

    diff --git a/django/core/management/base.py b/django/core/management/base.py
    index 869a11b..dc22d74 100644
    a b class OutputWrapper(object): 
    107107        return getattr(self._out, name)
    108108
    109109    def write(self, msg, style_func=None, ending=None):
    110         ending = self.ending if ending is None else ending
    111         if ending and not msg.endswith(ending):
    112             msg += ending
    113110        style_func = style_func or self.style_func
    114         self._out.write(force_str(style_func(msg)))
     111        ending = self.ending if ending is None else ending
     112        if ending:
     113            if msg.endswith(ending):
     114                msg = msg[:-len(ending)]
     115        msg = force_str(style_func(msg)) + ending
     116        self._out.write(msg)
    115117
    116118
    117119class BaseCommand(object):

Change History (1)

comment:1 Changed 8 years ago by Markus Holtermann

Description: modified (diff)
Needs tests: set
Owner: changed from nobody to Markus Holtermann
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top