Skip to content

Commit 3ca52dc

Browse files
committed
Raise RequirementsFileParseError when missing closing quotation
shlex.split, used to split options in requirements.txt files, might raise a ValueError when the input string is not well formed. Catch the ValueError and re-raise it as a RequirementsFileParseError instead.
1 parent e6e7c12 commit 3ca52dc

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

news/11491.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Raise RequirementsFileParseError when parsing malformed requirements options that can't be sucessfully parsed by shlex.

src/pip/_internal/req/req_file.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,12 @@ def parse_line(line: str) -> Tuple[str, Values]:
397397

398398
args_str, options_str = break_args_options(line)
399399

400-
opts, _ = parser.parse_args(shlex.split(options_str), defaults)
400+
try:
401+
options = shlex.split(options_str)
402+
except ValueError as e:
403+
raise OptionParsingError(f"Could not split options: {options_str}") from e
404+
405+
opts, _ = parser.parse_args(options, defaults)
401406

402407
return args_str, opts
403408

tests/unit/test_req_file.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,20 @@ def test_req_file_parse_comment_start_of_line(
786786

787787
assert not reqs
788788

789+
def test_invalid_options(self, tmpdir: Path, finder: PackageFinder) -> None:
790+
"""
791+
Test parsing invalid options such as missing closing quotation
792+
"""
793+
with open(tmpdir.joinpath("req1.txt"), "w") as fp:
794+
fp.write("--'data\n")
795+
796+
with pytest.raises(RequirementsFileParseError):
797+
list(
798+
parse_reqfile(
799+
tmpdir.joinpath("req1.txt"), finder=finder, session=PipSession()
800+
)
801+
)
802+
789803
def test_req_file_parse_comment_end_of_line_with_url(
790804
self, tmpdir: Path, finder: PackageFinder
791805
) -> None:

0 commit comments

Comments
 (0)