[Django] #33295: Django throws an error when a list is passed into self.stdout.write for writing message into the terminal

13 views
Skip to first unread message

Django

unread,
Nov 17, 2021, 6:00:25 AM11/17/21
to django-...@googlegroups.com
#33295: Django throws an error when a list is passed into self.stdout.write for
writing message into the terminal
-------------------------------------+-------------------------------------
Reporter: Vishal | Owner: nobody
Pandey |
Type: Bug | Status: assigned
Component: Core | Version: 3.2
(Management commands) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
{{{
class Command(BaseCommand):
def handle(self, *args, **kwargs):
self.stdout.write([1,2,3])
}}}

The following piece of code throws an Attribute error stating that list'
object has no attribute 'endswith', it is because the datatype of msg is
not checked in handle function while writing the source code.

Here is the full traceback of the error
{{{
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/vishal/.local/lib/python3.8/site-
packages/django/core/management/__init__.py", line 401, in
execute_from_command_line
utility.execute()
File "/home/vishal/.local/lib/python3.8/site-
packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/vishal/.local/lib/python3.8/site-
packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/vishal/.local/lib/python3.8/site-
packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File
"/home/vishal/Desktop/open_source/leetcode_companywise/questions/management/commands/addcompanies.py",
line 18, in handle
self.stdout.write([1,2,3])
File "/home/vishal/.local/lib/python3.8/site-
packages/django/core/management/base.py", line 144, in write
if ending and not msg.endswith(ending):
AttributeError: 'list' object has no attribute 'endswith'
}}}

It can be fixed if we just ensure the datatype of the msg is string and
handle it accordingly with
{{{
isinstance(msg, str)
}}}
in line 151 of /django/core/management/base.py.

After handling this error a proper error message(i.e. TypeError) will be
shown on the terminal of the user which is more appropriate in this
situation.

New traceback:

{{{
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/vishal/.local/lib/python3.8/site-
packages/django/core/management/__init__.py", line 401, in
execute_from_command_line
utility.execute()
File "/home/vishal/.local/lib/python3.8/site-
packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/vishal/.local/lib/python3.8/site-
packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/vishal/.local/lib/python3.8/site-
packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File
"/home/vishal/Desktop/open_source/leetcode_companywise/questions/management/commands/addcompanies.py",
line 18, in handle
self.stdout.write([1,2,3])
File "/home/vishal/.local/lib/python3.8/site-
packages/django/core/management/base.py", line 147, in write
self._out.write(style_func(msg))
TypeError: write() argument must be str, not list
}}}

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

Django

unread,
Nov 17, 2021, 6:01:50 AM11/17/21
to django-...@googlegroups.com
#33295: Django throws an error when a list is passed into self.stdout.write for
writing message into the terminal
-------------------------------------+-------------------------------------
Reporter: Vishal Pandey | Owner: nobody
Type: Bug | Status: assigned
Component: Core (Management | Version: 3.2
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Vishal Pandey):

* Attachment "Screenshot from 2021-11-17 15-46-51.png" added.

First traceback is the current output and second traceback is the new
output after handling the bug

Django

unread,
Nov 17, 2021, 6:02:26 AM11/17/21
to django-...@googlegroups.com
#33295: Django throws an error when a list is passed into self.stdout.write for
writing message into the terminal
-------------------------------------+-------------------------------------
Reporter: Vishal Pandey | Owner: Vishal

| Pandey
Type: Bug | Status: assigned
Component: Core (Management | Version: 3.2
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Vishal Pandey):

* owner: nobody => Vishal Pandey


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

Django

unread,
Nov 17, 2021, 6:20:43 AM11/17/21
to django-...@googlegroups.com
#33295: Django throws an error when a list is passed into self.stdout.write for
writing message into the terminal
-------------------------------------+-------------------------------------
Reporter: Vishal Pandey | Owner: Vishal

| Pandey
Type: Bug | Status: assigned
Component: Core (Management | Version: 3.2
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Vishal Pandey:

Old description:

New description:

We pretty much know and understand that only a string could be passed in
self.stdout.write function
Just in case if we pass a list into it, like the below piece of code:


{{{
class Command(BaseCommand):
def handle(self, *args, **kwargs):
self.stdout.write([1,2,3])
}}}

It throws an Attribute error stating that list' object has no attribute


'endswith', it is because the datatype of msg is not checked in handle

function while writing write method in Outputwrapper class.

New traceback:

--

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

Django

unread,
Nov 17, 2021, 6:27:41 AM11/17/21
to django-...@googlegroups.com
#33295: Django throws an error when a list is passed into self.stdout.write for
writing message into the terminal
-------------------------------------+-------------------------------------
Reporter: Vishal Pandey | Owner: Vishal
| Pandey
Type: Bug | Status: closed

Component: Core (Management | Version: 3.2
commands) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

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


Comment:

I don't think it worth fixing, there are many methods and functions in
Django that assume that the expected arguments are passed. It's not worth
adding `isinstance()` check to most of them.

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

Django

unread,
Nov 17, 2021, 6:35:37 AM11/17/21
to django-...@googlegroups.com
#33295: AttributeError is raised when a list is passed into self.stdout.write().

-------------------------------------+-------------------------------------
Reporter: Vishal Pandey | Owner: Vishal
| Pandey
Type: Bug | Status: closed
Component: Core (Management | Version: 3.2
commands) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/33295#comment:4>

Reply all
Reply to author
Forward
0 new messages