Code

#20489 closed Uncategorized (needsinfo)

Middleware importerror's are silently ignored

Reported by: foxwhisper Owned by: nobody
Component: Uncategorized Version: 1.4
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

Description

Originally posted in django, figured it would be more appropriate in here.. this is a long shot but.. has anyone here ever had any issues with load_middleware() ignoring ImportError exceptions from importlib.import_module? Specifically the import call on import_module is raising ImportError.. if i wrap that in a try/catch i can see it is an importerror.. but load_middleware() does not catch this exception.. even if i replace the except ImportError with except Exception, it still won't catch it. If i manually raise importerror BEFORE import happens, it will catch it.. if i manually raise after its happened, it wont catch it. this is driving me utterly insane. im wondering if there is some magic in import that i dont know about. ive even gone as far as making sure ImportError wasnt being monkeypatched somewhere.. so far, it just seems that ImportError being raised in import_module, doesn't seem to get catch back at load_middleware.. which seems impossible surely. i suspect this is due to my lack of understanding about how the django core really works. any hints/pointers would be very much appreciated.

It appears that import is not traversing down. if the middleware itself cannot be imported, then the importerror exception is raised and caught by load_middleware(). however if ImportError is raised by a middleware component itself, then the error is ignored silently. I tested this by putting a print statement before and after the import on importlib.py, along with the name of the module being imported, and a traceback print on the try/except i put in there.. i can see the exceptions being printed for all the importerror's that happened within the middleware, but the final result is that the middleware is still loaded and no exception is raised.

To replicate this problem, all you have to do is attempt to import a missing module from within a middleware module, and then run the WSGI server.

Even more amusingly, this only seems to happen when I run the WSGI server, not when I use shell or runserver.. but I suspect this is because of path differences which is causing this importerror in the first place.

So my questions are;

  • Is this behavior expected?
  • Should the load_middleware() not refuse to continue further if one of the middleware modules fails to load?
  • Am I missing something here?

Thanks

Cal

Attachments (0)

Change History (1)

comment:1 Changed 10 months ago by bak1an

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to needsinfo
  • Status changed from new to closed

Hi!

I was not able to reproduce your issue on sandbox project (python2.7, django 1.4, runserver|gunicorn|mod_wsgi). All import errors were raised right into my browser.
Can you provide more information or maybe sample project where issue can be spotted?

Originally posted in django

Sorry, can't find the thread. Can you post a link to the thread here?

Thanks.

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.