[Django] #30305: Feature to set the returncode of a django custom command

17 views
Skip to first unread message

Django

unread,
Apr 1, 2019, 6:46:17 AM4/1/19
to django-...@googlegroups.com
#30305: Feature to set the returncode of a django custom command
-----------------------------------------+------------------------
Reporter: stephanm | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 2.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
In the docs:

https://docs.djangoproject.com/en/2.1/howto/custom-management-
commands/#django.core.management.BaseCommand.handle

I can see that the `handle()` function will return a string which goes to
stdout.

But I would like to set (supplementary) an integer returncode like its
done in `sys.exit()` see
https://docs.python.org/3/library/sys.html#sys.exit

I didn't find any "official" way to do this, perhaps this is a new
feature.

Thanks.

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

Django

unread,
Apr 1, 2019, 7:03:46 AM4/1/19
to django-...@googlegroups.com
#30305: Feature to set the returncode of a django custom command
-------------------------------+--------------------------------------
Reporter: stephanm | Owner: nobody
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: master
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by felixxm):

* status: new => closed
* version: 2.1 => master
* resolution: => invalid


Comment:

Thanks for the report, however answer for your question is already in the
ticket description, i.e. you can use `sys.exit(custom_number)` in the
`handle()`.

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

Django

unread,
Apr 1, 2019, 8:39:21 AM4/1/19
to django-...@googlegroups.com
#30305: Feature to set the returncode of a django custom command
-------------------------------+--------------------------------------

Reporter: stephanm | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: master
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by stephanm):

* status: closed => new
* resolution: invalid =>


Comment:

Hi,

I stepped a little through the code after the `handle()` function returns.

Here I see in that I pass through the function
`django.core.management.base.BaseCommand.run_from_argv()`
which itself calls a `connections.close_all()` function at the end,
so using `sys.exit()` inside the `handle()` function will not give the
exact same
behaviour as a "controlled" exit.

Or do I miss something?

See:
[https://github.com/django/django/blob/2.1.8/django/core/management/base.py#L299
run_from_argv(self, argv)]
{{{#!python

def run_from_argv(self, argv):
try:
self.execute(*args, **cmd_options)
except Exception as e:
#....
finally:
try:
connections.close_all()
except ImproperlyConfigured:
# Ignore if connections aren't setup at this point (e.g. no
# configured settings).
pass
}}}

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

Django

unread,
Apr 1, 2019, 8:56:31 AM4/1/19
to django-...@googlegroups.com
#30305: Feature to set the returncode of a django custom command
-------------------------------+--------------------------------------

Reporter: stephanm | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: master
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by stephanm):

I observed an other strange thing:

if my `handle()`function returns a 1 (int) instead of a string I get the
error like below,
but if I return a 0 (again as int) I do **not** get the error below.

Is this a magic thing in django or did I stuble over a python bug?

{{{
Traceback (most recent call last):
File "D:\dev\eclipseworkspace\djtest\manage.py", line 12, in <module>
execute_from_command_line(sys.argv)
File "D:\Programme\python37\lib\site-
packages\django\core\management\__init__.py", line 364, in
execute_from_command_line
utility.execute()
File "D:\Programme\python37\lib\site-
packages\django\core\management\__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\Programme\python37\lib\site-
packages\django\core\management\base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "D:\Programme\python37\lib\site-
packages\django\core\management\base.py", line 339, in execute
self.stdout.write(output)
File "D:\Programme\python37\lib\site-
packages\django\core\management\base.py", line 107, in write
if ending and not msg.endswith(ending):
AttributeError: 'int' object has no attribute 'endswith'
}}}

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

Django

unread,
Apr 1, 2019, 9:10:14 AM4/1/19
to django-...@googlegroups.com
#30305: Feature to set the returncode of a django custom command
-------------------------------+--------------------------------------
Reporter: stephanm | Owner: nobody
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: master
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by felixxm):

* status: new => closed

* resolution: => invalid


Comment:

`sys.exit()` works fine, e.g.

{{{
class Command(BaseCommand):
def handle(self, *args, **options):
sys.exit(43)
}}}

{{{
> python manage.py test_command
> echo $?
43
}}}

All cleanup actions are honored (see
[https://docs.python.org/3.6/library/sys.html#sys.exit sys.exit
documentation]). Please use one of
[https://code.djangoproject.com/wiki/TicketClosingReasons/UseSupportChannels
support channels] if you have more questions.

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

Reply all
Reply to author
Forward
0 new messages