﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
24068	management commands' OutputWrapper adds newline wrapped inside style	Markus Holtermann	nobody	"According to the [https://docs.djangoproject.com/en/dev/howto/custom-management-commands/#management-commands-output 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:

{{{#!diff
diff --git a/django/core/management/base.py b/django/core/management/base.py
index 869a11b..e29bb8a 100644
--- a/django/core/management/base.py
+++ b/django/core/management/base.py
@@ -107,11 +107,12 @@ class OutputWrapper(object):
         return getattr(self._out, name)

     def write(self, msg, style_func=None, ending=None):
+        style_func = style_func or self.style_func
         ending = self.ending if ending is None else ending
+        msg = force_str(style_func(msg))
         if ending and not msg.endswith(ending):
             msg += ending
-        style_func = style_func or self.style_func
-        self._out.write(force_str(style_func(msg)))
+        self._out.write(msg)


 class BaseCommand(object):
}}}"	Bug	new	Core (Management commands)	dev	Normal				Unreviewed	1	0		0	1	0
