From bae2169c037bfcfea6ef566e44eabbdcb396d5a2 Mon Sep 17 00:00:00 2001 From: Greg Estren Date: Fri, 19 Sep 2025 21:54:38 +0000 Subject: [PATCH 1/4] Remote outdated nn-toolchain resolution logic. - "If toolchains disabled" logic - `configuration_field` on `python_top` - `_py_interpreter` attribute For https://github.com/bazel-contrib/rules_python/issues/3252. --- python/private/py_executable.bzl | 65 +++++++++++--------------------- 1 file changed, 23 insertions(+), 42 deletions(-) diff --git a/python/private/py_executable.bzl b/python/private/py_executable.bzl index bef5934729..e2d5f47fd6 100644 --- a/python/private/py_executable.bzl +++ b/python/private/py_executable.bzl @@ -203,15 +203,6 @@ accepting arbitrary Python versions. # empty target for other platforms. default = "//tools/launcher:launcher", ), - "_py_interpreter": lambda: attrb.Label( - # The configuration_field args are validated when called; - # we use the precense of py_internal to indicate this Bazel - # build has that fragment and name. - default = configuration_field( - fragment = "bazel_py", - name = "python_top", - ) if py_internal else None, - ), # TODO: This appears to be vestigial. It's only added because # GraphlessQueryTest.testLabelsOperator relies on it to test for # query behavior of implicit dependencies. @@ -1202,39 +1193,29 @@ def _maybe_get_runtime_from_ctx(ctx): Returns: 2-tuple of toolchain_runtime, effective_runtime """ - if ctx.fragments.py.use_toolchains: - toolchain = ctx.toolchains[TOOLCHAIN_TYPE] - - if not hasattr(toolchain, "py3_runtime"): - fail("Python toolchain field 'py3_runtime' is missing") - if not toolchain.py3_runtime: - fail("Python toolchain missing py3_runtime") - py3_runtime = toolchain.py3_runtime - - # Hack around the fact that the autodetecting Python toolchain, which is - # automatically registered, does not yet support Windows. In this case, - # we want to return null so that _get_interpreter_path falls back on - # --python_path. See tools/python/toolchain.bzl. - # TODO(#7844): Remove this hack when the autodetecting toolchain has a - # Windows implementation. - if py3_runtime.interpreter_path == "/_magic_pyruntime_sentinel_do_not_use": - return None, None - - if py3_runtime.python_version != "PY3": - fail("Python toolchain py3_runtime must be python_version=PY3, got {}".format( - py3_runtime.python_version, - )) - toolchain_runtime = toolchain.py3_runtime - effective_runtime = toolchain_runtime - else: - toolchain_runtime = None - attr_target = ctx.attr._py_interpreter - - # In Bazel, --python_top is null by default. - if attr_target and PyRuntimeInfo in attr_target: - effective_runtime = attr_target[PyRuntimeInfo] - else: - return None, None + toolchain = ctx.toolchains[TOOLCHAIN_TYPE] + + if not hasattr(toolchain, "py3_runtime"): + fail("Python toolchain field 'py3_runtime' is missing") + if not toolchain.py3_runtime: + fail("Python toolchain missing py3_runtime") + py3_runtime = toolchain.py3_runtime + + # Hack around the fact that the autodetecting Python toolchain, which is + # automatically registered, does not yet support Windows. In this case, + # we want to return null so that _get_interpreter_path falls back on + # --python_path. See tools/python/toolchain.bzl. + # TODO(#7844): Remove this hack when the autodetecting toolchain has a + # Windows implementation. + if py3_runtime.interpreter_path == "/_magic_pyruntime_sentinel_do_not_use": + return None, None + + if py3_runtime.python_version != "PY3": + fail("Python toolchain py3_runtime must be python_version=PY3, got {}".format( + py3_runtime.python_version, + )) + toolchain_runtime = toolchain.py3_runtime + effective_runtime = toolchain_runtime return toolchain_runtime, effective_runtime From 4ede08e434abe737420b199d542d990cd7c8f2ac Mon Sep 17 00:00:00 2001 From: Greg Estren Date: Fri, 19 Sep 2025 22:07:39 +0000 Subject: [PATCH 2/4] Remove now-unreferenced PyRuntimeInfo. --- python/private/py_executable.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/private/py_executable.bzl b/python/private/py_executable.bzl index e2d5f47fd6..1ab49615a8 100644 --- a/python/private/py_executable.bzl +++ b/python/private/py_executable.bzl @@ -59,7 +59,7 @@ load(":py_cc_link_params_info.bzl", "PyCcLinkParamsInfo") load(":py_executable_info.bzl", "PyExecutableInfo") load(":py_info.bzl", "PyInfo", "VenvSymlinkKind") load(":py_internal.bzl", "py_internal") -load(":py_runtime_info.bzl", "DEFAULT_STUB_SHEBANG", "PyRuntimeInfo") +load(":py_runtime_info.bzl", "DEFAULT_STUB_SHEBANG") load(":reexports.bzl", "BuiltinPyInfo", "BuiltinPyRuntimeInfo") load(":rule_builders.bzl", "ruleb") load(":toolchain_types.bzl", "EXEC_TOOLS_TOOLCHAIN_TYPE", "TARGET_TOOLCHAIN_TYPE", TOOLCHAIN_TYPE = "TARGET_TOOLCHAIN_TYPE") From 158a592cda66bb5e87d05ad94a92ec50dfa57b49 Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Sat, 20 Sep 2025 11:49:46 -0700 Subject: [PATCH 3/4] optimize assignments Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- python/private/py_executable.bzl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/python/private/py_executable.bzl b/python/private/py_executable.bzl index 1ab49615a8..359bd78c4f 100644 --- a/python/private/py_executable.bzl +++ b/python/private/py_executable.bzl @@ -1199,7 +1199,7 @@ def _maybe_get_runtime_from_ctx(ctx): fail("Python toolchain field 'py3_runtime' is missing") if not toolchain.py3_runtime: fail("Python toolchain missing py3_runtime") - py3_runtime = toolchain.py3_runtime + toolchain_runtime = toolchain.py3_runtime # Hack around the fact that the autodetecting Python toolchain, which is # automatically registered, does not yet support Windows. In this case, @@ -1207,14 +1207,13 @@ def _maybe_get_runtime_from_ctx(ctx): # --python_path. See tools/python/toolchain.bzl. # TODO(#7844): Remove this hack when the autodetecting toolchain has a # Windows implementation. - if py3_runtime.interpreter_path == "/_magic_pyruntime_sentinel_do_not_use": + if toolchain_runtime.interpreter_path == "/_magic_pyruntime_sentinel_do_not_use": return None, None - if py3_runtime.python_version != "PY3": + if toolchain_runtime.python_version != "PY3": fail("Python toolchain py3_runtime must be python_version=PY3, got {}".format( - py3_runtime.python_version, + toolchain_runtime.python_version, )) - toolchain_runtime = toolchain.py3_runtime effective_runtime = toolchain_runtime return toolchain_runtime, effective_runtime From e5faa24da03bcc4d4a7619642f580d397bd4360e Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Sat, 20 Sep 2025 11:51:06 -0700 Subject: [PATCH 4/4] rename py3_runtime variable --- python/private/py_executable.bzl | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/python/private/py_executable.bzl b/python/private/py_executable.bzl index 359bd78c4f..5993a4f003 100644 --- a/python/private/py_executable.bzl +++ b/python/private/py_executable.bzl @@ -1199,7 +1199,7 @@ def _maybe_get_runtime_from_ctx(ctx): fail("Python toolchain field 'py3_runtime' is missing") if not toolchain.py3_runtime: fail("Python toolchain missing py3_runtime") - toolchain_runtime = toolchain.py3_runtime + py3_runtime = toolchain.py3_runtime # Hack around the fact that the autodetecting Python toolchain, which is # automatically registered, does not yet support Windows. In this case, @@ -1207,16 +1207,14 @@ def _maybe_get_runtime_from_ctx(ctx): # --python_path. See tools/python/toolchain.bzl. # TODO(#7844): Remove this hack when the autodetecting toolchain has a # Windows implementation. - if toolchain_runtime.interpreter_path == "/_magic_pyruntime_sentinel_do_not_use": + if py3_runtime.interpreter_path == "/_magic_pyruntime_sentinel_do_not_use": return None, None - if toolchain_runtime.python_version != "PY3": + if py3_runtime.python_version != "PY3": fail("Python toolchain py3_runtime must be python_version=PY3, got {}".format( - toolchain_runtime.python_version, + py3_runtime.python_version, )) - effective_runtime = toolchain_runtime - - return toolchain_runtime, effective_runtime + return py3_runtime, py3_runtime def _get_base_runfiles_for_binary( ctx,