Skip to content

Commit 2d6d4c5

Browse files
committed
fix env marker evaluation with double brackets
1 parent d049788 commit 2d6d4c5

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

python/private/pypi/pep508_evaluate.bzl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def evaluate(marker, *, env, strict = True, **kwargs):
138138
"""
139139
tokens = tokenize(marker)
140140

141-
ast = _new_expr(**kwargs)
141+
ast = _new_expr(marker = marker, **kwargs)
142142
for _ in range(len(tokens) * 2):
143143
if not tokens:
144144
break
@@ -219,17 +219,20 @@ def _not_fn(x):
219219
return not x
220220

221221
def _new_expr(
222+
*,
223+
marker,
222224
and_fn = _and_fn,
223225
or_fn = _or_fn,
224226
not_fn = _not_fn):
225227
# buildifier: disable=uninitialized
226228
self = struct(
229+
marker = marker,
227230
tree = [],
228231
parse = lambda **kwargs: _parse(self, **kwargs),
229232
value = lambda: _value(self),
230233
# This is a way for us to have a handle to the currently constructed
231234
# expression tree branch.
232-
current = lambda: self._current[0] if self._current else None,
235+
current = lambda: self._current[-1] if self._current else None,
233236
_current = [],
234237
_and = and_fn,
235238
_or = or_fn,
@@ -393,20 +396,26 @@ def _append(self, value):
393396
current.tree.append(value)
394397
elif hasattr(current.tree[-1], "append"):
395398
current.tree[-1].append(value)
396-
else:
399+
elif hasattr(current.tree, "_append"):
397400
current.tree._append(value)
401+
else:
402+
fail("Cannot evaluate '{}' in '{}', current: {}".format(value, self.marker, current))
398403

399404
def _open_parenthesis(self):
400405
"""Add an extra node into the tree to perform evaluate inside parenthesis."""
401406
self._current.append(_new_expr(
407+
marker = self.marker,
402408
and_fn = self._and,
403409
or_fn = self._or,
404410
not_fn = self._not,
405411
))
406412

407413
def _close_parenthesis(self):
408414
"""Backtrack and evaluate the expression within parenthesis."""
409-
value = self._current.pop().value()
415+
current = self.current()
416+
value = current.value()
417+
self._current.pop()
418+
410419
if type(value) == type(""):
411420
return "({})".format(value)
412421
else:

tests/pypi/pep508/evaluate_tests.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ def _logical_expression_tests(env):
148148
# expr
149149
"os_name == 'fo'": False,
150150
"(os_name == 'fo')": False,
151+
"((os_name == 'fo'))": False,
152+
"((os_name == 'foo'))": True,
151153
"not (os_name == 'fo')": True,
152154

153155
# and

0 commit comments

Comments
 (0)