﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
7151	Management UnBoundLocalError error on local variable 'args' rreferenced before assignment	trbs	nobody	"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
}}}"		closed	Core (Management commands)	dev		fixed	UnBoundLocalError, management, manage.py		Accepted	1	0	0	0	0	0
