Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give imported packages lower precedence than standard library. #6532

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import sys
import subprocess
import zipfile

# Stupid little utility to dedup entries in a list, while preserving order.
def DedupList(lst):
seen = []
return [x for x in lst if not (x in seen or seen.append(x))]

# Return True if running on Windows
def IsWindows():
return os.name == 'nt'
Expand Down Expand Up @@ -147,6 +152,17 @@ def RunfilesEnvvar(module_space):

return (None, None)

def BuildUserCustomizeModule(import_paths):
return """\
# AUTOGENERATED FILE PRODUCED BY PYTHON LAUNCHER STUB SCRIPT. DO NOT EDIT.
import sys

import_paths = [
%s
]
sys.path.extend(import_paths)
""" % "\n".join(' "%s",' % path for path in import_paths)

katzdm marked this conversation as resolved.
Show resolved Hide resolved
def Main():
args = sys.argv[1:]

Expand All @@ -162,11 +178,19 @@ def Main():
python_path_entries += GetRepositoriesImports(module_space, %import_all%)

python_path_entries = [GetWindowsPathWithUNCPrefix(d) for d in python_path_entries]
python_path_entries = DedupList(python_path_entries)

# Write `usercustomize.py` file. This will be loaded during startup, and will append package
# directories to `sys.path`.
usercustomize_module = open("%s/usercustomize.py" % module_space, "w")
usercustomize_module.write(BuildUserCustomizeModule(python_path_entries))
usercustomize_module.close()

old_python_path = os.environ.get('PYTHONPATH')
python_path = os.pathsep.join(python_path_entries)
if old_python_path:
python_path += os.pathsep + old_python_path
python_path = old_python_path + os.pathsep + module_space
else:
python_path = module_space

if IsWindows():
python_path = python_path.replace("/", os.sep)
Expand Down
8 changes: 4 additions & 4 deletions src/test/shell/bazel/python_version_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,8 @@ EOF
# a separate suite.

# Tests that a non-standard library module on the PYTHONPATH added by Bazel
# can override the standard library. This behavior is not necessarily ideal, but
# it is the current semantics; see #6532 about changing that.
# cannot override the standard library. (See #6532 for discussion on this
# behavior.)
function test_source_file_does_not_override_standard_library() {
mkdir -p test

Expand Down Expand Up @@ -506,8 +506,8 @@ EOF

bazel run //test:main \
&> $TEST_log || fail "bazel run failed"
# Indicates that the local module overrode the system one.
expect_log "I am lib!"
# Presence would indicate that the local module overrode the system one.
expect_not_log "I am lib!"
}

run_suite "Tests for how the Python rules handle Python 2 vs Python 3"