Skip to content

Commit

Permalink
Extract module to file mapping as _file_for_module classmethod.
Browse files Browse the repository at this point in the history
Avoids condition where zip check always produces an AttributeError in the failure stack even when irrelevant.
  • Loading branch information
jaraco committed May 28, 2018
1 parent 9110766 commit 4ce8547
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions cherrypy/process/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,27 +627,40 @@ def start(self):

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."""
Expand Down

0 comments on commit 4ce8547

Please sign in to comment.