diff options
author | Adam Gross <grossag@vmware.com> | 2020-11-16 21:59:20 (GMT) |
---|---|---|
committer | Adam Gross <grossag@vmware.com> | 2020-11-17 00:25:33 (GMT) |
commit | 55b41b5841aaf1f86d8a8380e0f8603b45bcc49f (patch) | |
tree | e011288ff78c3e64ed62e9be88be6dc7c316f75a | |
parent | 1037bb6069612408183e953081c72dac9814f370 (diff) | |
download | SCons-55b41b5841aaf1f86d8a8380e0f8603b45bcc49f.zip SCons-55b41b5841aaf1f86d8a8380e0f8603b45bcc49f.tar.gz SCons-55b41b5841aaf1f86d8a8380e0f8603b45bcc49f.tar.bz2 |
[ci skip] Some fixes to the Python scanner and test
-rw-r--r-- | SCons/Scanner/Python.py | 76 | ||||
-rw-r--r-- | test/Scanner/Python.py | 2 |
2 files changed, 43 insertions, 35 deletions
diff --git a/SCons/Scanner/Python.py b/SCons/Scanner/Python.py index 5f51a7c..ada575e 100644 --- a/SCons/Scanner/Python.py +++ b/SCons/Scanner/Python.py @@ -118,43 +118,51 @@ def scan(node, env, path=()): search_paths = [env.Dir(p) for p in path] search_string = module - module_components = search_string.split('.') - module_joined = '/'.join(module_components) - - # For an import of "p", it could either result in a directory named - # p or a file named p.py. We can't do two consecutive searches for p - # then p.py because the first search could return a result that is - # lower in the search_paths precedence order. As a result, it is safest - # to iterate over search_paths and check whether p or p.py exists in - # each path. This allows us to cleanly respect the precedence order. - for search_path in search_paths: - paths = [search_path] - node = SCons.Node.FS.find_file(module_joined, paths, verbose=True) - if node: - # The fact that we were able to find the node without appending .py - # means that this is a directory import. - nodes.append(env.Dir(node).File('__init__.py')) - - # Take a dependency on all __init__.py files from all imported - # packages unless it's a relative import. If it's a relative - # import, we don't need to take the dependency because Python - # requires that all referenced packages have already been imported, - # which means that the dependency has already been established. - if not is_relative and len(module_components) > 1: - import_dirs = module_components - for i in range(len(import_dirs)): - init_path = '/'.join(module_components[:i+1] + ['__init__.py']) - # TODO: Passing search_paths is not correct. - init_node = SCons.Node.FS.find_file(init_path, paths, verbose=True) - if init_node: - nodes.append(init_node) - else: - node = SCons.Node.FS.find_file(module_joined + '.py', paths, verbose=True) + if not imports: + imports = [None] + + for i in imports: + module_components = search_string.split('.') + import_components = [i] if i is not None else [] + components = [x for x in module_components + import_components if x] + module_path = '/'.join(components) + '.py' + package_path = '/'.join(components + ['__init__.py']) + + # For an import of "p", it could either result in a file named p.py or + # p/__init__.py. We can't do two consecutive searches for p then p.py + # because the first search could return a result that is lower in the + # search_paths precedence order. As a result, it is safest to iterate + # over search_paths and check whether p or p.py exists in each path. + # This allows us to cleanly respect the precedence order. + for search_path in search_paths: + paths = [search_path] + # See if p/__init__.py exists. + node = SCons.Node.FS.find_file(package_path, paths, verbose=True) if node: nodes.append(node) + else: + node = SCons.Node.FS.find_file(module_path, paths, verbose=True) + if node: + nodes.append(node) - print('returning nodes %s' % ([str(n) for n in nodes])) - return nodes + if node: + # Take a dependency on all __init__.py files from all imported + # packages unless it's a relative import. If it's a relative + # import, we don't need to take the dependency because Python + # requires that all referenced packages have already been imported, + # which means that the dependency has already been established. + if not is_relative and len(module_components) > 1: + for i in range(len(module_components[:-1])): + init_path = '/'.join(module_components[:i+1] + ['__init__.py']) + init_node = SCons.Node.FS.find_file(init_path, paths, verbose=True) + if init_node: + nodes.append(init_node) + + # The import was found, so no need to keep iterating through + # search_paths. + break + + return sorted(nodes) PythonSuffixes = ['.py'] diff --git a/test/Scanner/Python.py b/test/Scanner/Python.py index 404967c..bb6e2c7 100644 --- a/test/Scanner/Python.py +++ b/test/Scanner/Python.py @@ -32,7 +32,7 @@ import TestSCons test = TestSCons.TestSCons() test.dir_fixture('Python') -test.run(arguments = '-n --tree=prune --debug=stacktrace .', stdout='') +test.run(arguments = '--debug=stacktrace .', stdout='') test.pass_test() # Local Variables: |