Ticket #9589: autoreload-r9410.patch

File autoreload-r9410.patch, 1.6 KB (added by eibaan, 16 years ago)
  • django/utils/autoreload.py

     
    8686
    8787def python_reloader(main_func, args, kwargs):
    8888    if os.environ.get("RUN_MAIN") == "true":
    89         thread.start_new_thread(main_func, args, kwargs)
     89        def watched_main_func():
     90            # Files of successfully loaded modules are watched for changes.
     91            # However, if loading does not succeed, no module is registered
     92            # and so that file isn't watched. Therefore, we add fake module
     93            # entries to `sys.modules` to watch all files which possibly
     94            # caused the exception so that fixing the problem will cause the
     95            # usual reload process.
     96            class FakeModule(object):
     97                def __init__(self, filename):
     98                    self.__file__ = filename
     99                    sys.modules[id(self)] = self
     100            try:
     101                main_func()
     102            except StandardError:
     103                t, v, tb = sys.exc_info()
     104                if t == SyntaxError:
     105                    FakeModule(v.filename)
     106                else:
     107                    while tb:
     108                        filename = tb.tb_frame.f_code.co_filename
     109                        if filename:
     110                            FakeModule(filename)
     111                        tb = tb.tb_next
     112                raise
     113        thread.start_new_thread(watched_main_func, args, kwargs)
    90114        try:
    91115            reloader_thread()
    92116        except KeyboardInterrupt:
Back to Top