Ticket #11696: 20091107.diff
File 20091107.diff, 3.5 KB (added by , 15 years ago) |
---|
-
django/db/models/loading.py
8 8 import sys 9 9 import os 10 10 import threading 11 import imp 12 import traceback 11 13 12 14 __all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models', 13 15 'load_app', 'app_cache_ready') 14 16 17 def module_exists(full_name,path=None): 18 """ 19 tests whether the module exists but does not import it. 20 see http://www.python.org/doc/current/library/imp.html#module-imp 21 """ 22 a = full_name.split('.',1) 23 if len(a) == 1: 24 # simple module name without package 25 try: 26 (file, pathname, description) = imp.find_module(full_name,path) 27 if file is not None: file.close() 28 except ImportError,e: 29 return False 30 return True 31 assert len(a) == 2 32 (file, pathname, description) = imp.find_module(a[0],path) 33 if description[-1] != imp.PKG_DIRECTORY: 34 return False 35 pkg = imp.load_module(a[0],file,pathname,description) 36 if file is not None: file.close() 37 return module_exists(a[1],pkg.__path__) 38 39 40 41 15 42 class AppCache(object): 16 43 """ 17 44 A cache that stores installed applications and their models. Used to … … 57 84 continue 58 85 self.load_app(app_name, True) 59 86 if not self.nesting_level: 60 for app_name in self.postponed: 87 postponed = self.postponed 88 self.postponed = [] 89 for app_name,tb in postponed: 61 90 self.load_app(app_name) 62 91 self.loaded = True 92 if len(self.postponed): 93 msg = ''.join(["%s:\n%s" % i for i in self.postponed]) 94 raise ImproperlyConfigured( 95 "Failed to import %d applications:\n%s" % ( 96 len(self.postponed),msg)) 63 97 finally: 64 98 self.write_lock.release() 65 99 66 100 def load_app(self, app_name, can_postpone=False): 67 101 """ 68 102 Loads the app with the provided fully qualified name, and returns the 69 103 model module. 70 104 """ 71 105 self.handled[app_name] = None 106 if not module_exists(app_name + ".models"): 107 # silently ignore applications without models 108 #print "[DEBUG] application %s has no models" % app_name 109 return None 72 110 self.nesting_level += 1 73 111 try: 74 112 models = import_module('.models', app_name) 75 except ImportError :113 except ImportError,e: 76 114 self.nesting_level -= 1 115 self.postponed.append( (app_name,traceback.format_exc(e)) ) 77 116 if can_postpone: 78 # Either the app has no models, or the package isstill being117 # The package is maybe still being 79 118 # imported by Python and the model module isn't available yet. 80 119 # We will check again once all the recursion has finished (in 81 120 # populate). 82 self.postponed.append(app_name) 83 return None 121 #print "[DEBUG] Postponing application %s:" % app_name 122 #traceback.print_exc(e) 123 return None 124 traceback.print_exc(e) 125 raise 84 126 self.nesting_level -= 1 85 127 if models not in self.app_store: 86 128 self.app_store[models] = len(self.app_store)