Ticket #8193: import_fixes-with-sys.modules.diff
File import_fixes-with-sys.modules.diff, 19.1 KB (added by , 16 years ago) |
---|
-
django/test/client.py
172 172 Obtains the current session variables. 173 173 """ 174 174 if 'django.contrib.sessions' in settings.INSTALLED_APPS: 175 engine = __import__(settings.SESSION_ENGINE, {}, {}, ['']) 175 __import__(settings.SESSION_ENGINE) 176 engine = sys.modules[settings.SESSION_ENGINE] 176 177 cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None) 177 178 if cookie: 178 179 return engine.SessionStore(cookie.value) … … 374 375 user = authenticate(**credentials) 375 376 if user and user.is_active \ 376 377 and 'django.contrib.sessions' in settings.INSTALLED_APPS: 377 engine = __import__(settings.SESSION_ENGINE, {}, {}, ['']) 378 __import__(settings.SESSION_ENGINE) 379 engine = sys.modules[settings.SESSION_ENGINE] 378 380 379 381 # Create a fake request to store login details. 380 382 request = HttpRequest() … … 409 411 410 412 Causes the authenticated user to be logged out. 411 413 """ 412 session = __import__(settings.SESSION_ENGINE, {}, {}, ['']).SessionStore() 414 __import__(settings.SESSION_ENGINE) 415 session = sys.modules[settings.SESSION_ENGINE].SessionStore() 413 416 session.delete(session_key=self.cookies[settings.SESSION_COOKIE_NAME].value) 414 417 self.cookies = SimpleCookie() -
django/conf/__init__.py
6 6 a list of all possible variables. 7 7 """ 8 8 9 import os9 import sys, os 10 10 import time # Needed for Windows 11 11 from django.conf import global_settings 12 12 … … 89 89 self.SETTINGS_MODULE = settings_module 90 90 91 91 try: 92 mod = __import__(self.SETTINGS_MODULE, {}, {}, ['']) 92 __import__(self.SETTINGS_MODULE) 93 mod = sys.modules[self.SETTINGS_MODULE] 93 94 except ImportError, e: 94 95 raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e) 95 96 … … 109 110 new_installed_apps = [] 110 111 for app in self.INSTALLED_APPS: 111 112 if app.endswith('.*'): 112 appdir = os.path.dirname(__import__(app[:-2], {}, {}, ['']).__file__) 113 __import__(app[:-2]) 114 appdir = os.path.dirname(sys.modules[app[:-2]].__file__) 113 115 app_subdirs = os.listdir(appdir) 114 116 app_subdirs.sort() 115 117 for d in app_subdirs: -
django/db/__init__.py
1 import os1 import sys, os 2 2 from django.conf import settings 3 3 from django.core import signals 4 4 from django.core.exceptions import ImproperlyConfigured … … 13 13 # Most of the time, the database backend will be one of the official 14 14 # backends that ships with Django, so look there first. 15 15 _import_path = 'django.db.backends.' 16 backend = __import__('%s%s.base' % (_import_path, settings.DATABASE_ENGINE), {}, {}, ['']) 16 _mod_name = '%s%s.base' % (_import_path, settings.DATABASE_ENGINE) 17 __import__(_mod_name) 18 backend = sys.modules[_mod_name] 17 19 except ImportError, e: 18 20 # If the import failed, we might be looking for a database backend 19 21 # distributed external to Django. So we'll try that next. 20 22 try: 21 23 _import_path = '' 22 backend = __import__('%s.base' % settings.DATABASE_ENGINE, {}, {}, ['']) 24 _mod_name = '%s.base' % settings.DATABASE_ENGINE 25 __import__(_mod_name) 26 backend = sys.modules[_mod_name] 23 27 except ImportError, e_user: 24 28 # The database backend wasn't found. Display a helpful error message 25 29 # listing all possible (built-in) database backends. -
django/core/serializers/__init__.py
16 16 17 17 """ 18 18 19 import sys 19 20 from django.conf import settings 20 21 21 22 # Built-in serializers … … 35 36 _serializers = {} 36 37 37 38 def register_serializer(format, serializer_module, serializers=None): 38 """"Register a new serializer. 39 39 """"Register a new serializer. 40 40 41 ``serializer_module`` should be the fully qualified module name 41 42 for the serializer. 42 43 43 44 If ``serializers`` is provided, the registration will be added 44 45 to the provided dictionary. 45 46 46 47 If ``serializers`` is not provided, the registration will be made 47 48 directly into the global register of serializers. Adding serializers 48 49 directly is not a thread-safe operation. 49 50 """ 50 module = __import__(serializer_module, {}, {}, ['']) 51 __import__(serializer_module) 52 module = sys.modules[serializer_module] 51 53 if serializers is None: 52 54 _serializers[format] = module 53 55 else: 54 56 serializers[format] = module 55 57 56 58 def unregister_serializer(format): 57 59 "Unregister a given serializer. This is not a thread-safe operation." 58 60 del _serializers[format] -
django/core/urlresolvers.py
7 7 (view_function, function_args, function_kwargs) 8 8 """ 9 9 10 import re10 import sys, re 11 11 12 12 from django.http import Http404 13 13 from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist … … 54 54 lookup_view = lookup_view.encode('ascii') 55 55 mod_name, func_name = get_mod_func(lookup_view) 56 56 if func_name != '': 57 lookup_view = getattr(__import__(mod_name, {}, {}, ['']), func_name) 57 __import__(mod_name) 58 lookup_view = getattr(sys.modules[mod_name], func_name) 58 59 if not callable(lookup_view): 59 60 raise AttributeError("'%s.%s' is not a callable." % (mod_name, func_name)) 60 61 except (ImportError, AttributeError): … … 195 196 try: 196 197 return self._urlconf_module 197 198 except AttributeError: 198 self._urlconf_module = __import__(self.urlconf_name, {}, {}, ['']) 199 __import__(self.urlconf_name) 200 self._urlconf_module = sys.modules[self.urlconf_name] 199 201 return self._urlconf_module 200 202 urlconf_module = property(_get_urlconf_module) 201 203 … … 207 209 callback = getattr(self.urlconf_module, 'handler%s' % view_type) 208 210 mod_name, func_name = get_mod_func(callback) 209 211 try: 210 return getattr(__import__(mod_name, {}, {}, ['']), func_name), {} 212 __import__(mod_name) 213 return getattr(sys.modules[mod_name], func_name), {} 211 214 except (ImportError, AttributeError), e: 212 215 raise ViewDoesNotExist, "Tried %s. Error was: %s" % (callback, str(e)) 213 216 -
django/core/handlers/base.py
35 35 raise exceptions.ImproperlyConfigured, '%s isn\'t a middleware module' % middleware_path 36 36 mw_module, mw_classname = middleware_path[:dot], middleware_path[dot+1:] 37 37 try: 38 mod = __import__(mw_module, {}, {}, ['']) 38 __import__(mw_module) 39 mod = sys.modules[mw_module] 39 40 except ImportError, e: 40 41 raise exceptions.ImproperlyConfigured, 'Error importing middleware %s: "%s"' % (mw_module, e) 41 42 try: -
django/core/files/storage.py
1 import os1 import sys, os 2 2 import errno 3 3 import urlparse 4 4 … … 219 219 raise ImproperlyConfigured("%s isn't a storage module." % import_path) 220 220 module, classname = import_path[:dot], import_path[dot+1:] 221 221 try: 222 mod = __import__(module, {}, {}, ['']) 222 __import__(module) 223 mod = sys.modules[module] 223 224 except ImportError, e: 224 225 raise ImproperlyConfigured('Error importing storage module %s: "%s"' % (module, e)) 225 226 try: -
django/core/cache/__init__.py
15 15 See docs/cache.txt for information on the public API. 16 16 """ 17 17 18 import sys 18 19 from cgi import parse_qsl 19 20 from django.conf import settings 20 21 from django.core import signals … … 49 50 host = host[:-1] 50 51 51 52 if scheme in BACKENDS: 52 module = __import__('django.core.cache.backends.%s' % BACKENDS[scheme], {}, {}, ['']) 53 modname = 'django.core.cache.backends.%s' % BACKENDS[scheme] 54 __import__(modname) 55 module = sys.modules[modname] 53 56 else: 54 module = __import__(scheme, {}, {}, ['']) 57 __import__(scheme) 58 module = sys.modules[scheme] 55 59 return getattr(module, 'CacheClass')(host, params) 56 60 57 61 cache = get_cache(settings.CACHE_BACKEND) -
django/core/management/commands/flush.py
23 23 # dispatcher events. 24 24 for app_name in settings.INSTALLED_APPS: 25 25 try: 26 __import__(app_name + '.management' , {}, {}, [''])26 __import__(app_name + '.management') 27 27 except ImportError: 28 28 pass 29 29 -
django/core/management/commands/syncdb.py
30 30 # dispatcher events. 31 31 for app_name in settings.INSTALLED_APPS: 32 32 try: 33 __import__(app_name + '.management' , {}, {}, [''])33 __import__(app_name + '.management') 34 34 except ImportError, exc: 35 35 # This is slightly hackish. We want to ignore ImportErrors 36 36 # if the "management" module itself is missing -- but we don't -
django/core/management/__init__.py
104 104 # Find the project directory 105 105 try: 106 106 from django.conf import settings 107 __import__(settings.SETTINGS_MODULE) 107 108 project_directory = setup_environ( 108 __import__( 109 settings.SETTINGS_MODULE, {}, {}, 110 (settings.SETTINGS_MODULE.split(".")[-1],) 111 ), settings.SETTINGS_MODULE 109 sys.modules[settings.SETTINGS_MODULE], 110 settings.SETTINGS_MODULE 112 111 ) 113 112 except (AttributeError, EnvironmentError, ImportError): 114 113 project_directory = None … … 313 312 project_name = os.path.basename(project_directory) 314 313 settings_name = os.path.splitext(settings_filename)[0] 315 314 sys.path.append(os.path.join(project_directory, os.pardir)) 316 project_module = __import__(project_name, {}, {}, ['']) 315 __import__(project_name) 316 project_module = sys.modules[project_name] 317 317 sys.path.pop() 318 318 319 319 # Set DJANGO_SETTINGS_MODULE appropriately. -
django/templatetags/__init__.py
1 import sys 1 2 from django.conf import settings 2 3 3 4 for a in settings.INSTALLED_APPS: … … 2 3 try: 3 __path__.extend(__import__(a + '.templatetags', {}, {}, ['']).__path__) 4 modname = a + '.templatetags' 5 __import__(modname) 6 __path__.extend(sys.modules[modname].__path__) 4 7 except ImportError: -
django/views/i18n.py
2 2 from django.utils.translation import check_for_language, activate, to_locale, get_language 3 3 from django.utils.text import javascript_quote 4 4 from django.conf import settings 5 import os5 import sys, os 6 6 import gettext as gettext_module 7 7 8 8 def set_language(request): … … 128 128 paths = [] 129 129 # first load all english languages files for defaults 130 130 for package in packages: 131 p = __import__(package, {}, {}, ['']) 131 __import__(package) 132 p = sys.modules[package] 132 133 path = os.path.join(os.path.dirname(p.__file__), 'locale') 133 134 paths.append(path) 134 135 try: -
django/contrib/comments/__init__.py
1 import sys 1 2 from django.conf import settings 2 3 from django.core import urlresolvers 3 4 from django.core.exceptions import ImproperlyConfigured … … 17 18 18 19 # Try to import the package 19 20 try: 20 package = __import__(comments_app, '', '', ['']) 21 __import__(comments_app) 22 package = sys.modules[comments_app] 21 23 except ImportError: 22 24 raise ImproperlyConfigured("The COMMENTS_APP setting refers to "\ 23 25 "a non-existing package.") -
django/contrib/admin/views/template.py
1 import sys 1 2 from django import template, forms 2 3 from django.contrib.admin.views.decorators import staff_member_required 3 4 from django.template import loader … … 15 16 # get a dict of {site_id : settings_module} for the validator 16 17 settings_modules = {} 17 18 for mod in settings.ADMIN_FOR: 18 settings_module = __import__(mod, {}, {}, ['']) 19 __import__(mod) 20 settings_module = sys.modules[mod] 19 21 settings_modules[settings_module.SITE_ID] = settings_module 20 22 site_list = Site.objects.in_bulk(settings_modules.keys()).values() 21 23 if request.POST: -
django/contrib/admindocs/views.py
11 11 from django.contrib.sites.models import Site 12 12 from django.utils.translation import ugettext as _ 13 13 from django.utils.safestring import mark_safe 14 import inspect, os, re14 import sys, inspect, os, re 15 15 16 16 # Exclude methods starting with these strings from documentation 17 17 MODEL_METHODS_EXCLUDE = ('_', 'add_', 'delete', 'save', 'set_') … … 114 114 return missing_docutils_page(request) 115 115 116 116 if settings.ADMIN_FOR: 117 settings_modules = [__import__(m, {}, {}, ['']) for m in settings.ADMIN_FOR] 117 settings_modules = [] 118 for m in settings.ADMIN_FOR: 119 __import__(m) 120 settings_modules.append(sys.modules[m]) 118 121 else: 119 122 settings_modules = [settings] 120 123 121 124 views = [] 122 125 for settings_mod in settings_modules: 123 urlconf = __import__(settings_mod.ROOT_URLCONF, {}, {}, ['']) 126 __import__(settings_mod.ROOT_URLCONF) 127 urlconf = sys.modules[settings_mod.ROOT_URLCONF] 124 128 view_functions = extract_views_from_urlpatterns(urlconf.urlpatterns) 125 129 if Site._meta.installed: 126 130 site_obj = Site.objects.get(pk=settings_mod.SITE_ID) … … 146 150 147 151 mod, func = urlresolvers.get_mod_func(view) 148 152 try: 149 view_func = getattr(__import__(mod, {}, {}, ['']), func) 153 __import__(mod) 154 view_func = getattr(sys.modules[mod], func) 150 155 except (ImportError, AttributeError): 151 156 raise Http404 152 157 title, body, metadata = utils.parse_docstring(view_func.__doc__) … … 257 262 def template_detail(request, template): 258 263 templates = [] 259 264 for site_settings_module in settings.ADMIN_FOR: 260 settings_mod = __import__(site_settings_module, {}, {}, ['']) 265 __import__(site_settings_module) 266 settings_mod = sys.modules[site_settings_module] 261 267 if Site._meta.installed: 262 268 site_obj = Site.objects.get(pk=settings_mod.SITE_ID) 263 269 else: -
django/contrib/sessions/middleware.py
1 import time1 import sys, time 2 2 3 3 from django.conf import settings 4 4 from django.utils.cache import patch_vary_headers … … 6 6 7 7 class SessionMiddleware(object): 8 8 def process_request(self, request): 9 engine = __import__(settings.SESSION_ENGINE, {}, {}, ['']) 9 # FIXME: __import__ on EVERY request?! Move to __init__ 10 __import__(settings.SESSION_ENGINE) 11 engine = sys.modules[settings.SESSION_ENGINE] 10 12 session_key = request.COOKIES.get(settings.SESSION_COOKIE_NAME, None) 11 13 request.session = engine.SessionStore(session_key) 12 14 -
django/template/__init__.py
48 48 >>> t.render(c) 49 49 u'<html></html>' 50 50 """ 51 import re51 import sys, re 52 52 from inspect import getargspec 53 53 from django.conf import settings 54 54 from django.template.context import Context, RequestContext, ContextPopException … … 934 934 lib = libraries.get(module_name, None) 935 935 if not lib: 936 936 try: 937 mod = __import__(module_name, {}, {}, ['']) 937 __import__(module_name) 938 mod = sys.modules[module_name] 938 939 except ImportError, e: 939 940 raise InvalidTemplateLibrary("Could not load template library from %s, %s" % (module_name, e)) 940 941 try: -
tests/regressiontests/dynamic_imports/tests.py
1 __test__ = {'API_TESTS': """ 2 >>> import sys 3 >>> import os 4 >>> mod_name = os.environ['DJANGO_SETTINGS_MODULE'] 5 >>> mod = __import__(mod_name, {}, {}, [mod_name.split('.')[-1]]) 6 >>> from django.core.management import setup_environ 7 >>> d = setup_environ(mod) 8 >>> [k for k in sys.modules if k.endswith('.') or '..' in k] 9 [] 10 """}