@@ -56,6 +56,16 @@ _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+ }
5969
6070def tokenize (marker ):
6171 """Tokenize the input string.
@@ -123,13 +133,17 @@ def tokenize(marker):
123133
124134 return fail ("BUG: failed to process the marker in allocated cycles: {}" .format (marker ))
125135
126- def evaluate (marker , * , env , strict = True , ** kwargs ):
136+ def evaluate (marker , * , env , strict = True , value_aliases = _VALUE_ALIASES , ** kwargs ):
127137 """Evaluate the marker against a given env.
128138
129139 Args:
130- marker: {type}`str`: The string marker to evaluate.
131- env: {type}`dict`: The environment to evaluate the marker against.
132- strict: {type}`bool`: A setting to not fail on missing values in the env.
140+ marker: {type}`str` The string marker to evaluate.
141+ env: {type}`dict` The environment to evaluate the marker against.
142+ 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.
133147 **kwargs: Extra kwargs to be passed to the expression evaluator.
134148
135149 Returns:
@@ -142,7 +156,7 @@ def evaluate(marker, *, env, strict = True, **kwargs):
142156 if not tokens :
143157 break
144158
145- tokens = ast .parse (env = env , tokens = tokens , strict = strict )
159+ tokens = ast .parse (env = env , tokens = tokens , strict = strict , value_aliases = value_aliases )
146160
147161 if not tokens :
148162 return ast .value ()
@@ -236,7 +250,7 @@ def _new_expr(
236250 )
237251 return self
238252
239- def _parse (self , * , env , tokens , strict = False ):
253+ def _parse (self , * , env , tokens , strict = False , value_aliases = {} ):
240254 """The parse function takes the consumed tokens and returns the remaining."""
241255 token , remaining = tokens [0 ], tokens [1 :]
242256
@@ -251,7 +265,7 @@ def _parse(self, *, env, tokens, strict = False):
251265 elif token == _NOT :
252266 expr = _not_expr (self )
253267 else :
254- expr = marker_expr (env = env , strict = strict , * tokens [:3 ])
268+ expr = marker_expr (env = env , strict = strict , value_aliases = value_aliases , * tokens [:3 ])
255269 remaining = tokens [3 :]
256270
257271 _append (self , expr )
@@ -277,7 +291,7 @@ def _value(self):
277291
278292 fail ("BUG: invalid state: {}" .format (self .tree ))
279293
280- def marker_expr (left , op , right , * , env , strict = True ):
294+ def marker_expr (left , op , right , * , env , strict = True , value_aliases = {} ):
281295 """Evaluate a marker expression
282296
283297 Args:
@@ -288,6 +302,7 @@ def marker_expr(left, op, right, *, env, strict = True):
288302 in the environment, otherwise returns the original expression.
289303 env: {type}`dict[str, str]` the `env` to substitute `env` identifiers in
290304 the `<left> <op> <right>` expression.
305+ value_aliases: the value normalization used for certain fields.
291306
292307 Returns:
293308 {type}`bool` if the expression evaluation result or {type}`str` if the expression
@@ -300,11 +315,21 @@ def marker_expr(left, op, right, *, env, strict = True):
300315 var_name = right
301316 right = env [right ]
302317 left = left .strip ("\" " )
318+
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 )
303325 else :
304326 var_name = left
305327 left = env [left ]
306328 right = right .strip ("\" " )
307329
330+ # See the note above on normalization
331+ right = value_aliases .get (var_name , {}).get (right , right )
332+
308333 if var_name in _NON_VERSION_VAR_NAMES :
309334 return _env_expr (left , op , right )
310335 elif var_name .endswith ("_version" ):
0 commit comments