summaryrefslogtreecommitdiff
path: root/cherrypy/process
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2018-05-28 14:06:25 -0400
committerJason R. Coombs <jaraco@jaraco.com>2018-05-28 14:14:56 -0400
commit4ce85477f4d2a049c3d702b0a37a7d22cbfa4d5d (patch)
tree65b3c1cc39ff48bc4b8bc48a2aa9ed3556dc9b38 /cherrypy/process
parent91107666a5c37aabe3a79971bb8324645c78a734 (diff)
downloadcherrypy-git-4ce85477f4d2a049c3d702b0a37a7d22cbfa4d5d.tar.gz
Extract module to file mapping as _file_for_module classmethod.
Avoids condition where zip check always produces an AttributeError in the failure stack even when irrelevant.
Diffstat (limited to 'cherrypy/process')
-rw-r--r--cherrypy/process/plugins.py51
1 files changed, 32 insertions, 19 deletions
diff --git a/cherrypy/process/plugins.py b/cherrypy/process/plugins.py
index 37913a61..506c1b7f 100644
--- a/cherrypy/process/plugins.py
+++ b/cherrypy/process/plugins.py
@@ -627,27 +627,40 @@ class Autoreloader(Monitor):
def sysfiles(self):
"""Return a Set of sys.modules filenames to monitor."""
- files = set()
-
search_mod_names = filter(re.compile(self.match).match, sys.modules)
- for name in search_mod_names:
- m = sys.modules[name]
+ mods = map(sys.modules.get, search_mod_names)
+ return set(filter(None, map(self._file_for_module, mods)))
- try:
- f = m.__loader__.archive
- except AttributeError:
- try:
- f = m.__file__
- if not os.path.isabs(f):
- # ensure absolute paths so a os.chdir() in the app
- # doesn't break me
- f = os.path.normpath(
- os.path.join(_module__file__base, f)
- )
- except AttributeError:
- continue
- files.add(f)
- return files
+ @classmethod
+ def _file_for_module(cls, module):
+ """Return the relevant file for the module."""
+ return (
+ cls._archive_for_zip_module(module)
+ or cls._file_for_file_module(module)
+ )
+
+ @staticmethod
+ def _archive_for_zip_module(module):
+ """Return the archive filename for the module if relevant."""
+ try:
+ return module.__loader__.archive
+ except AttributeError:
+ pass
+
+ @classmethod
+ def _file_for_file_module(cls, module):
+ """Return the file for the module."""
+ try:
+ return cls._make_absolute(module.__file__)
+ except AttributeError:
+ pass
+
+ @staticmethod
+ def _make_absolute(filename):
+ """Ensure filename is absolute to avoid effect of os.chdir."""
+ return filename if os.path.isabs(filename) else (
+ os.path.normpath(os.path.join(_module__file__base, filename))
+ )
def run(self):
"""Reload the process if registered files have been modified."""