Ticket #9589: autoreload.diff

File autoreload.diff, 2.7 KB (added by Glenn, 6 years ago)

fixed 'exceptions.TypeError' object has no attribute 'filename'

  • django/utils/autoreload.py

     
    4747
    4848_mtimes = {}
    4949_win = (sys.platform == "win32")
     50_extra_files_to_monitor = []
    5051
    5152def code_changed():
    52     global _mtimes, _win
    53     for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())):
     53    def check(filename):
     54        global _mtimes, _win
    5455        if filename.endswith(".pyc") or filename.endswith(".pyo"):
    5556            filename = filename[:-1]
    5657        if not os.path.exists(filename):
    57             continue # File might be in an egg, so it can't be reloaded.
     58            return False # File might be in an egg, so it can't be reloaded.
    5859        stat = os.stat(filename)
    5960        mtime = stat.st_mtime
    6061        if _win:
    6162            mtime -= stat.st_ctime
    6263        if filename not in _mtimes:
    6364            _mtimes[filename] = mtime
    64             continue
     65            return False
    6566        if mtime != _mtimes[filename]:
    6667            _mtimes = {}
    6768            return True
     69
     70    for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())):
     71        if check(filename): return True
     72    for filename in _extra_files_to_monitor:
     73        if check(filename): return True
    6874    return False
    6975
    7076def reloader_thread():
     
    8490        if exit_code != 3:
    8591            return exit_code
    8692
     93def watched_main_func(*args, **kwargs):
     94    try:
     95        return args[0](*args[1:], **kwargs)
     96    except StandardError:
     97        # Keep track of files that fail to load, so we can still try to
     98        # monitor them for changes.  This won't work if the cause of the
     99        # exception is external.
     100        t, v, tb = sys.exc_info()
     101        if t == SyntaxError:
     102            _extra_files_to_monitor.append(v.filename)
     103        else:
     104            while tb:
     105                filename = tb.tb_frame.f_code.co_filename
     106                if filename:
     107                    _extra_files_to_monitor.append(filename)
     108                tb = tb.tb_next
     109        raise
     110
    87111def python_reloader(main_func, args, kwargs):
    88112    if os.environ.get("RUN_MAIN") == "true":
    89         thread.start_new_thread(main_func, args, kwargs)
     113        thread.start_new_thread(watched_main_func, (main_func,) + args, kwargs)
    90114        try:
    91115            reloader_thread()
    92116        except KeyboardInterrupt:
     
    99123
    100124def jython_reloader(main_func, args, kwargs):
    101125    from _systemrestart import SystemRestart
    102     thread.start_new_thread(main_func, args)
     126    thread.start_new_thread(watched_main_func, (main_func,) + args, kwargs)
    103127    while True:
    104128        if code_changed():
    105129            raise SystemRestart
Back to Top