Opened 8 years ago

Closed 8 years ago

#15946 closed Bug (fixed)

Exceptions used for program logic db/ load_backend

Reported by: sgronblo@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I noticed this problem when trying to figure out why one of my views wouldn't load. The problem there seems to be that it catches both ImportErrors and AttributeErrors in the same catch. I tried modifying the code to explode at when import_module calls import instead but then Django stopped working.

This is because of how db/'s load_backend tries to determine if you have used a non-package-prefixed name when specifying your db backend. load_backend does this by blindly prefixing the provded backend name with django.db.backends and then importing it. If the import succeeds Django thinks you used the short name and if it fails Django thinks you specified the prefixed name.

It would probably have been better to just check if the module name begins with django.db.backends or not instead of relying on import success/failure?

Change History (2)

comment:1 Changed 8 years ago by Aymeric Augustin

Third-party database backends have names not starting with django.db.backends, so your solution does not work. Example here:

comment:2 Changed 8 years ago by Luke Plant

Resolution: fixed
Status: newclosed

OK, I'm struggling to unravel what is actually being reported as a bug.

If you are saying that:

A) exceptions should not be used to decide program logic:

  1. This isn't a dogma that Pythonistas would generally accept. Exceptions are used in Python code in lots of cases in where they are not errors, and are used to determine program flow.
  2. The specific behaviour of load_backend was a temporary measure to support deprecated syntax, and it is now removed in trunk.

If you are saying that

B) import_module should be changed in some way:

  1. it's difficult to know what you are suggesting, because you were not explicit
  2. that code is taken from the standard library of Python 2.7, so you should be filing a bug with Python, not Django.

Since I think you are saying A, I'm closing 'fixed' because of A.2

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