Opened 3 years ago

Closed 3 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 claudep 3 years ago.
Do not swallow too much exceptions

Download all attachments as: .zip

Change History (6)

comment:1 follow-up: Changed 3 years ago by claudep

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

comment:2 in reply to: ↑ 1 Changed 3 years ago by anonymous

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 Changed 3 years ago by claudep

  • Easy pickings unset
  • Triage Stage changed from Unreviewed to Accepted

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.

Changed 3 years ago by claudep

Do not swallow too much exceptions

comment:4 Changed 3 years ago by claudep

  • 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 Changed 3 years ago by Claude Paroz <claude@…>

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

In bb7da7844ff9f11286509c22a2549bbd4553d58d:

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

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