Skip to content
11 changes: 10 additions & 1 deletion Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,14 @@ def is_python(self, path):
head, tail = os.path.splitext(path)
return tail.lower() in (".py", ".pyw")

def make_cmdline( self, scriptfile, query ):
cmdline = [scriptfile]
if self.is_python(scriptfile):
interp = sys.executable
cmdline = [interp, '-u'] + cmdline
if '=' not in query:
cmdline.append(query)

def run_cgi(self):
"""Execute a CGI script."""
dir, rest = self.cgi_info
Expand Down Expand Up @@ -1179,7 +1187,8 @@ def run_cgi(self):
else:
# Non-Unix -- use subprocess
import subprocess
cmdline = [scriptfile]
cmdline = self.make_cmdline( scriptfile, query )
self.log_message("command: %s", subprocess.list2cmdline(cmdline))
if self.is_python(scriptfile):
interp = sys.executable
if interp.lower().endswith("w.exe"):
Expand Down
15 changes: 9 additions & 6 deletions Lib/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def _find_module(name, path=None):

class Module:

def __init__(self, name, file=None, path=None):
def __init__(self, name, file=None, path=None, recurse=True):
self.__name__ = name
self.__file__ = file
self.__path__ = path
Expand All @@ -113,17 +113,19 @@ def __repr__(self):

class ModuleFinder:

def __init__(self, path=None, debug=0, excludes=None, replace_paths=None):
def __init__(self, path=None, debug=0, excludes=None, replace_paths=None, recurse=1):
if path is None:
path = sys.path
self.path = path
self.modules = {}
self.badmodules = {}
self.debug = debug
self.recurse = 0
self.indent = 0
self.excludes = excludes if excludes is not None else []
self.replace_paths = replace_paths if replace_paths is not None else []
self.processed_paths = [] # Used in debugging only
self.recurse = recurse

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to set self.recurse twice

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made the change in local repro.

def msg(self, level, str, *args):
if level <= self.debug:
Expand Down Expand Up @@ -439,10 +441,11 @@ def scan_code(self, co, m):
# We don't expect anything else from the generator.
raise RuntimeError(what)

for c in co.co_consts:
if isinstance(c, type(co)):
self.scan_code(c, m)

if self.recurse:
for c in co.co_consts:
if isinstance(c, type(co)):
self.scan_code(c, m)

def load_package(self, fqname, pathname):
self.msgin(2, "load_package", fqname, pathname)
newname = replacePackageMap.get(fqname)
Expand Down
23 changes: 21 additions & 2 deletions Lib/test/test_modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,21 @@ def foo(): pass
from . import bar
"""]

non_recursive_import_test_1 = [
"a",
["a", "b", "c", "sys"],
[],
[],
"""\
a.py
import b
b.py
import c
c.py
import sys
"""]


relative_import_test_4 = [
"a.module",
["a", "a.module"],
Expand Down Expand Up @@ -319,12 +334,13 @@ def create_package(source):
ofi.close()

class ModuleFinderTest(unittest.TestCase):
def _do_test(self, info, report=False, debug=0, replace_paths=[], modulefinder_class=modulefinder.ModuleFinder):
def _do_test(self, info, report=False, debug=0, replace_paths=[], modulefinder_class=modulefinder.ModuleFinder,
**kwargs):
import_this, modules, missing, maybe_missing, source = info
create_package(source)
try:
mf = modulefinder_class(path=TEST_PATH, debug=debug,
replace_paths=replace_paths)
replace_paths=replace_paths, **kwargs)
mf.import_hook(import_this)
if report:
mf.report()
Expand Down Expand Up @@ -433,5 +449,8 @@ def load_module(self, fqname, fp, pathname, file_info):

self._do_test(absolute_import_test, modulefinder_class=CheckLoadModuleApi)

def test_non_recursive_1(self):
self._do_test(non_recursive_import_test_1, recurse=False, debug=0)

if __name__ == "__main__":
unittest.main()
Binary file added _bootstrap_python
Binary file not shown.