Fixed #21284 -- Prevented KeyError swallowing in fetch_command

Thanks wildfire for the report.
This commit is contained in:
Claude Paroz 2013-10-17 18:57:44 +02:00
parent a14f087233
commit 3514bcb251
2 changed files with 21 additions and 15 deletions

View File

@ -257,8 +257,10 @@ class ManagementUtility(object):
appropriate command called from the command line (usually appropriate command called from the command line (usually
"django-admin.py" or "manage.py") if it can't be found. "django-admin.py" or "manage.py") if it can't be found.
""" """
# Get commands outside of try block to prevent swallowing exceptions
commands = get_commands()
try: try:
app_name = get_commands()[subcommand] app_name = commands[subcommand]
except KeyError: except KeyError:
sys.stderr.write("Unknown command: %r\nType '%s help' for usage.\n" % \ sys.stderr.write("Unknown command: %r\nType '%s help' for usage.\n" % \
(subcommand, self.prog_name)) (subcommand, self.prog_name))

View File

@ -1011,28 +1011,24 @@ class ManageMultipleSettings(AdminScriptTestCase):
self.assertOutput(out, "EXECUTE:NoArgsCommand") self.assertOutput(out, "EXECUTE:NoArgsCommand")
class ManageSettingsWithImportError(AdminScriptTestCase): class ManageSettingsWithSettingsErrors(AdminScriptTestCase):
"""Tests for manage.py when using the default settings.py file """
with an import error. Ticket #14130. Tests for manage.py when using the default settings.py file containing
runtime errors.
""" """
def tearDown(self): def tearDown(self):
self.remove_settings('settings.py') self.remove_settings('settings.py')
def write_settings_with_import_error(self, filename, apps=None, is_dir=False, sdict=None): def write_settings_with_import_error(self, filename):
if is_dir:
settings_dir = os.path.join(test_dir, filename)
os.mkdir(settings_dir)
settings_file_path = os.path.join(settings_dir, '__init__.py')
else:
settings_file_path = os.path.join(test_dir, filename) settings_file_path = os.path.join(test_dir, filename)
with open(settings_file_path, 'w') as settings_file: with open(settings_file_path, 'w') as settings_file:
settings_file.write('# Settings file automatically generated by admin_scripts test case\n') settings_file.write('# Settings file automatically generated by admin_scripts test case\n')
settings_file.write('# The next line will cause an import error:\nimport foo42bar\n') settings_file.write('# The next line will cause an import error:\nimport foo42bar\n')
def test_builtin_command(self): def test_import_error(self):
""" """
import error: manage.py builtin commands shows useful diagnostic info import error: manage.py builtin commands shows useful diagnostic info
when settings with import errors is provided when settings with import errors is provided (#14130).
""" """
self.write_settings_with_import_error('settings.py') self.write_settings_with_import_error('settings.py')
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
@ -1041,9 +1037,10 @@ class ManageSettingsWithImportError(AdminScriptTestCase):
self.assertOutput(err, "No module named") self.assertOutput(err, "No module named")
self.assertOutput(err, "foo42bar") self.assertOutput(err, "foo42bar")
def test_builtin_command_with_attribute_error(self): def test_attribute_error(self):
""" """
manage.py builtin commands does not swallow attribute errors from bad settings (#18845) manage.py builtin commands does not swallow attribute error due to bad
settings (#18845).
""" """
self.write_settings('settings.py', sdict={'BAD_VAR': 'INSTALLED_APPS.crash'}) self.write_settings('settings.py', sdict={'BAD_VAR': 'INSTALLED_APPS.crash'})
args = ['collectstatic', 'admin_scripts'] args = ['collectstatic', 'admin_scripts']
@ -1051,6 +1048,13 @@ class ManageSettingsWithImportError(AdminScriptTestCase):
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "AttributeError: 'list' object has no attribute 'crash'") self.assertOutput(err, "AttributeError: 'list' object has no attribute 'crash'")
def test_key_error(self):
self.write_settings('settings.py', sdict={'BAD_VAR': 'DATABASES["blah"]'})
args = ['collectstatic', 'admin_scripts']
out, err = self.run_manage(args)
self.assertNoOutput(out)
self.assertOutput(err, "KeyError: 'blah'")
class ManageValidate(AdminScriptTestCase): class ManageValidate(AdminScriptTestCase):
def tearDown(self): def tearDown(self):