[Django] #32183: Incorrect exec usage in `shell -c` command prevents functions from working

27 views
Skip to first unread message

Django

unread,
Nov 9, 2020, 5:19:59 PM11/9/20
to django-...@googlegroups.com
#32183: Incorrect exec usage in `shell -c` command prevents functions from working
-------------------------------------+-------------------------------------
Reporter: William | Owner: nobody
Schwartz |
Type: Bug | Status: new
Component: Core | Version: master
(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 |
-------------------------------------+-------------------------------------
The examples below use Python 3.7 and Django 2.2.16, but I checked that
the code is the same on master and works the same in Python 3.8.

Here's how [https://docs.python.org/3/using/cmdline.html#cmdoption-c
python -c] works:

{{{
$ python -c <<EOF "
import django
def f():
print(django.__version__)
f()"
EOF
2.2.16
}}}

Here's how [https://docs.djangoproject.com/en/3.1/ref/django-admin
/#cmdoption-shell-command python -m django shell -c] works (paths
shortened for clarify):
{{{
$ python -m django shell -c <<EOF "
import django
def f():
print(django.__version__)
f()"
EOF
Traceback (most recent call last):
File "{sys.base_prefix}/lib/python3.7/runpy.py", line 193, in
_run_module_as_main
"__main__", mod_spec)
File "{sys.base_prefix}/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "{sys.prefix}/lib/python3.7/site-packages/django/__main__.py", line
9, in <module>
management.execute_from_command_line()
File "{sys.prefix}/lib/python3.7/site-
packages/django/core/management/__init__.py", line 381, in
execute_from_command_line
utility.execute()
File "{sys.prefix}/lib/python3.7/site-
packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "{sys.prefix}/lib/python3.7/site-
packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "{sys.prefix}/lib/python3.7/site-
packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "{sys.prefix}/lib/python3.7/site-
packages/django/core/management/commands/shell.py", line 86, in handle
exec(options['command'])
File "<string>", line 5, in <module>
File "<string>", line 4, in f
NameError: name 'django' is not defined
}}}

The problem is in the
[https://github.com/django/django/blob/dbb4a86fa7c15fb9cf73a5954356eb88d65ac25f/django/core/management/commands/shell.py#L84-L94
usage] of [https://docs.python.org/3/library/functions.html#exec exec]:
{{{
#!python
def handle(self, **options):
# Execute the command and exit.
if options['command']:
exec(options['command'])
return


# Execute stdin if it has anything to read and exit.
# Not supported on Windows due to select.select() limitations.
if sys.platform != 'win32' and not sys.stdin.isatty() and
select.select([sys.stdin], [], [], 0)[0]:
exec(sys.stdin.read())
return
}}}

`exec` should be passed a dictionary containing a minimal set of globals.
This can be done by just passing a new, empty dictionary as the second
argument of `exec`.

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

Django

unread,
Nov 9, 2020, 5:46:36 PM11/9/20
to django-...@googlegroups.com
#32183: Incorrect exec usage in `shell -c` command prevents functions from working
-------------------------------------+-------------------------------------
Reporter: William Schwartz | Owner: William
| Schwartz
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: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by William Schwartz):

* owner: nobody => William Schwartz
* status: new => assigned
* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/13660 PR] includes tests and
documents the new feature in the release notes (but not in the main docs
since it seems more like a bug fix than a new feature to me).

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

Django

unread,
Nov 10, 2020, 2:54:28 AM11/10/20
to django-...@googlegroups.com
#32183: Incorrect exec usage in `shell -c` command prevents functions from working
-------------------------------------+-------------------------------------
Reporter: William Schwartz | Owner: William
| Schwartz
Type: Bug | Status: assigned
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

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

* stage: Unreviewed => Accepted


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

Django

unread,
Nov 10, 2020, 3:04:50 AM11/10/20
to django-...@googlegroups.com
#32183: shell command crashes when passing (with -c) the python code with
functions.

-------------------------------------+-------------------------------------
Reporter: William Schwartz | Owner: William
| Schwartz
Type: Bug | Status: assigned
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

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

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

Django

unread,
Nov 10, 2020, 3:25:43 AM11/10/20
to django-...@googlegroups.com
#32183: shell command crashes when passing (with -c) the python code with
functions.
-------------------------------------+-------------------------------------
Reporter: William Schwartz | Owner: William
| Schwartz
Type: Bug | Status: assigned
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

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

* needs_better_patch: 0 => 1


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

Django

unread,
Nov 11, 2020, 3:21:23 AM11/11/20
to django-...@googlegroups.com
#32183: shell command crashes when passing (with -c) the python code with
functions.
-------------------------------------+-------------------------------------
Reporter: William Schwartz | Owner: William
| Schwartz
Type: Bug | Status: assigned
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

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

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/32183#comment:5>

Django

unread,
Nov 11, 2020, 4:55:07 AM11/11/20
to django-...@googlegroups.com
#32183: shell command crashes when passing (with -c) the python code with
functions.
-------------------------------------+-------------------------------------
Reporter: William Schwartz | Owner: William
| Schwartz
Type: Bug | Status: closed

Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"c0fc5ba3808becefcdc8b878f133fd2a864a072d" c0fc5ba]:
{{{
#!CommitTicketReference repository=""
revision="c0fc5ba3808becefcdc8b878f133fd2a864a072d"
Fixed #32183 -- Fixed shell crash when passing code with nested scopes.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32183#comment:6>

Reply all
Reply to author
Forward
0 new messages