fix(pypi): reject the not operator in the env markers The dependency specifiers spec does not have a unary `not`, only the `not in` operator, and `packaging` rejects the markers that use it. We used to accept it as an extension, which means that an invalid marker was evaluated instead of being reported. Removing it also means that the evaluation no longer needs to track the pending negations for each parenthesis.
diff --git a/news/pypi-env-marker-not.changed.md b/news/pypi-env-marker-not.changed.md new file mode 100644 index 0000000..855b593 --- /dev/null +++ b/news/pypi-env-marker-not.changed.md
@@ -0,0 +1,4 @@ +(pypi) The `not` operator, e.g. `not os_name == "nt"`, is no longer accepted in +the environment markers, because it is not in the +[dependency specifiers spec](https://packaging.python.org/en/latest/specifications/dependency-specifiers/#environment-markers). +Use the inverse operator instead, e.g. `os_name != "nt"`.
diff --git a/python/private/pypi/pep508_evaluate.bzl b/python/private/pypi/pep508_evaluate.bzl index 29a52ae..ecf0ef6 100644 --- a/python/private/pypi/pep508_evaluate.bzl +++ b/python/private/pypi/pep508_evaluate.bzl
@@ -62,7 +62,6 @@ _OPERATOR_CHARS = "<>!=~" _AND = "and" _OR = "or" -_NOT = "not" # The key of the `env` that has the functions to normalize the strings that # the variables are compared with, e.g. `{"platform_machine": lambda x: ...}`. @@ -143,7 +142,7 @@ for word in text.replace("(", " ( ").replace(")", " ) ").replace("\t", " ").split(" "): if not word: continue - elif word == "in" and tokens and tokens[-1] == _NOT: + elif word == "in" and tokens and tokens[-1] == "not": tokens[-1] = "not in" elif word in ["(", ")"] or not word.strip(_OPERATOR_CHARS) or _is_name(word): tokens.append(word) @@ -210,9 +209,6 @@ return None, "Expected 'and', 'or' or ')', but got '{}'".format(token) elif token == "(": depth += 1 - elif token == _NOT: - # NOTE: the spec does not have the `not` operator, it is our extension. - pass else: token, err = _parse_expr(tokens[i:i + 3]) if err: @@ -297,12 +293,11 @@ parsed = marker # Starlark does not have recursion, hence there is a stack with a frame - # for each open parenthesis. Because `not` binds tighter than `and`, which - # binds tighter than `or`, a frame is: + # for each open parenthesis. Because `and` binds tighter than `or`, a + # frame is: # * The value of the `or` operands that we have seen so far. # * The value of the `and` operands that we have seen since the last `or`. - # * The number of `not` to apply to the next operand. - frame = [False, True, 0] + frame = [False, True] stack = [frame] for token in parsed: if token == _AND: @@ -313,11 +308,8 @@ frame[0] = _or(frame[0], frame[1]) frame[1] = True continue - elif token == _NOT: - frame[2] += 1 - continue elif token == "(": - frame = [False, True, 0] + frame = [False, True] stack.append(frame) continue else: # token == ")" @@ -327,9 +319,6 @@ value = "({})".format(value) frame = stack[-1] - for _ in range(frame[2]): - value = "not {}".format(value) if type(value) == "string" else not value - frame[2] = 0 frame[1] = _and(frame[1], value) return _or(frame[0], frame[1])
diff --git a/tests/perf/README.md b/tests/perf/README.md index ace9113..8945760 100644 --- a/tests/perf/README.md +++ b/tests/perf/README.md
@@ -80,14 +80,14 @@ | Workload | Steps | Bazel, µs | |---|---:|---:| -| `evaluate_long` | 1974 | 383.6 | -| `evaluate_long_parsed` | 693 | 114.1 | -| `evaluate_non_strict` | 286 | 58.4 | -| `evaluate_string` | 122 | 24.5 | -| `evaluate_two_strings` | 229 | 43.0 | -| `evaluate_version` | 283 | 56.4 | -| `evaluate_version_parsed` | 132 | 21.3 | -| `parse_long` | 1284 | 231.5 | +| `evaluate_long` | 1926 | 383.6 | +| `evaluate_long_parsed` | 657 | 114.1 | +| `evaluate_non_strict` | 280 | 58.4 | +| `evaluate_string` | 119 | 24.5 | +| `evaluate_two_strings` | 223 | 43.0 | +| `evaluate_version` | 280 | 56.4 | +| `evaluate_version_parsed` | 130 | 21.3 | +| `parse_long` | 1272 | 231.5 | | `tokenize_long` | 514 | 112.9 | | `tokenize_no_whitespace` | 84 | 23.0 | | `tokenize_version` | 48 | 10.8 |
diff --git a/tests/perf/steps_test.py b/tests/perf/steps_test.py index 3fb5338..c731805 100644 --- a/tests/perf/steps_test.py +++ b/tests/perf/steps_test.py
@@ -9,14 +9,14 @@ # numbers with the output of `bazel run //tests/perf:benchmark`. _STEPS = { # keep sorted - "evaluate_long": 1974, - "evaluate_long_parsed": 693, - "evaluate_non_strict": 286, - "evaluate_string": 122, - "evaluate_two_strings": 229, - "evaluate_version": 283, - "evaluate_version_parsed": 132, - "parse_long": 1284, + "evaluate_long": 1926, + "evaluate_long_parsed": 657, + "evaluate_non_strict": 280, + "evaluate_string": 119, + "evaluate_two_strings": 223, + "evaluate_version": 280, + "evaluate_version_parsed": 130, + "parse_long": 1272, "tokenize_long": 514, "tokenize_no_whitespace": 84, "tokenize_version": 48,
diff --git a/tests/pypi/pep508/evaluate_tests.bzl b/tests/pypi/pep508/evaluate_tests.bzl index 5b521d5..745eed6 100644 --- a/tests/pypi/pep508/evaluate_tests.bzl +++ b/tests/pypi/pep508/evaluate_tests.bzl
@@ -32,15 +32,14 @@ "'x' not in os_name": ['"x"', "not in", "os_name"], "('arm' in platform_machine)": ["(", '"arm"', "in", "platform_machine", ")"], "()": ["(", ")"], - "(os_name == 'osx' and not os_name == 'posix') or os_name == \"win\"": [ + "(os_name == 'osx' and os_name != 'posix') or os_name == \"win\"": [ "(", "os_name", "==", '"osx"', "and", - "not", "os_name", - "==", + "!=", '"posix"', ")", "or", @@ -86,6 +85,10 @@ "os_name == 'posix')", "and os_name == 'posix'", "not", + # The `not` operator is not in the spec. + "not os_name == 'posix'", + "not (os_name == 'posix')", + "os_name == 'posix' and not os_name == 'nt'", "os_name", "os_name 'posix'", "os_name <> 'posix'", @@ -268,20 +271,13 @@ "(os_name == 'fo')": False, "((os_name == 'fo'))": False, "((os_name == 'foo'))": True, - "not (os_name == 'fo')": True, # and "os_name == 'fo' and os_name == 'foo'": False, - # and not - "os_name == 'fo' and not os_name == 'foo'": False, - # or "os_name == 'oo' or os_name == 'foo'": True, - # or not - "os_name == 'foo' or not os_name == 'foo'": True, - # multiple or "os_name == 'oo' or os_name == 'fo' or os_name == 'foo'": True, "os_name == 'oo' or os_name == 'foo' or os_name == 'fo'": True, @@ -289,19 +285,11 @@ # multiple and "os_name == 'foo' and os_name == 'foo' and os_name == 'fo'": False, - # x or not y and z != (x or not y), but is instead evaluated as x or (not y and z) - "os_name == 'foo' or not os_name == 'fo' and os_name == 'fo'": True, - # x or y and z != (x or y) and z, but is instead evaluated as x or (y and z) "os_name == 'foo' or os_name == 'fo' and os_name == 'fo'": True, - "not (os_name == 'foo' or os_name == 'fo' and os_name == 'fo')": False, # x or y and z and w != (x or y and z) and w, but is instead evaluated as x or (y and z and w) "os_name == 'foo' or os_name == 'fo' and os_name == 'fo' and os_name == 'fo'": True, - - # not not True - "not not os_name == 'foo'": True, - "not not not os_name == 'foo'": False, }.items(): # buildifier: @unsorted-dict-items _check_evaluate(env, input, want, {"os_name": "foo"}) @@ -404,8 +392,8 @@ "(extra == 'foo' or os_name == 'nt') and sys_platform == 'linux'", "(extra == 'bar' or os_name == 'posix') and python_version < '3.11'", "extra == 'foo' and (os_name == 'nt' or python_version >= '3.8')", - "not (extra == 'foo' or os_name == 'nt') or sys_platform == 'linux'", - "not extra == 'bar' and not os_name == 'nt'", + "(extra != 'foo' and os_name != 'nt') or sys_platform == 'linux'", + "extra != 'bar' and os_name != 'nt'", "platform_version == 'a \"b\" c' and extra == 'foo'", "platform_version == \"it's\" or python_version >= '3.8' and os_name == 'posix'", ]: