Skip to content

Commit

Permalink
Enforce word boundaries in operators and names
Browse files Browse the repository at this point in the history
This ensures that these are only parsed when they're independent words.
  • Loading branch information
pradyunsg committed Dec 7, 2022
1 parent b41326d commit cb09331
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
12 changes: 6 additions & 6 deletions packaging/_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ def __str__(self) -> str:
re.VERBOSE,
),
"OP": r"(===|==|~=|!=|<=|>=|<|>)",
"BOOLOP": r"(or|and)",
"IN": r"in",
"NOT": r"not",
"BOOLOP": r"\b(or|and)\b",
"IN": r"\bin\b",
"NOT": r"\bnot\b",
"VARIABLE": re.compile(
r"""
(
\b(
python_version
|python_full_version
|os[._]name
Expand All @@ -68,14 +68,14 @@ def __str__(self) -> str:
|python_implementation
|implementation_(name|version)
|extra
)
)\b
""",
re.VERBOSE,
),
"VERSION": re.compile(Specifier._version_regex_str, re.VERBOSE | re.IGNORECASE),
"AT": r"\@",
"URL": r"[^ \t]+",
"IDENTIFIER": r"[a-zA-Z0-9][a-zA-Z0-9._-]*",
"IDENTIFIER": r"\b[a-zA-Z0-9][a-zA-Z0-9._-]*\b",
"WS": r"[ \t]+",
"END": r"$",
}
Expand Down
22 changes: 20 additions & 2 deletions tests/test_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,27 @@ def test_error_invalid_marker_notin_without_whitespace(self) -> None:
# THEN
assert ctx.exconly() == (
"packaging.requirements.InvalidRequirement: "
"Expected whitespace after 'not'\n"
"Expected marker operator, one of <=, <, !=, ==, >=, >, ~=, ===, "
"not, not in\n"
" name; '3.7' notin python_version\n"
" ^"
" ^"
)

def test_error_when_no_word_boundary(self) -> None:
# GIVEN
to_parse = "name; '3.6'inpython_version"

# WHEN
with pytest.raises(InvalidRequirement) as ctx:
Requirement(to_parse)

# THEN
assert ctx.exconly() == (
"packaging.requirements.InvalidRequirement: "
"Expected marker operator, one of <=, <, !=, ==, >=, >, ~=, ===, "
"not, not in\n"
" name; '3.6'inpython_version\n"
" ^"
)

def test_error_invalid_marker_not_without_in(self) -> None:
Expand Down

0 comments on commit cb09331

Please sign in to comment.