Robert Brewer committed 973d4f2

2.x backport of [1220] (Fix for #438 ( Server fails to start if a .pyc is imported with no corresponding .py)) and [1337] (New engine.autoreload.match attribute for filtering autoreload to a single package.)

Comments (0)

Files changed (1)


 # autoreloading launcher
 # stolen a lot from Ian Bicking's WSGIKit (
+import errno
 import os
+import re
 import sys
 import time
 import thread
 reloadFiles = []
 ignoreFiles = ['<string>']
+match = ".*"
 def reloader_thread(freq):
     mtimes = {}
-    def fileattr(m):
-        if hasattr(m, "__loader__"):
-            if hasattr(m.__loader__, "archive"):
-                return m.__loader__.archive
-        return getattr(m, "__file__", None)
     while RUN_RELOADER:
-        for filename in map(fileattr, sys.modules.values()) + reloadFiles:
+        sysfiles = []
+        for k, m in sys.modules.items():
+            if re.match(match, k):
+                if hasattr(m, "__loader__"):
+                    if hasattr(m.__loader__, "archive"):
+                        k = m.__loader__.archive
+                k = getattr(m, "__file__", None)
+                sysfiles.append(k)
+        for filename in sysfiles + reloadFiles:
             if filename and filename not in ignoreFiles:
+                orig = filename
                 if filename.endswith(".pyc"):
                     filename = filename[:-1]
+                # Get the last-modified time of the source file.
                     mtime = os.stat(filename).st_mtime
-                except OSError:
+                except OSError, e:
+                    if orig.endswith('.pyc') and e[0] == errno.ENOENT:
+                        # This prevents us from endlessly restarting if
+                        # there is an old .pyc lying around after a .py
+                        # file has been deleted. Note that TG's solution
+                        # actually deletes the .pyc, but we just ignore it.
+                        # See
+                        continue
                     sys.exit(3) # force reload
                 if filename not in mtimes:
                     mtimes[filename] = mtime
                 if mtime > mtimes[filename]:
                     sys.exit(3) # force reload
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.