Skip to content

Commit

Permalink
python: fix bazel py_test testSmoke for Python 3.11+
Browse files Browse the repository at this point in the history
The test was failing because the `b.py` file couldn't be imported because, starting with Python 3.11, the `PYTHONSAFEPATH` environment variable (set by the bootstrap startup) is respected. This setting inhibits the default Python behavior of adding the main script's directory to sys.path.

To fix, use `py_library.imports` to explicitly add the necessary directory to `sys.path`.

Fixes #20660

Closes #20738.

PiperOrigin-RevId: 595498775
Change-Id: I0c9521b210fe9e2c40692727fc87d41995e0968a
  • Loading branch information
rickeylev authored and copybara-github committed Jan 3, 2024
1 parent ef9d146 commit 9b027c8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/test/py/bazel/py_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ def createSimpleFiles(self):
'a/BUILD',
[
'py_binary(name="a", srcs=["a.py"], deps=[":b"])',
'py_library(name="b", srcs=["b.py"])',
])
'py_library(name="b", srcs=["b.py"], imports=["."])',
],
)

self.ScratchFile(
'a/a.py',
Expand Down
2 changes: 1 addition & 1 deletion src/test/py/bazel/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ def RunProgram(
]

if not allow_failure:
self.AssertExitCode(exit_code, 0, stderr_lines)
self.AssertExitCode(exit_code, 0, stderr_lines, stdout_lines)

return exit_code, stdout_lines, stderr_lines

Expand Down

0 comments on commit 9b027c8

Please sign in to comment.