Skip to content

Commit

Permalink
Use re.escape for windows paths
Browse files Browse the repository at this point in the history
  • Loading branch information
hellozee committed Sep 3, 2024
1 parent 59dbbba commit 8d15e3b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/pip/_internal/req/req_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,12 @@ def _parse_and_recurse(
if req_path in self._parsed_files.keys():
initial_file = self._parsed_files[req_path]
tail = (
f"and again in {initial_file}"
f" and again in {initial_file}"
if initial_file is not None
else ""
)
raise RequirementsFileParseError(
f"{req_path} recursively references itself in {filename} {tail}"
f"{req_path} recursively references itself in {filename}{tail}"
)
# Keeping a track where was each file first included in
self._parsed_files[req_path] = filename
Expand Down
21 changes: 8 additions & 13 deletions tests/unit/test_req_file.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import collections
import logging
import os
import re
import textwrap
from optparse import Values
from pathlib import Path
Expand Down Expand Up @@ -78,12 +79,6 @@ def parse_reqfile(
)


def path_to_string(p: Path) -> str:
# Escape the back slashes in windows path before using it
# in a regex
return str(p).replace("\\", "\\\\")


def test_read_file_url(tmp_path: Path, session: PipSession) -> None:
reqs = tmp_path.joinpath("requirements.txt")
reqs.write_text("foo")
Expand Down Expand Up @@ -365,8 +360,8 @@ def test_recursive_requirements_file(
with pytest.raises(
RequirementsFileParseError,
match=(
f"{path_to_string(req_files[0])} recursively references itself"
f" in {path_to_string(req_files[req_file_count - 1])}"
f"{re.escape(str(req_files[0]))} recursively references itself"
f" in {re.escape(str(req_files[req_file_count - 1]))}"
),
):
list(parse_requirements(filename=str(req_files[0]), session=session))
Expand All @@ -379,10 +374,10 @@ def test_recursive_requirements_file(
with pytest.raises(
RequirementsFileParseError,
match=(
f"{path_to_string(req_files[req_file_count - 2])} recursively"
f"{re.escape(str(req_files[req_file_count - 2]))} recursively"
" references itself in"
f" {path_to_string(req_files[req_file_count - 1])} and again in"
f" {path_to_string(req_files[req_file_count - 3])}"
f" {re.escape(str(req_files[req_file_count - 1]))} and again in"
f" {re.escape(str(req_files[req_file_count - 3]))}"
),
):
list(parse_requirements(filename=str(req_files[0]), session=session))
Expand All @@ -402,8 +397,8 @@ def test_recursive_relative_requirements_file(
with pytest.raises(
RequirementsFileParseError,
match=(
f"{path_to_string(root_req_file)} recursively references itself in"
f" {path_to_string(level_2_req_file)}"
f"{re.escape(str(root_req_file))} recursively references itself in"
f" {re.escape(str(level_2_req_file))}"
),
):
list(parse_requirements(filename=str(root_req_file), session=session))
Expand Down

0 comments on commit 8d15e3b

Please sign in to comment.