Opened 12 years ago

Closed 12 years ago

Last modified 2 years ago

#18845 closed Bug (fixed)

Runtime error in setting.py causes silent exception and empty apps list

Reported by: ac@… Owned by: nobody
Component: Core (Management commands) Version: 1.4
Severity: Normal Keywords: management installed apps settings exception
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I had a runtime error in my settings.py file that wasn't being reported.

django/config/init.py : 94 was silently re-throwing, and
django/core/management/init.py : 103 was setting the INSTALLED_APPS list to an empty array.

I suggest the following changes:

django/config/init.py : 94 : except Exception, e:
django/core/management/init.py : 103 : sys.stdout.write('%s\n' % e)

This will result in the following printed on execution of any management command:

Could not import settings '<project>.settings' (Is it on sys.path?): <exception description here>

Thank you

Attachments (1)

18845-1.diff (3.0 KB ) - added by Claude Paroz 12 years ago.
Do not swallow too much exceptions

Download all attachments as: .zip

Change History (7)

comment:1 by Claude Paroz, 12 years ago

Can you give use an example of the type of error you had in your settings.py to reproduce the issue?

in reply to:  1 comment:2 by anonymous, 12 years ago

Replying to claudep:

Can you give use an example of the type of error you had in your settings.py to reproduce the issue?


Sure. I am putting a class Config in settings.py, containing various debug switches that I'm using further down in settings.py. My mistake was to refer to a class variable of Config that I forgot to set.

# settings.py
class Config:
    pass
A = Config.B

Whilst runserver fails with the correct AttributeError: class Config has no attribute 'B'

collectstatic fails silently/misleadingly with Unknown command: 'collectstatic'

Whilst it's true that had I simply run runserver I would have found the error (facedesk), it should be reported for all manage.py commands.

comment:3 by Claude Paroz, 12 years ago

Easy pickings: unset
Triage Stage: UnreviewedAccepted

Agreed that in your case, the exception should not be swallowed. Now the fix is not so trivial, because you have to take into account the case of the startproject command where accessing settings.INSTALLED_APPS will fail and we should not propagate the error in that case.

by Claude Paroz, 12 years ago

Attachment: 18845-1.diff added

Do not swallow too much exceptions

comment:4 by Claude Paroz, 12 years ago

Has patch: set

I do not know why get_commands() was swallowing AttributeError/EnvironmentError. Neither source code or commit provide any clue. So I suggest to only catch ImportError there.

comment:5 by Claude Paroz <claude@…>, 12 years ago

Resolution: fixed
Status: newclosed

In bb7da7844ff9f11286509c22a2549bbd4553d58d:

Fixed #18845 -- Do not swallow AttributeErrors when running commands

comment:6 by Anders Hovmöller, 2 years ago

This bug is not fixed. I'm not totally sure how to actually trigger it though, but I'm trying to help someone with this issue and it's not going well!

There's a bunch of people with this problem too: https://stackoverflow.com/questions/17804743/django-admin-py-unknown-command-collectstatic

I can fake trigger this by adding import foo inside ManagementUtility.execute:

        try:
            import foo
            settings.INSTALLED_APPS
        except ImproperlyConfigured as exc:
            self.settings_exception = exc
        except ImportError as exc:
            self.settings_exception = exc

Now I get the silent failure.

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