Code

Opened 3 years ago

Closed 3 years ago

#15946 closed Bug (fixed)

Exceptions used for program logic db/utils.py 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:

Description

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/utils.py'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?

Attachments (0)

Change History (2)

comment:1 Changed 3 years ago by aaugustin

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

Third-party database backends have names not starting with django.db.backends, so your solution does not work. Example here: http://code.google.com/p/ibm-db/wiki/ibm_db_django_README

comment:2 Changed 3 years ago by lukeplant

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

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.