Skip to content

Commit e8bccc1

Browse files
committed
unify setting defaults
1 parent 4e2a97e commit e8bccc1

File tree

3 files changed

+47
-39
lines changed

3 files changed

+47
-39
lines changed

python/private/pypi/env_marker_setting.bzl

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
44
load("//python/private:toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE")
55
load(":env_marker_info.bzl", "EnvMarkerInfo")
6-
load(":pep508_env.bzl", "env_aliases")
6+
load(":pep508_env.bzl", "create_env", "set_missing_env_defaults")
77
load(":pep508_evaluate.bzl", "evaluate")
88

99
# Use capitals to hint its not an actual boolean type.
@@ -37,7 +37,7 @@ def env_marker_setting(*, name, expression, **kwargs):
3737
)
3838

3939
def _env_marker_setting_impl(ctx):
40-
env = {}
40+
env = create_env()
4141
env.update(
4242
ctx.attr._env_marker_config_flag[EnvMarkerInfo].env,
4343
)
@@ -60,28 +60,10 @@ def _env_marker_setting_impl(ctx):
6060
env["python_full_version"] = full_version
6161
env["implementation_version"] = full_version
6262

63-
if "implementation_name" not in env:
64-
# We assume cpython if the toolchain doesn't specify because it's most
65-
# likely to be true.
66-
env["implementation_name"] = runtime.implementation_name or "cpython"
67-
68-
if "platform_python_implementation" not in env:
69-
# The `platform_python_implementation` marker value is supposed to come
70-
# from `platform.python_implementation()`, however, PEP 421 introduced
71-
# `sys.implementation.name` and the `implementation_name` env marker to
72-
# replace it. Per the platform.python_implementation docs, there's now
73-
# essentially just two possible "registered" values: CPython or PyPy.
74-
# Rather than add a field to the toolchain, we just special case the value
75-
# from `sys.implementation.name` to handle the two documented values.
76-
platform_python_impl = runtime.implementation_name
77-
if platform_python_impl == "cpython":
78-
platform_python_impl = "CPython"
79-
elif platform_python_impl == "pypy":
80-
platform_python_impl = "PyPy"
81-
env["platform_python_implementation"] = platform_python_impl
82-
83-
env.update(env_aliases())
63+
if "implementation_name" not in env and runtime.implementation_name:
64+
env["implementation_name"] = runtime.implementation_name
8465

66+
set_missing_env_defaults(env)
8567
if evaluate(ctx.attr.expression, env = env):
8668
value = _ENV_MARKER_TRUE
8769
else:

python/private/pypi/flags.bzl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ load("//python/private:enum.bzl", "enum")
2323
load(":env_marker_info.bzl", "EnvMarkerInfo")
2424
load(
2525
":pep508_env.bzl",
26+
"create_env",
2627
"os_name_select_map",
2728
"platform_machine_select_map",
2829
"platform_system_select_map",
@@ -124,7 +125,7 @@ def _default_env_marker_config(**kwargs):
124125
)
125126

126127
def _env_marker_config_impl(ctx):
127-
env = {}
128+
env = create_env()
128129
env["os_name"] = ctx.attr.os_name
129130
env["sys_platform"] = ctx.attr.sys_platform
130131
env["platform_machine"] = ctx.attr.platform_machine
@@ -139,8 +140,8 @@ def _env_marker_config_impl(ctx):
139140
env["platform_release"] = platform_release
140141
env["platform_system"] = ctx.attr.platform_system
141142

142-
# For lack of a better option, just use an empty string for now.
143-
env["platform_version"] = ""
143+
# NOTE: We intentionally do not call set_missing_env_defaults() here because
144+
# `env_marker_setting()` computes missing values using the toolchain.
144145
return [EnvMarkerInfo(env = env)]
145146

146147
_env_marker_config = rule(

python/private/pypi/pep508_env.bzl

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ os_name_select_map = {
158158
def env(target_platform, *, extra = None):
159159
"""Return an env target platform
160160
161+
NOTE: This is for use during the loading phase. For the analysis phase,
162+
`env_marker_setting()` constructs the env dict.
163+
161164
Args:
162165
target_platform: {type}`str` the target platform identifier, e.g.
163166
`cp33_linux_aarch64`
@@ -166,16 +169,9 @@ def env(target_platform, *, extra = None):
166169
Returns:
167170
A dict that can be used as `env` in the marker evaluation.
168171
"""
169-
170-
# TODO @aignas 2025-02-13: consider moving this into config settings.
171-
172-
env = {"extra": extra} if extra != None else {}
173-
env = env | {
174-
"implementation_name": "cpython",
175-
"platform_python_implementation": "CPython",
176-
"platform_release": "",
177-
"platform_version": "",
178-
}
172+
env = create_env()
173+
if extra != None:
174+
env["extra"] = extra
179175

180176
if type(target_platform) == type(""):
181177
target_platform = platform_from_str(target_platform, python_version = "")
@@ -196,13 +192,42 @@ def env(target_platform, *, extra = None):
196192
"platform_system": _platform_system_values.get(os, ""),
197193
"sys_platform": _sys_platform_values.get(os, ""),
198194
}
195+
set_missing_env_defaults(env)
199196

200-
# This is split by topic
201-
return env | env_aliases()
197+
return env
202198

203-
def env_aliases():
199+
def create_env():
204200
return {
201+
# This is split by topic
205202
"_aliases": {
206203
"platform_machine": platform_machine_aliases,
207204
},
208205
}
206+
207+
def set_missing_env_defaults(env):
208+
"""Sets defaults based on existing values.
209+
210+
Args:
211+
env: dict; NOTE: modified in-place
212+
"""
213+
if "implementation_name" not in env:
214+
# Use cpython as the default because it's likely the correct value.
215+
env["implementation_name"] = "cpython"
216+
if "platform_python_implementation" not in env:
217+
# The `platform_python_implementation` marker value is supposed to come
218+
# from `platform.python_implementation()`, however, PEP 421 introduced
219+
# `sys.implementation.name` and the `implementation_name` env marker to
220+
# replace it. Per the platform.python_implementation docs, there's now
221+
# essentially just two possible "registered" values: CPython or PyPy.
222+
# Rather than add a field to the toolchain, we just special case the value
223+
# from `sys.implementation.name` to handle the two documented values.
224+
platform_python_impl = env["implementation_name"]
225+
if platform_python_impl == "cpython":
226+
platform_python_impl = "CPython"
227+
elif platform_python_impl == "pypy":
228+
platform_python_impl = "PyPy"
229+
env["platform_python_implementation"] = platform_python_impl
230+
if "platform_release" not in env:
231+
env["platform_release"] = ""
232+
if "platform_version" not in env:
233+
env["platform_version"] = "0"

0 commit comments

Comments
 (0)