diff --git a/mitogen/master.py b/mitogen/master.py index dca4eb46..c6c04d7d 100644 --- a/mitogen/master.py +++ b/mitogen/master.py @@ -34,6 +34,7 @@ import logging import os import pkgutil import re +import string import sys import threading import types @@ -349,11 +350,28 @@ class ModuleFinder(object): return False + def _looks_like_script(self, path): + """ + Return :data:`True` if the (possibly extensionless) file at `path` + resembles a Python script. For now we simply verify the file contains + ASCII text. + """ + fp = open(path, 'r') + try: + return not set(fp.read(512)).difference(string.printable) + finally: + fp.close() + def _py_filename(self, path): - path = path.rstrip('co') + if path[-4:] in ('.pyc', '.pyo'): + path = path.rstrip('co') + if path.endswith('.py'): return path + if os.path.exists(path) and self._looks_like_script(path): + return path + def _get_module_via_pkgutil(self, fullname): """Attempt to fetch source code via pkgutil. In an ideal world, this would be the only required implementation of get_module()."""