File tree Expand file tree Collapse file tree 5 files changed +61
-0
lines changed Expand file tree Collapse file tree 5 files changed +61
-0
lines changed Original file line number Diff line number Diff line change @@ -95,6 +95,8 @@ Unreleased changes template.
9595 and py_library rules
9696 ([ #1647 ] ( https://github.com/bazelbuild/rules_python/issues/1647 ) )
9797* (rules) Added env-var to allow additional interpreter args for stage1 bootstrap
98+ * (rules) Added {obj}` interpreter_args ` attribute to ` py_binary ` and ` py_test ` ,
99+ which allows pass arguments to the interpreter before the regular args.
98100
99101{#v0-0-0-removed}
100102### Removed
Original file line number Diff line number Diff line change @@ -87,6 +87,21 @@ EXECUTABLE_ATTRS = dicts.add(
8787 IMPORTS_ATTRS ,
8888 COVERAGE_ATTRS ,
8989 {
90+ "interpreter_args" : lambda : attrb .StringList (
91+ doc = """
92+ Arguments that are only applicable to the interpreter.
93+
94+ The args an interpreter supports are specific to the interpreter. For
95+ CPython, see https://docs.python.org/3/using/cmdline.html.
96+
97+ :::{note}
98+ Only supported for {obj}`--bootstrap_impl=script`. Ignored otherwise.
99+ :::
100+
101+ :::{versionadded} VERSION_NEXT_FEATURE
102+ :::
103+ """ ,
104+ ),
90105 "legacy_create_init" : lambda : attrb .Int (
91106 default = - 1 ,
92107 values = [- 1 , 0 , 1 ],
@@ -658,6 +673,10 @@ def _create_stage1_bootstrap(
658673 python_binary_actual = venv .interpreter_actual_path if venv else ""
659674
660675 subs = {
676+ "%interpreter_args%" : "\n " .join ([
677+ '"{}"' .format (v )
678+ for v in ctx .attr .interpreter_args
679+ ]),
661680 "%is_zipfile%" : "1" if is_for_zip else "0" ,
662681 "%python_binary%" : python_binary_path ,
663682 "%python_binary_actual%" : python_binary_actual ,
Original file line number Diff line number Diff line change @@ -21,6 +21,11 @@ IS_ZIPFILE="%is_zipfile%"
2121# 0 or 1
2222RECREATE_VENV_AT_RUNTIME=" %recreate_venv_at_runtime%"
2323
24+ # array of strings
25+ declare -a INTERPRETER_ARGS_FROM_TARGET=(
26+ %interpreter_args%
27+ )
28+
2429if [[ " $IS_ZIPFILE " == " 1" ]]; then
2530 # NOTE: Macs have an old version of mktemp, so we must use only the
2631 # minimal functionality of it.
@@ -229,6 +234,7 @@ command=(
229234 " ${interpreter_env[@]} "
230235 " $python_exe "
231236 " ${interpreter_args[@]} "
237+ " ${INTERPRETER_ARGS_FROM_TARGET[@]} "
232238 " $stage2_bootstrap "
233239 " $@ "
234240)
Original file line number Diff line number Diff line change @@ -124,4 +124,13 @@ sh_py_run_test(
124124 target_compatible_with = SUPPORTS_BOOTSTRAP_SCRIPT ,
125125)
126126
127+ py_reconfig_test (
128+ name = "interpreter_args_test" ,
129+ srcs = ["interpreter_args_test.py" ],
130+ bootstrap_impl = "script" ,
131+ interpreter_args = ["-XSPECIAL=1" ],
132+ main = "interpreter_args_test.py" ,
133+ target_compatible_with = SUPPORTS_BOOTSTRAP_SCRIPT ,
134+ )
135+
127136relative_path_test_suite (name = "relative_path_tests" )
Original file line number Diff line number Diff line change 1+ # Copyright 2025 The Bazel Authors. All rights reserved.
2+ #
3+ # Licensed under the Apache License, Version 2.0 (the "License");
4+ # you may not use this file except in compliance with the License.
5+ # You may obtain a copy of the License at
6+ #
7+ # http://www.apache.org/licenses/LICENSE-2.0
8+ #
9+ # Unless required by applicable law or agreed to in writing, software
10+ # distributed under the License is distributed on an "AS IS" BASIS,
11+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ # See the License for the specific language governing permissions and
13+ # limitations under the License.
14+
15+ import sys
16+ import unittest
17+
18+
19+ class InterpreterArgsTest (unittest .TestCase ):
20+ def test_interpreter_args (self ):
21+ self .assertEqual (sys ._xoptions , {"SPECIAL" : "1" })
22+
23+
24+ if __name__ == "__main__" :
25+ unittest .main ()
You can’t perform that action at this time.
0 commit comments