Ticket #8413: 8413.patch

File 8413.patch, 3.2 KB (added by Yuri Baburov, 11 years ago)

Fix

  • django/utils/autoreload.py

    diff --git a/django/utils/autoreload.py b/django/utils/autoreload.py
    index 8d9d6f2..c878a51 100644
    a b  
    2727# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    2828# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    2929# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30from ihooks import ModuleImporter, ModuleLoader
     31import ihooks
    3032
    31 import os, sys, time
    32 
     33import os, sys, time, imp
    3334try:
    3435    import thread
    3536except ImportError:
    try: 
    4243except ImportError:
    4344    pass
    4445
     46try:
     47    set
     48except NameError:
     49    from sets import Set as set     # For Python 2.3
    4550
    4651RUN_RELOADER = True
    4752
    4853_mtimes = {}
    4954_win = (sys.platform == "win32")
    5055
     56class ModuleLoaderHook(ModuleLoader):
     57    __caught = set()
     58    def __init__(self, hooks = None, verbose = False):
     59        ModuleLoader.__init__(self, hooks, verbose)
     60        before = filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values()))
     61        for filename in before:
     62            self.note('Already loaded %s', filename)
     63        ModuleLoaderHook.__caught = set(before)
     64   
     65    def tracked(self):
     66        return list(self.__caught)
     67
     68    def load_module(self, name, stuff):
     69        file, filename, info = stuff
     70        if filename:
     71            if info[2] == imp.PKG_DIRECTORY:
     72                filename = filename+os.sep+'__init__.py'
     73            #DEBUG information, please remove
     74            if not filename in self.__caught:
     75                self.note('Caught %s', filename)
     76            self.__caught.add(filename)
     77        return ModuleLoader.load_module(self, name, stuff)
     78
     79class ModuleImporterHook(ModuleImporter):
     80    def ensure_fromlist(self, m, fromlist, recursive=0):
     81        try:
     82            ModuleImporter.ensure_fromlist(self, m, fromlist, recursive)
     83        except ValueError:
     84            # It's recommended to fix django to never reach this place
     85            # and remove this except case, see http://bugs.python.org/issue2090
     86            # and related django bugs: #6587 and #6579
     87            # XXX: Turn it on to catch empty-name module imports.
     88            # print 'Importing empty module with __import__:', m.__name__
     89            pass
     90        except ImportError:
     91            # ihooks catches missing fromlist imports by default
     92            pass   
     93
     94loader = None
     95def hook_imports(verbose=False):
     96    global loader
     97    loader = ModuleLoaderHook()
     98    importer = ModuleImporterHook(loader=loader, verbose=verbose)
     99    ihooks.install(importer)
     100
    51101def code_changed():
    52102    global _mtimes, _win
    53     for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())):
     103    files = filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values()))
     104    if loader:
     105        files = loader.tracked()
     106    for filename in files:
    54107        if filename.endswith(".pyc") or filename.endswith(".pyo"):
    55108            filename = filename[:-1]
    56109        if not os.path.exists(filename):
    def jython_reloader(main_func, args, kwargs): 
    107160
    108161
    109162def main(main_func, args=None, kwargs=None):
     163    hook_imports()
    110164    if args is None:
    111165        args = ()
    112166    if kwargs is None:
Back to Top