Skip to content

Commit 8d15e3b

Browse files
committed
Use re.escape for windows paths
1 parent 59dbbba commit 8d15e3b

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

src/pip/_internal/req/req_file.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,12 @@ def _parse_and_recurse(
367367
if req_path in self._parsed_files.keys():
368368
initial_file = self._parsed_files[req_path]
369369
tail = (
370-
f"and again in {initial_file}"
370+
f" and again in {initial_file}"
371371
if initial_file is not None
372372
else ""
373373
)
374374
raise RequirementsFileParseError(
375-
f"{req_path} recursively references itself in {filename} {tail}"
375+
f"{req_path} recursively references itself in {filename}{tail}"
376376
)
377377
# Keeping a track where was each file first included in
378378
self._parsed_files[req_path] = filename

tests/unit/test_req_file.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import collections
22
import logging
33
import os
4+
import re
45
import textwrap
56
from optparse import Values
67
from pathlib import Path
@@ -78,12 +79,6 @@ def parse_reqfile(
7879
)
7980

8081

81-
def path_to_string(p: Path) -> str:
82-
# Escape the back slashes in windows path before using it
83-
# in a regex
84-
return str(p).replace("\\", "\\\\")
85-
86-
8782
def test_read_file_url(tmp_path: Path, session: PipSession) -> None:
8883
reqs = tmp_path.joinpath("requirements.txt")
8984
reqs.write_text("foo")
@@ -365,8 +360,8 @@ def test_recursive_requirements_file(
365360
with pytest.raises(
366361
RequirementsFileParseError,
367362
match=(
368-
f"{path_to_string(req_files[0])} recursively references itself"
369-
f" in {path_to_string(req_files[req_file_count - 1])}"
363+
f"{re.escape(str(req_files[0]))} recursively references itself"
364+
f" in {re.escape(str(req_files[req_file_count - 1]))}"
370365
),
371366
):
372367
list(parse_requirements(filename=str(req_files[0]), session=session))
@@ -379,10 +374,10 @@ def test_recursive_requirements_file(
379374
with pytest.raises(
380375
RequirementsFileParseError,
381376
match=(
382-
f"{path_to_string(req_files[req_file_count - 2])} recursively"
377+
f"{re.escape(str(req_files[req_file_count - 2]))} recursively"
383378
" references itself in"
384-
f" {path_to_string(req_files[req_file_count - 1])} and again in"
385-
f" {path_to_string(req_files[req_file_count - 3])}"
379+
f" {re.escape(str(req_files[req_file_count - 1]))} and again in"
380+
f" {re.escape(str(req_files[req_file_count - 3]))}"
386381
),
387382
):
388383
list(parse_requirements(filename=str(req_files[0]), session=session))
@@ -402,8 +397,8 @@ def test_recursive_relative_requirements_file(
402397
with pytest.raises(
403398
RequirementsFileParseError,
404399
match=(
405-
f"{path_to_string(root_req_file)} recursively references itself in"
406-
f" {path_to_string(level_2_req_file)}"
400+
f"{re.escape(str(root_req_file))} recursively references itself in"
401+
f" {re.escape(str(level_2_req_file))}"
407402
),
408403
):
409404
list(parse_requirements(filename=str(root_req_file), session=session))

0 commit comments

Comments
 (0)