Middleware importerror's are silently ignored
|Reported by:||foxwhisper||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
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?