Code

Ticket #3420: db.models.loading.diff

File db.models.loading.diff, 1.2 KB (added by egmanoj@…, 7 years ago)

Patch for displaying details such as file name, line number etc. for import errors that occur during 'validate'.

Line 
1Index: models/loading.py
2===================================================================
3--- models/loading.py   (revision 4541)
4+++ models/loading.py   (working copy)
5@@ -4,6 +4,7 @@
6 from django.core.exceptions import ImproperlyConfigured
7 import sys
8 import os
9+import traceback
10 
11 __all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models')
12 
13@@ -29,9 +30,14 @@
14                 load_app(app_name)
15             except Exception, e:
16                 # Problem importing the app
17-                _app_errors[app_name] = e
18+                _app_errors[app_name] = format_exception(e)
19     return _app_list
20 
21+def format_exception(exception):
22+    "Adds traceback information to the exception to assist debugging."
23+    filename, line_number, function_name, text = traceback.extract_tb(sys.exc_info()[-1])[-1]
24+    return "%s\n\tFile %s, line %s, in %s\n\t\t%s" % (exception, filename, line_number, function_name, text)
25+
26 def get_app(app_label, emptyOK=False):
27     "Returns the module containing the models for the given app_label. If the app has no models in it and 'emptyOK' is True, returns None."
28     get_apps() # Run get_apps() to populate the _app_list cache. Slightly hackish.