Skip to content

Commit 8e935f4

Browse files
committed
Fix Windows hopefully
1 parent 94d5ee3 commit 8e935f4

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

docs/toolchains.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ To run the interpreter that Bazel will use, you can use the
516516
`@rules_python//python/bin:python` target. This is a binary target with
517517
the executable pointing at the `python3` binary plus the relevent runfiles.
518518

519-
```
519+
```console
520520
$ bazel run @rules_python//python/bin:python
521521
Python 3.11.1 (main, Jan 16 2023, 22:41:20) [Clang 15.0.7 ] on linux
522522
Type "help", "copyright", "credits" or "license" for more information.
@@ -528,14 +528,23 @@ Type "help", "copyright", "credits" or "license" for more information.
528528
```
529529

530530
You can also access a specific binary's interpreter this way by using the
531-
`@rules_python//python/bin:python_src` target.
531+
`@rules_python//python/bin:python_src` target. In the example below, it is
532+
assumed that the `@rules_python//tools/publish:twine` binary is fixed at Python
533+
3.11.
532534

533-
```
534-
$ bazel run @rules_python//python/bin:python --@rules_python//python/bin:interpreter_src=//path/to:bin
535+
```console
536+
$ bazel run @rules_python//python/bin:python --@rules_python//python/bin:interpreter_src=@rules_python//tools/publish:twine
537+
Python 3.11.1 (main, Jan 16 2023, 22:41:20) [Clang 15.0.7 ] on linux
538+
Type "help", "copyright", "credits" or "license" for more information.
539+
>>>
540+
$ bazel run @rules_python//python/bin:python --@rules_python//python/bin:interpreter_src=@rules_python//tools/publish:twine --@rules_python//python/config_settings:python_version=3.12
535541
Python 3.11.1 (main, Jan 16 2023, 22:41:20) [Clang 15.0.7 ] on linux
536542
Type "help", "copyright", "credits" or "license" for more information.
537543
>>>
538544
```
545+
Despite setting the Python version explicitly to 3.12 in the example above, the
546+
interpreter comes from the `@rules_python//tools/publish:twine` binary. That is
547+
a fixed version.
539548

540549
:::{note}
541550
The `python` target does not provide access to any modules from `py_*`

tests/interpreter/BUILD.bazel

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,31 @@
1414

1515
load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test")
1616

17+
PYTHON_VERSIONS_TO_TEST = (
18+
"3.10",
19+
"3.11",
20+
"3.12",
21+
)
22+
1723
[py_reconfig_test(
1824
name = "python_version_%s_test" % python_version,
1925
srcs = ["interpreter_test.py"],
2026
data = [
2127
"//python/bin:python",
2228
],
2329
env = {
24-
"EXPECTED_PYTHON_VERSION": python_version,
30+
# Both the interpreter and the test itself are expected to run under
31+
# the same version.
32+
"EXPECTED_INTERPRETER_VERSION": python_version,
33+
"EXPECTED_SELF_VERSION": python_version,
34+
"PYTHON_BIN": "$(location //python/bin:python)",
2535
},
2636
main = "interpreter_test.py",
2737
python_version = python_version,
2838
deps = [
2939
"//python/runfiles",
3040
],
31-
) for python_version in (
32-
"3.10",
33-
"3.11",
34-
"3.12",
35-
)]
41+
) for python_version in PYTHON_VERSIONS_TO_TEST]
3642

3743
[py_reconfig_test(
3844
name = "python_src_%s_test" % python_version,
@@ -41,16 +47,18 @@ load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test")
4147
"//python/bin:python",
4248
],
4349
env = {
44-
"EXPECTED_PYTHON_VERSION": "3.11",
50+
# Since we're grabbing the interpreter from a binary with a fixed
51+
# version, we expect to always see that version. It doesn't matter what
52+
# Python version the test itself is running with.
53+
"EXPECTED_INTERPRETER_VERSION": "3.11",
54+
# The test itself is still running under different Python versions.
55+
"EXPECTED_SELF_VERSION": python_version,
56+
"PYTHON_BIN": "$(rootpath //python/bin:python)",
4557
},
4658
main = "interpreter_test.py",
4759
python_src = "//tools/publish:twine",
4860
python_version = python_version,
4961
deps = [
5062
"//python/runfiles",
5163
],
52-
) for python_version in (
53-
"3.10",
54-
"3.11",
55-
"3.12",
56-
)]
64+
) for python_version in PYTHON_VERSIONS_TO_TEST]

tests/interpreter/interpreter_test.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import os
1616
import subprocess
17+
import sys
1718
import unittest
1819

1920
from python.runfiles import runfiles
@@ -22,11 +23,17 @@
2223
class InterpreterTest(unittest.TestCase):
2324
def setUp(self):
2425
r = runfiles.Create()
25-
self.interpreter = r.Rlocation("rules_python/python/bin/python3")
26+
self.interpreter = os.environ["PYTHON_BIN"]
2627

27-
def test_version(self):
28+
def test_self_version(self):
29+
"""Performs a sanity check on the Python version used for this test."""
30+
expected_version = os.environ["EXPECTED_SELF_VERSION"]
31+
v = sys.version_info
32+
self.assertEqual(expected_version, f"{v.major}.{v.minor}")
33+
34+
def test_interpreter_version(self):
2835
"""Validates that we can successfully execute arbitrary code from the CLI."""
29-
expected_version = os.environ["EXPECTED_PYTHON_VERSION"]
36+
expected_version = os.environ["EXPECTED_INTERPRETER_VERSION"]
3037

3138
result = subprocess.check_output(
3239
[self.interpreter],

0 commit comments

Comments
 (0)