Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.6.x] Fixed #21284 -- Prevented KeyError swallowing in fetch_command

Thanks wildfire for the report.

Backport of 3514bcb from master.
  • Loading branch information...
commit 621fc1f1d74df2d9240dea88b5f7ebdf472bca38 1 parent 37afcbe
@claudep claudep authored
Showing with 21 additions and 15 deletions.
  1. +3 −1 django/core/management/__init__.py
  2. +18 −14 tests/admin_scripts/tests.py
View
4 django/core/management/__init__.py
@@ -257,8 +257,10 @@ def fetch_command(self, subcommand):
appropriate command called from the command line (usually
"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:
- app_name = get_commands()[subcommand]
+ app_name = commands[subcommand]
except KeyError:
sys.stderr.write("Unknown command: %r\nType '%s help' for usage.\n" % \
(subcommand, self.prog_name))
View
32 tests/admin_scripts/tests.py
@@ -1004,28 +1004,24 @@ def test_custom_command_with_environment(self):
self.assertOutput(out, "EXECUTE:NoArgsCommand")
-class ManageSettingsWithImportError(AdminScriptTestCase):
- """Tests for manage.py when using the default settings.py file
- with an import error. Ticket #14130.
+class ManageSettingsWithSettingsErrors(AdminScriptTestCase):
+ """
+ Tests for manage.py when using the default settings.py file containing
+ runtime errors.
"""
def tearDown(self):
self.remove_settings('settings.py')
- def write_settings_with_import_error(self, filename, apps=None, is_dir=False, sdict=None):
- 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)
+ def write_settings_with_import_error(self, filename):
+ settings_file_path = os.path.join(test_dir, filename)
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('# 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
- when settings with import errors is provided
+ when settings with import errors is provided (#14130).
"""
self.write_settings_with_import_error('settings.py')
args = ['sqlall', 'admin_scripts']
@@ -1034,9 +1030,10 @@ def test_builtin_command(self):
self.assertOutput(err, "No module named")
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'})
args = ['collectstatic', 'admin_scripts']
@@ -1044,6 +1041,13 @@ def test_builtin_command_with_attribute_error(self):
self.assertNoOutput(out)
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):
def tearDown(self):
Please sign in to comment.
Something went wrong with that request. Please try again.