Opened 8 years ago

Closed 8 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: master
Severity: Keywords: UnBoundLocalError, management,
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


With a malformed command like:

./ help sql -h

Django management/ 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/' ./ help export_emails -h
Usage: [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
                        A directory to add to the Python path, e.g.
  --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/", line 194, in execute
    options, args = parser.parse_args(self.argv)
  File "/usr/lib/python2.5/", line 1385, in parse_args
    stop = self._process_args(largs, rargs, values)
  File "/usr/lib/python2.5/", line 1429, in _process_args
    self._process_short_opts(rargs, values)
  File "/usr/lib/python2.5/", line 1536, in _process_short_opts
    option.process(opt, value, values, self)
  File "/usr/lib/python2.5/", line 782, in process
    self.action, self.dest, opt, value, values, parser)
  File "/usr/lib/python2.5/", line 805, in take_action
  File "/usr/lib/python2.5/", line 1559, in exit
SystemExit: 0
Traceback (most recent call last):
  File "./", line 34, in <module>
  File "./", line 23, in execute
  File "/home/trbs/django/core/management/", line 209, in execute
    if len(args) > 2:
UnboundLocalError: local variable 'args' referenced before assignment

Attachments (3)

7151-patch1.diff (2.1 KB) - added by trbs 8 years ago.
7151-patch2.diff (2.2 KB) - added by trbs 8 years ago.
7151-patch3.diff (632 bytes) - added by trbs 8 years ago.
clean version of the patch

Download all attachments as: .zip

Change History (6)

Changed 8 years ago by trbs

Changed 8 years ago by trbs

comment:1 Changed 8 years ago by telenieko

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Your patch has the remainings of a conflict in whatever VCS you use. Please upload a clean diff.

Changed 8 years ago by trbs

clean version of the patch

comment:2 Changed 8 years ago by trbs

added clean patch

comment:3 Changed 8 years ago by trbs

  • Resolution set to fixed
  • Status changed from new to closed

this apparently got fixed somewhere during normal development :)

(guessing with the updates to management commands)

Note: See TracTickets for help on using tickets.
Back to Top