[Django] #24068: management commands' OutputWrapper adds newline wrapped inside style

16 views
Skip to first unread message

Django

unread,
Jan 1, 2015, 8:46:07 AM1/1/15
to django-...@googlegroups.com
#24068: management commands' OutputWrapper adds newline wrapped inside style
--------------------------------------------+--------------------
Reporter: MarkusH | Owner: nobody
Type: Bug | Status: new
Component: Core (Management commands) | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Easy pickings: 1 | UI/UX: 0
--------------------------------------------+--------------------
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):
}}}

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

Django

unread,
Jan 1, 2015, 8:56:05 AM1/1/15
to django-...@googlegroups.com
#24068: management commands' OutputWrapper adds newline wrapped inside style
-------------------------------------+-------------------------------------
Reporter: MarkusH | Owner: MarkusH
Type: Bug | Status: assigned
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by MarkusH):

* status: new => assigned
* needs_better_patch: => 0
* needs_tests: => 1
* owner: nobody => MarkusH
* needs_docs: => 0


Old description:

> class BaseCommand(object):
> }}}

New description:

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'
}}}

A potential patch would be:

{{{#!diff
diff --git a/django/core/management/base.py
b/django/core/management/base.py

index 869a11b..dc22d74 100644
--- a/django/core/management/base.py
+++ b/django/core/management/base.py
@@ -107,11 +107,13 @@ class OutputWrapper(object):
return getattr(self._out, name)

def write(self, msg, style_func=None, ending=None):

- ending = self.ending if ending is None else ending
- 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)))

+ ending = self.ending if ending is None else ending
+ if ending:
+ if msg.endswith(ending):
+ msg = msg[:-len(ending)]
+ msg = force_str(style_func(msg)) + ending
+ self._out.write(msg)


class BaseCommand(object):
}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/24068#comment:1>

Django

unread,
Jan 1, 2015, 8:57:13 AM1/1/15
to django-...@googlegroups.com
#24068: management commands' OutputWrapper adds newline wrapped inside style
-------------------------------------+-------------------------------------
Reporter: MarkusH | Owner: MarkusH
Type: Bug | Status: assigned
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by MarkusH:

Old description:

> 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..dc22d74 100644
> --- a/django/core/management/base.py
> +++ b/django/core/management/base.py
> @@ -107,11 +107,13 @@ class OutputWrapper(object):


> return getattr(self._out, name)
>
> def write(self, msg, style_func=None, ending=None):

> - ending = self.ending if ending is None else ending
> - 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)))

> + ending = self.ending if ending is None else ending
> + if ending:
> + if msg.endswith(ending):
> + msg = msg[:-len(ending)]
> + msg = force_str(style_func(msg)) + ending
> + self._out.write(msg)
>

> class BaseCommand(object):
> }}}

New description:

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'
}}}

A potential patch would be:

{{{#!diff
diff --git a/django/core/management/base.py
b/django/core/management/base.py

index 869a11b..318740f 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):

- ending = self.ending if ending is None else ending
- 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)))

+ ending = self.ending if ending is None else ending
+ if ending and msg.endswith(ending):


+ msg = msg[:-len(ending)]
+ msg = force_str(style_func(msg)) + ending
+ self._out.write(msg)
}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/24068#comment:2>

Django

unread,
Jan 2, 2015, 4:02:58 PM1/2/15
to django-...@googlegroups.com
#24068: management commands' OutputWrapper adds newline wrapped inside style
-------------------------------------+-------------------------------------
Reporter: MarkusH | Owner: MarkusH
Type: Bug | Status: closed

Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: assigned => closed
* resolution: => wontfix


Comment:

Let's focus our efforts on #21429 instead. We can merge
[https://github.com/django/django/pull/3823 PR 3823] to fix the particular
problem.

--
Ticket URL: <https://code.djangoproject.com/ticket/24068#comment:3>

Reply all
Reply to author
Forward
0 new messages