Skip to content

Commit 5de40c8

Browse files
committed
Fix NameError when handling InvalidRequirement in install_req_from_req_string
Previously, an InvalidRequirement would raise a NameError while trying to raise an InstallationError because `req` was not defined. Discovered while working on pypa#6402.
1 parent c8e9caa commit 5de40c8

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/pip/_internal/req/constructors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def install_req_from_req_string(
319319
try:
320320
req = Requirement(req_string)
321321
except InvalidRequirement:
322-
raise InstallationError("Invalid requirement: '%s'" % req)
322+
raise InstallationError("Invalid requirement: '%s'" % req_string)
323323

324324
domains_not_allowed = [
325325
PyPI.file_storage_domain,

tests/unit/test_req_install.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55
from pip._vendor.packaging.requirements import Requirement
66

7+
from pip._internal.exceptions import InstallationError
78
from pip._internal.req.constructors import (
89
install_req_from_line, install_req_from_req_string,
910
)
@@ -49,6 +50,14 @@ def test_forward_slash_results_in_a_link(self, tmpdir):
4950

5051
class TestInstallRequirementFrom(object):
5152

53+
def test_install_req_from_string_invalid_requirement(self):
54+
"""
55+
Requirement strings that cannot be parsed by
56+
packaging.requirements.Requirement raise an InstallationError.
57+
"""
58+
with pytest.raises(InstallationError):
59+
install_req_from_req_string("http:/this/is/invalid")
60+
5261
def test_install_req_from_string_without_comes_from(self):
5362
"""
5463
Test to make sure that install_req_from_string succeeds

0 commit comments

Comments
 (0)