Ticket #8413: 8413_2.patch

File 8413_2.patch, 2.1 KB (added by Yuri Baburov, 10 years ago)

Better fix working through PEP 302

  • django/utils/autoreload.py

    diff --git a/django/utils/autoreload.py b/django/utils/autoreload.py
    index 8d9d6f2..d15abb6 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.
    30 
    31 import os, sys, time
    32 
     30import os, sys, time, imp
    3331try:
    3432    import thread
    3533except ImportError:
    try: 
    4240except ImportError:
    4341    pass
    4442
     43try:
     44    set
     45except NameError:
     46    from sets import Set as set     # For Python 2.3
    4547
    4648RUN_RELOADER = True
    4749
    4850_mtimes = {}
    4951_win = (sys.platform == "win32")
    5052
     53def get_loaded_modules():
     54    return filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values()))
     55
     56class ModuleLoaderHook:
     57    __caught = set()
     58    def __init__(self):
     59        before = get_loaded_modules()
     60        ModuleLoaderHook.__caught = set(before)
     61   
     62    def tracked(self):
     63        return list(self.__caught)
     64
     65    def find_module(self, name, path):
     66        try:
     67            if path:
     68                name = name.split('.')[-1]
     69            file, filename, _info = imp.find_module(name, path)
     70            if file:
     71                self.__caught.add(filename)
     72                file.close()
     73        except:
     74            pass
     75        return None #as it was not found
     76
     77loader = None
     78def hook_imports():
     79    global loader
     80    loader = ModuleLoaderHook()
     81    sys.meta_path.insert(0, loader)
     82
    5183def code_changed():
    5284    global _mtimes, _win
    53     for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())):
     85    files = get_loaded_modules()
     86    if loader:
     87        files = loader.tracked()
     88    for filename in files:
    5489        if filename.endswith(".pyc") or filename.endswith(".pyo"):
    5590            filename = filename[:-1]
    5691        if not os.path.exists(filename):
    def jython_reloader(main_func, args, kwargs): 
    107142
    108143
    109144def main(main_func, args=None, kwargs=None):
     145    hook_imports()
    110146    if args is None:
    111147        args = ()
    112148    if kwargs is None:
Back to Top