Merge pull request #16 from google/dgreiman/b11
Insert imports near the front of sys.path instead of the end.
diff --git a/runtime/support.py b/runtime/support.py
index 53ad9b6..d9e957a 100644
--- a/runtime/support.py
+++ b/runtime/support.py
@@ -70,7 +70,8 @@
ImportWarning)
return
- for import_root in (import_roots or []):
+ # We try to match to order of Bazel's stub
+ for import_root in reversed(import_roots or []):
new_path = os.path.join(archive_path, import_root)
_log('# adding %s to sys.path' % new_path)
- sys.path.append(new_path)
+ sys.path.insert(1, new_path)
diff --git a/runtime/support_test.py b/runtime/support_test.py
index 9b9d8b0..3469b23 100644
--- a/runtime/support_test.py
+++ b/runtime/support_test.py
@@ -42,9 +42,9 @@
self.assertNotEqual(path, None)
def test_setup(self):
- support.setup(import_roots=['some_root'])
- last_entry = sys.path[-1]
- self.assertTrue(last_entry.endswith('subpar/runtime/some_root'))
+ support.setup(import_roots=['some_root', 'another_root'])
+ self.assertTrue(sys.path[1].endswith('subpar/runtime/some_root'), sys.path)
+ self.assertTrue(sys.path[2].endswith('subpar/runtime/another_root'), sys.path)
if __name__ == '__main__':
diff --git a/tests/BUILD b/tests/BUILD
index bb2d53c..aff188b 100644
--- a/tests/BUILD
+++ b/tests/BUILD
@@ -87,6 +87,22 @@
"PY3",
]]
+[par_binary(
+ name = "package_shadow/main_%s" % version,
+ srcs = [
+ "package_shadow/code/__init__.py",
+ "package_shadow/code/shadowed.py",
+ "package_shadow/main.py",
+ ],
+ default_python_version = version,
+ imports = ["package_shadow"],
+ main = "package_shadow/main.py",
+ srcs_version = "PY2AND3",
+) for version in [
+ "PY2",
+ "PY3",
+]]
+
# Test targets
[(
# Run test without .par file as a control
@@ -124,6 +140,7 @@
("external_workspace", ":package_e/e", "/package_e/e"),
("version", ":package_f/f", "/package_f/f"),
("main_boilerplate", ":package_g/g", "/package_g/g"),
+ ("shadow", ":package_shadow/main", "/package_shadow/main"),
] for version in [
"PY2",
"PY3",
diff --git a/tests/package_shadow/code/__init__.py b/tests/package_shadow/code/__init__.py
new file mode 100644
index 0000000..8eba7db
--- /dev/null
+++ b/tests/package_shadow/code/__init__.py
@@ -0,0 +1 @@
+# This package name shadows the builtin 'code' module
diff --git a/tests/package_shadow/code/shadowed.py b/tests/package_shadow/code/shadowed.py
new file mode 100644
index 0000000..a112f6f
--- /dev/null
+++ b/tests/package_shadow/code/shadowed.py
@@ -0,0 +1,15 @@
+# Copyright 2016 Google Inc. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+print('In code/shadowed.py')
diff --git a/tests/package_shadow/main.py b/tests/package_shadow/main.py
new file mode 100644
index 0000000..380560e
--- /dev/null
+++ b/tests/package_shadow/main.py
@@ -0,0 +1,30 @@
+# Copyright 2016 Google Inc. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Integration test program for Subpar.
+
+Test a user package with the same name as a builtin module
+"""
+
+# This will fail with the following error
+# ImportError: No module named shadowed
+# if we erroneously import the builtin 'code' module
+import code.shadowed
+
+def main():
+ print('In shadow_test main()')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/tests/package_shadow/main_PY2_filelist.txt b/tests/package_shadow/main_PY2_filelist.txt
new file mode 100644
index 0000000..20331f8
--- /dev/null
+++ b/tests/package_shadow/main_PY2_filelist.txt
@@ -0,0 +1,10 @@
+__main__.py
+subpar/__init__.py
+subpar/runtime/__init__.py
+subpar/runtime/support.py
+subpar/tests/__init__.py
+subpar/tests/package_shadow/__init__.py
+subpar/tests/package_shadow/code/__init__.py
+subpar/tests/package_shadow/code/shadowed.py
+subpar/tests/package_shadow/main.py
+subpar/tests/package_shadow/main_PY2
diff --git a/tests/package_shadow/main_PY3_filelist.txt b/tests/package_shadow/main_PY3_filelist.txt
new file mode 100644
index 0000000..f1c6b97
--- /dev/null
+++ b/tests/package_shadow/main_PY3_filelist.txt
@@ -0,0 +1,10 @@
+__main__.py
+subpar/__init__.py
+subpar/runtime/__init__.py
+subpar/runtime/support.py
+subpar/tests/__init__.py
+subpar/tests/package_shadow/__init__.py
+subpar/tests/package_shadow/code/__init__.py
+subpar/tests/package_shadow/code/shadowed.py
+subpar/tests/package_shadow/main.py
+subpar/tests/package_shadow/main_PY3