Ticket #9589: autoreload-error_decorator-r11628.patch

File autoreload-error_decorator-r11628.patch, 2.2 KB (added by berto, 5 years ago)

Added decorator that checks for file errors and adds them to watch list.

  • django/utils/autoreload.py

    diff --git a/django/utils/autoreload.py b/django/utils/autoreload.py
    index 8d9d6f2..011f7ab 100644
    a b  
    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.
    3030
    31 import os, sys, time
     31import os, sys, time, traceback
    3232
    3333try:
    3434    import thread
    RUN_RELOADER = True 
    4848_mtimes = {}
    4949_win = (sys.platform == "win32")
    5050
     51_error_files = []
     52
    5153def code_changed():
    5254    global _mtimes, _win
    53     for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())):
     55    for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())) + _error_files:
    5456        if filename.endswith(".pyc") or filename.endswith(".pyo"):
    5557            filename = filename[:-1]
    5658        if not os.path.exists(filename):
    def code_changed(): 
    6466            continue
    6567        if mtime != _mtimes[filename]:
    6668            _mtimes = {}
     69            try:
     70                del _error_files[_error_files.index(filename)]
     71            except ValueError: pass
    6772            return True
    6873    return False
    6974
     75def check_errors(fn):
     76    def wrapper(*args, **kwargs):
     77        try:
     78            fn(*args, **kwargs)
     79        except (ImportError, IndentationError,
     80                NameError, SyntaxError, TypeError), msg:
     81            et, ev, tb = sys.exc_info()
     82
     83            if getattr(ev, 'filename', None) is None:
     84                # get the filename from the last item in the stack
     85                filename = traceback.extract_tb(tb)[-1][0]
     86            else:
     87                filename = ev.filename
     88
     89            if filename not in _error_files:
     90                _error_files.append(filename)
     91
     92            raise
     93
     94    return wrapper
     95
    7096def reloader_thread():
    7197    while RUN_RELOADER:
    7298        if code_changed():
    def main(main_func, args=None, kwargs=None): 
    115141        reloader = jython_reloader
    116142    else:
    117143        reloader = python_reloader
    118     reloader(main_func, args, kwargs)
     144
     145    wrapped_main_func = check_errors(main_func)
     146    reloader(wrapped_main_func, args, kwargs)
    119147
Back to Top