@@ -158,6 +158,9 @@ os_name_select_map = {
158158def 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