![]() get ( name, None ) if deps is not None : for dep in reversed ( deps ): if dep not in visited : _reload ( dep, visited ) # Clear this module's list of dependencies. # that we recursively call ourself to perform the nested reloads.ĭeps = _dependencies. add ( m ) # Start by reloading all of our dependencies in reverse order. # this set to avoid running into infinite recursion while walking the _name_ # Start by adding this module to our set of visited modules. import imp def _reload ( m, visited ): """Internal module reloading routine.""" name = m. The next step is to build a dependency-aware reload() routine. Theseĭependencies can be easily queried at runtime: def get_dependencies ( m ): """Get the dependency list for the given imported module.""" return _dependencies. This results in a complete set of per-module dependencies for all modules thatĪre imported after this custom import hook has been installed. ![]() Modules built-in extensions are ignored because they can’t be reloaded. Note that this code is only interested in file-based Top-level modules won’t have a parent.Īfter a module has been successfully imported, it is added to its parent’sĭependency list. ItĪlso tracks the current “parent” module, which is the module that is This code chains the built-in _import_ hook (stored in _baseimport). append ( m ) # Lastly, we always restore our global _parent pointer. If parent is not None and hasattr ( m, '_file_' ): l = _dependencies. # reloadable (source-based) module, we append ourself to our parent's M = _baseimport ( name, globals, locals, fromlist, level ) # If we have a parent (i.e. Global _parent parent = _parent _parent = name # Perform the actual import using the base import function. _import_ _dependencies = dict () _parent = None def _import ( name, globals = None, locals = None, fromlist = None, level =- 1 ): # Track our current parent module. Installing a custom import hook that is called as part of the regular module The ideal solution is to build aĭependency graph as the modules are loaded. ![]() They can be reloaded in the correct order. It is important to understand the dependencies between loaded modules so that The process automatic, transparent, and reliable. The following describes a general module reloading solution which aims to make Pre-test conditions, but it’s not a general solution for live code reloadingīecause the unloaded modules aren’t automatically reloaded. PyUnit’s solution is effective at restoring the interpreter’s state to To some previous state by overriding Python’s global _import_ hook. That approach “rolls back” the set of imported modules PyUnit deals with a variation of this problem by introducing a rollback To only reload module B after module A has been reloaded. The solution to this problem is to also reload module B. INTERVAL = 10 and just reload module A, any values in module B that wereīased on INTERVAL won’t be updated to reflect its new value. Into its namespace ( from A import INTERVAL). ![]() For example, imagine if module AĬontains the constant INTERVAL = 5, and module B imports that constant If any other code references symbols exported by the reloaded module, they may ![]() There are a few potential complications, however. Just reload() in Python 2.x), and the module will be reloaded Pass a module object to the imp.reload() function (or This allows developers to modify parts of a Python application Being able to reload code modules is one of the many nice features of ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |