Skip to content

Commit

Permalink
feat: add interpreter_version_info to py_runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
mattem committed Jan 7, 2024
1 parent ebd779e commit 27efeb5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
9 changes: 8 additions & 1 deletion python/private/common/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def _PyRuntimeInfo_init(
coverage_files = None,
python_version,
stub_shebang = None,
bootstrap_template = None):
bootstrap_template = None,
interpreter_version_info = None):
if (interpreter_path and interpreter) or (not interpreter_path and not interpreter):
fail("exactly one of interpreter or interpreter_path must be specified")

Expand Down Expand Up @@ -88,6 +89,7 @@ def _PyRuntimeInfo_init(
"coverage_tool": coverage_tool,
"files": files,
"interpreter": interpreter,
"interpreter_version_info": interpreter_version_info,
"interpreter_path": interpreter_path,
"python_version": python_version,
"stub_shebang": stub_shebang,
Expand Down Expand Up @@ -136,6 +138,11 @@ the same conventions as the standard CPython interpreter.
"filesystem path to the interpreter on the target platform. " +
"Otherwise, this is `None`."
),
"interpreter_version_info": (
"This is the version of the interpreter that this runtime provides. " +
"The list can contain the five components of the version number: major, minor, micro, releaselevel, and serial. " +
"This should match the format given by `sys.version_info`, however for simplicity the micro, releaselevel and serial values may be omitted."
),
"python_version": (
"Indicates whether this runtime uses Python major version 2 or 3. " +
"Valid values are (only) `\"PY2\"` and " +
Expand Down
18 changes: 18 additions & 0 deletions python/private/common/py_runtime_rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def _py_runtime_impl(ctx):

python_version = ctx.attr.python_version

interpreter_version_info = ctx.attr.interpreter_version_info
if interpreter_version_info and len(interpreter_version_info) < 2:
fail("interpreter_version_info must have at least two values [major, minor]")

# TODO: Uncomment this after --incompatible_python_disable_py2 defaults to true
# if ctx.fragments.py.disable_py2 and python_version == "PY2":
# fail("Using Python 2 is not supported and disabled; see " +
Expand All @@ -75,10 +79,16 @@ def _py_runtime_impl(ctx):
python_version = python_version,
stub_shebang = ctx.attr.stub_shebang,
bootstrap_template = ctx.file.bootstrap_template,
interpreter_version_info = interpreter_version_info,
)
builtin_py_runtime_info_kwargs = dict(py_runtime_info_kwargs)

# Pop this property as it does not exist on BuiltinPyRuntimeInfo
builtin_py_runtime_info_kwargs.pop("interpreter_version_info")

if not IS_BAZEL_7_OR_HIGHER:
builtin_py_runtime_info_kwargs.pop("bootstrap_template")

return [
PyRuntimeInfo(**py_runtime_info_kwargs),
# Return the builtin provider for better compatibility.
Expand Down Expand Up @@ -196,6 +206,14 @@ platform runtime this attribute must not be set.
For a platform runtime, this is the absolute path of a Python interpreter on
the target platform. For an in-build runtime this attribute must not be set.
"""),
"interpreter_version_info": attr.string_list(
doc = """
This is the version of the interpreter that this runtime provides.
The list can contain the five components of the version number: major, minor, micro, releaselevel, and serial.
This should match the format given by `sys.version_info`, however for simplicity the micro, releaselevel and serial values may be omitted.
""",
mandatory = False,
),
"python_version": attr.string(
default = "PY3",
values = ["PY2", "PY3"],
Expand Down
5 changes: 4 additions & 1 deletion python/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ def _python_repository_impl(rctx):

platform = rctx.attr.platform
python_version = rctx.attr.python_version
python_short_version = python_version.rpartition(".")[0]
python_version_info = python_version.split(".")
python_short_version = "{0}.{1}".format(*python_version_info)
release_filename = rctx.attr.release_filename
urls = rctx.attr.urls or [rctx.attr.url]
auth = get_auth(rctx, urls)
Expand Down Expand Up @@ -334,6 +335,7 @@ py_runtime(
files = [":files"],
{coverage_attr}
interpreter = "{python_path}",
interpreter_version_info = {interpreter_version_info},
python_version = "PY3",
)
Expand All @@ -355,6 +357,7 @@ py_cc_toolchain(
python_version = python_short_version,
python_version_nodot = python_short_version.replace(".", ""),
coverage_attr = coverage_attr_text,
interpreter_version_info = str(python_version_info),
)
rctx.delete("python")
rctx.symlink(python_bin, "python")
Expand Down

0 comments on commit 27efeb5

Please sign in to comment.