📄 importer.py
字号:
if not self._cache.has_key(label): mtime = os.path.getmtime(file) cache = _CacheInfo(label, file, mtime) self._cache[label] = cache return (cache, True) # Grab entry from cache. cache = self._cache[label] # Check if reloads have been disabled. # Only avoid a reload though if module # hadn't been explicitly marked to be # reloaded. if not cache.reload: if self._frozen or not autoreload: return (cache, False) # Determine modification time of file. try: mtime = os.path.getmtime(file) except: # Must have been removed just then. We return # currently cached module and avoid a reload. # Defunct module would need to be purged later. msg = 'Module code file has been removed. ' msg = msg + 'This may cause problems. Using cached module. ' msg = msg + 'File being imported "%s".' % file self._log_warning(msg) return (cache, False) # Check if modification time has changed or # if module has been flagged to be reloaded. if cache.reload or mtime != cache.mtime: cache.mtime = mtime return (cache, True) # Check if children have changed in any way # that would require a reload. if cache.children: visited = {} ancestors = [label] for tag in cache.children: # If the child isn't in the cache any longer # for some reason, force a reload. if not self._cache.has_key(tag): return (cache, True) child = self._cache[tag] # Now check the actual child module. if self._check_module(modules, cache, child, visited, ancestors): return (cache, True) # No need to reload the module. Module # should be cached in the request object by # the caller if required. return (cache, False) finally: self._lock1.release() def _check_module(self, modules, parent, current, visited, ancestors): # Update current modules access statistics. current.indirect = current.indirect + 1 current.atime = time.time() # Check if current module has been marked # for reloading. if current.reload: return True # Check if current module has been reloaded # since the parent was last loaded. if current.generation > parent.generation: return True # If the current module has been visited # already, no need to continue further as it # should be up to date. if visited.has_key(current.label): return False # Check if current module has been modified on # disk since last loaded. try: mtime = os.path.getmtime(current.file) if mtime != current.mtime: return True except: # Current module must have been removed. # Don't cause this to force a reload though # as can cause problems. Rely on the parent # being modified to cause a reload. msg = 'Module code file has been removed. ' msg = msg + 'This may cause problems. Using cached module. ' msg = msg + 'File being imported "%s".' % current.file self._log_warning(msg) if modules is not None: modules[current.label] = current.module return False # Check to see if all the children of the # current module need updating or are newer than # the current module. if current.children: ancestors = ancestors + [current.label] for label in current.children.keys(): # Check for a child which refers to one of its # ancestors. Hopefully this will never occur. If # it does we will force a reload every time to # highlight there is a problem. Note this does # not get detected first time module is loaded, # only here on subsequent checks. If reloading # is not enabled, then problem will never be # detected and flagged. if label in ancestors: msg = 'Module imports an ancestor module. ' msg = msg + 'This may cause problems. Please check code. ' msg = msg + 'File doing import is "%s".' % current.file self._log_warning(msg) return True # If the child isn't in the cache any longer for # some reason, force a reload. if not self._cache.has_key(label): return True child = self._cache[label] # Recurse back into this function to check # child. if self._check_module(modules, current, child, visited, ancestors): return True # No need to reload the current module. Now safe # to mark the current module as having been # visited and cache it into the request object # for quick later lookup if a parent needs to be # reloaded. visited[current.label] = current if modules is not None: modules[current.label] = current.module return False def _module_label(self, file): # The label is used in the __name__ field of the # module and then used in determining child # module imports. Thus really needs to be # unique. We don't really want to use a module # name which is a filesystem path. Hope MD5 hex # digest is okay. return self._prefix + md5.new(file).hexdigest()_global_modules_cache = _ModuleCache()def _get_global_modules_cache(): return _global_modules_cacheapache.freeze_modules = _global_modules_cache.freeze_modulesapache.modules_graph = _global_modules_cache.modules_graphapache.module_info = _global_modules_cache.module_infoclass _ModuleLoader: def __init__(self, file): self.__file = file def load_module(self, fullname): return _global_modules_cache.import_module(self.__file)class _ModuleImporter: def find_module(self, fullname, path=None): # Return straight away if requested to import a # sub module of a package. if '.' in fullname: return None # Retrieve the parent context. That is, the # details stashed into the parent module by the # module importing system itself. Only consider # using the module importing system for 'import' # statement if parent module was imported using # the same. (parent_info, parent_path) = _parent_context() if parent_info is None: return None # Determine the list of directories that need to # be searched for a module code file. These # directories will be, the directory of the # parent and any specified search path. When # enabled, the handler root directory will also # be searched. search_path = [] directory = os.path.dirname(parent_info.file) search_path.append(directory) if parent_path is not None: search_path.extend(parent_path) options = get_current_options() if options.has_key('mod_python.importer.path'): directory = eval(options['mod_python.importer.path']) search_path.extend(directory) # Return if we have no search path to search. if not search_path: return None # Attempt to find the code file for the module. file = _find_module(fullname, search_path) if file is not None: return _ModuleLoader(file) return Nonesys.meta_path.insert(0, _ModuleImporter())# Replace mod_python.publisher page cache object with a# dummy object which uses new module importer.class _PageCache: def __getitem__(self,req): return import_module(req.filename)publisher.xpage_cache = publisher.page_cachepublisher.page_cache = _PageCache()# Define alternate implementations of the top level# mod_python entry points and substitute them for the# standard one in the 'mod_python.apache' callback# object._callback = apache._callback_callback.xConnectionDispatch = _callback.ConnectionDispatch_callback.xFilterDispatch = _callback.FilterDispatch_callback.xHandlerDispatch = _callback.HandlerDispatch_callback.xIncludeDispatch = _callback.IncludeDispatch_callback.xImportDispatch = _callback.ImportDispatch_callback.xReportError = _callback.ReportError_result_warning = """Handler has returned result or raised SERVER_RETURNexception with argument having non integer type. Type of value returnedwas %s, whereas expected """ + str(types.IntType) + "."_status_values = { "postreadrequesthandler": [ apache.DECLINED, apache.OK ], "transhandler": [ apache.DECLINED ], "maptostoragehandler": [ apache.DECLINED ], "inithandler": [ apache.DECLINED, apache.OK ], "headerparserhandler": [ apache.DECLINED, apache.OK ], "accesshandler": [ apache.DECLINED, apache.OK ], "authenhandler": [ apache.DECLINED ], "authzhandler": [ apache.DECLINED ], "typehandler": [ apache.DECLINED ], "fixuphandler": [ apache.DECLINED, apache.OK ], "loghandler": [ apache.DECLINED, apache.OK ], "handler": [ apache.DECLINED, apache.OK ],}def _execute_target(config, req, object, arg): try: # Only permit debugging using pdb if Apache has # actually been started in single process mode. pdb_debug = int(config.get("PythonEnablePdb", "0")) one_process = apache.exists_config_define("ONE_PROCESS") if pdb_debug and one_process: # Don't use pdb.runcall() as it results in # a bogus 'None' response when pdb session # is quit. With this code the exception # marking that the session has been quit is # propogated back up and it is obvious in # the error message what actually occurred. debugger = pdb.Pdb() debugger.reset() sys.settrace(debugger.trace_dispatch) try: result = object(arg) finally: debugger.quitting = 1 sys.settrace(None) else: result = object(arg) except apache.SERVER_RETURN, value: # For a connection handler, there is no request # object so this form of response is invalid. # Thus exception is reraised to be handled later. if not req: raise # The SERVER_RETURN exception type when raised # otherwise indicates an abort from below with # value as (result, status) or (result, None) or # result. if len(value.args) == 2: (result, status) = value.args if status: req.status = status else: result = value.args[0] # Only check type of return value for connection # handlers and request phase handlers. The return # value of filters are ultimately ignored. if not req or req == arg: assert (type(result) == types.IntType), _result_warning % type(result) return resultdef _process_target(config, req, directory, handler, default, arg, silent): if not callable(handler): # Determine module name and target object. parts = handler.split('::', 1) module_name = parts[0] if len(parts) == 1: object_str = default
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -