Skip to content

Commit f2ec55e

Browse files
committed
refactor
1 parent aa4e055 commit f2ec55e

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

python/private/pypi/pep508_env.bzl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
"""
1717

1818
# See https://stackoverflow.com/questions/45125516/possible-values-for-uname-m
19+
_platform_machine_aliases = {
20+
# These pairs mean the same hardware, but different values may be used
21+
# on different host platforms.
22+
"amd64": "x86_64",
23+
"arm64": "aarch64",
24+
"i386": "x86_32",
25+
"i686": "x86_32",
26+
}
1927
_platform_system_values = {
2028
"linux": "Linux",
2129
"osx": "Darwin",
@@ -74,7 +82,11 @@ def env(target_platform, *, extra = None):
7482
}
7583

7684
# This is split by topic
77-
return env
85+
return env | {
86+
"_aliases": {
87+
"platform_machine": _platform_machine_aliases,
88+
},
89+
}
7890

7991
def _platform(*, abi = None, os = None, arch = None):
8092
return struct(

python/private/pypi/pep508_evaluate.bzl

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,7 @@ _NON_VERSION_VAR_NAMES = [
5656
_AND = "and"
5757
_OR = "or"
5858
_NOT = "not"
59-
_VALUE_ALIASES = {
60-
"platform_machine": {
61-
# These pairs mean the same hardware, but different values may be used
62-
# on different host platforms.
63-
"amd64": "x86_64",
64-
"arm64": "aarch64",
65-
"i386": "x86_32",
66-
"i686": "x86_32",
67-
},
68-
}
59+
_ENV_ALIASES = "_aliases"
6960

7061
def tokenize(marker):
7162
"""Tokenize the input string.
@@ -133,17 +124,13 @@ def tokenize(marker):
133124

134125
return fail("BUG: failed to process the marker in allocated cycles: {}".format(marker))
135126

136-
def evaluate(marker, *, env, strict = True, value_aliases = _VALUE_ALIASES, **kwargs):
127+
def evaluate(marker, *, env, strict = True, **kwargs):
137128
"""Evaluate the marker against a given env.
138129
139130
Args:
140131
marker: {type}`str` The string marker to evaluate.
141132
env: {type}`dict` The environment to evaluate the marker against.
142133
strict: {type}`bool` A setting to not fail on missing values in the env.
143-
value_aliases: {type}`dict` The value normalization to do for certain
144-
fields to ensure that `aarch64` evaluation in the `platform_machine`
145-
works the same way irrespective if the marker uses `arm64` or
146-
`aarch64` value in the expression.
147134
**kwargs: Extra kwargs to be passed to the expression evaluator.
148135
149136
Returns:
@@ -156,7 +143,7 @@ def evaluate(marker, *, env, strict = True, value_aliases = _VALUE_ALIASES, **kw
156143
if not tokens:
157144
break
158145

159-
tokens = ast.parse(env = env, tokens = tokens, strict = strict, value_aliases = value_aliases)
146+
tokens = ast.parse(env = env, tokens = tokens, strict = strict)
160147

161148
if not tokens:
162149
return ast.value()
@@ -250,7 +237,7 @@ def _new_expr(
250237
)
251238
return self
252239

253-
def _parse(self, *, env, tokens, strict = False, value_aliases = {}):
240+
def _parse(self, *, env, tokens, strict = False):
254241
"""The parse function takes the consumed tokens and returns the remaining."""
255242
token, remaining = tokens[0], tokens[1:]
256243

@@ -265,7 +252,7 @@ def _parse(self, *, env, tokens, strict = False, value_aliases = {}):
265252
elif token == _NOT:
266253
expr = _not_expr(self)
267254
else:
268-
expr = marker_expr(env = env, strict = strict, value_aliases = value_aliases, *tokens[:3])
255+
expr = marker_expr(env = env, strict = strict, *tokens[:3])
269256
remaining = tokens[3:]
270257

271258
_append(self, expr)
@@ -291,7 +278,7 @@ def _value(self):
291278

292279
fail("BUG: invalid state: {}".format(self.tree))
293280

294-
def marker_expr(left, op, right, *, env, strict = True, value_aliases = {}):
281+
def marker_expr(left, op, right, *, env, strict = True):
295282
"""Evaluate a marker expression
296283
297284
Args:
@@ -301,8 +288,11 @@ def marker_expr(left, op, right, *, env, strict = True, value_aliases = {}):
301288
strict: {type}`bool` if false, only evaluates the values that are present
302289
in the environment, otherwise returns the original expression.
303290
env: {type}`dict[str, str]` the `env` to substitute `env` identifiers in
304-
the `<left> <op> <right>` expression.
305-
value_aliases: the value normalization used for certain fields.
291+
the `<left> <op> <right>` expression. Note, if `env` has a key
292+
"_aliases", then we will do normalization so that we can ensure
293+
that e.g. `aarch64` evaluation in the `platform_machine` works the
294+
same way irrespective if the marker uses `arm64` or `aarch64` value
295+
in the expression.
306296
307297
Returns:
308298
{type}`bool` if the expression evaluation result or {type}`str` if the expression
@@ -316,19 +306,21 @@ def marker_expr(left, op, right, *, env, strict = True, value_aliases = {}):
316306
right = env[right]
317307
left = left.strip("\"")
318308

319-
# On Windows, Linux, OSX different values may mean the same hardware,
320-
# e.g. Python on Windows returns arm64, but on Linux returns aarch64.
321-
# e.g. Python on Windows returns amd64, but on Linux returns x86_64.
322-
#
323-
# The following normalizes the values
324-
left = value_aliases.get(var_name, {}).get(left, left)
309+
if _ENV_ALIASES in env:
310+
# On Windows, Linux, OSX different values may mean the same hardware,
311+
# e.g. Python on Windows returns arm64, but on Linux returns aarch64.
312+
# e.g. Python on Windows returns amd64, but on Linux returns x86_64.
313+
#
314+
# The following normalizes the values
315+
left = env.get(_ENV_ALIASES, {}).get(var_name, {}).get(left, left)
325316
else:
326317
var_name = left
327318
left = env[left]
328319
right = right.strip("\"")
329320

330-
# See the note above on normalization
331-
right = value_aliases.get(var_name, {}).get(right, right)
321+
if _ENV_ALIASES in env:
322+
# See the note above on normalization
323+
right = env.get(_ENV_ALIASES, {}).get(var_name, {}).get(right, right)
332324

333325
if var_name in _NON_VERSION_VAR_NAMES:
334326
return _env_expr(left, op, right)

0 commit comments

Comments
 (0)