From 34a1e3337f8ec2be3113859ea73428698ced49a6 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 16 Apr 2018 15:28:26 +0100 Subject: [PATCH] Fix get_module_via_sys_modules when running under unit2. --- mitogen/master.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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()."""