Fix get_module_via_sys_modules when running under unit2.

This commit is contained in:
David Wilson 2018-04-16 15:28:26 +01:00
parent 7a078a458c
commit 34a1e3337f
1 changed files with 19 additions and 1 deletions

View File

@ -34,6 +34,7 @@ import logging
import os import os
import pkgutil import pkgutil
import re import re
import string
import sys import sys
import threading import threading
import types import types
@ -349,11 +350,28 @@ class ModuleFinder(object):
return False 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): def _py_filename(self, path):
path = path.rstrip('co') if path[-4:] in ('.pyc', '.pyo'):
path = path.rstrip('co')
if path.endswith('.py'): if path.endswith('.py'):
return path return path
if os.path.exists(path) and self._looks_like_script(path):
return path
def _get_module_via_pkgutil(self, fullname): def _get_module_via_pkgutil(self, fullname):
"""Attempt to fetch source code via pkgutil. In an ideal world, this """Attempt to fetch source code via pkgutil. In an ideal world, this
would be the only required implementation of get_module().""" would be the only required implementation of get_module()."""