Opened 18 years ago
Closed 17 years ago
#7151 closed (fixed)
Management UnBoundLocalError error on local variable 'args' rreferenced before assignment
| Reported by: | trbs | Owned by: | nobody |
|---|---|---|---|
| Component: | Core (Management commands) | Version: | dev |
| Severity: | Keywords: | UnBoundLocalError, management, manage.py | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
With a malformed command like:
./manage.py help sql -h
Django management/__init__.py raises an UnboundLocalError:
UnboundLocalError: local variable 'args' referenced before assignment
Because the preprocessor tries to extract --settings and --pythonpath from an invalid command line, thus
raising an exception which is caught by the code and ignored.
(See full error message below)
The coding error in this is that it's possible to reach a code block while the 'args' variable is
still undefined. My solution (see patch attached) is to define the 'args' variable as None and check
if it's set or None before using it.
I've also attached a second patch which deals with this in a slightly different way. The problem is only
raised in the subcommand=='help' section, because the other sections use the unparsed self.argv instead of
the parsed args. There's technically no need for the help subcommand to use the parsed function and could very
well just return the help information on the command while ignoring the invalid latter part of the command.
Full Error:
$ PYTHONPATH='/home/trbs/django-command-extensions/' ./manage.py help export_emails -h
Usage: manage.py [options]
Options:
--settings=SETTINGS The Python path to a settings module, e.g.
"myproject.settings.main". If this isn't provided, the
DJANGO_SETTINGS_MODULE environment variable will be
used.
--pythonpath=PYTHONPATH
A directory to add to the Python path, e.g.
"/home/djangoprojects/myproject".
--traceback Print traceback on exception
--version show program's version number and exit
-h, --help show this help message and exit
Debug-Exception: <type 'exceptions.SystemExit'>
Traceback (most recent call last):
File "/home/trbs/django/core/management/__init__.py", line 194, in execute
options, args = parser.parse_args(self.argv)
File "/usr/lib/python2.5/optparse.py", line 1385, in parse_args
stop = self._process_args(largs, rargs, values)
File "/usr/lib/python2.5/optparse.py", line 1429, in _process_args
self._process_short_opts(rargs, values)
File "/usr/lib/python2.5/optparse.py", line 1536, in _process_short_opts
option.process(opt, value, values, self)
File "/usr/lib/python2.5/optparse.py", line 782, in process
self.action, self.dest, opt, value, values, parser)
File "/usr/lib/python2.5/optparse.py", line 805, in take_action
parser.exit()
File "/usr/lib/python2.5/optparse.py", line 1559, in exit
sys.exit(status)
SystemExit: 0
Traceback (most recent call last):
File "./manage.py", line 34, in <module>
execute(settings)
File "./manage.py", line 23, in execute
utility.execute()
File "/home/trbs/django/core/management/__init__.py", line 209, in execute
if len(args) > 2:
UnboundLocalError: local variable 'args' referenced before assignment
Attachments (3)
Change History (6)
by , 18 years ago
| Attachment: | 7151-patch1.diff added |
|---|
by , 18 years ago
| Attachment: | 7151-patch2.diff added |
|---|
comment:1 by , 17 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:3 by , 17 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
this apparently got fixed somewhere during normal development :)
(guessing with the updates to management commands)
Your patch has the remainings of a conflict in whatever VCS you use. Please upload a clean diff.