Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

req: fix possible exception in install_req_from_req #5893

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/5889.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix exception when installing a package depending on an already installed dependency using a PEP 508 requirement.
1 change: 1 addition & 0 deletions news/5892.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve handling of file URIs: correctly handle `file://localhost/...` and don't try to use UNC paths on Unix.
13 changes: 11 additions & 2 deletions src/pip/_internal/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,18 @@ def url_to_path(url):

_, netloc, path, _, _ = urllib_parse.urlsplit(url)

# if we have a UNC path, prepend UNC share notation
if netloc:
netloc = '\\\\' + netloc
if netloc == 'localhost':
# According to RFC 8089, same as empty authority.
netloc = ''
elif sys.platform == 'win32':
# If we have a UNC path, prepend UNC share notation.
netloc = '\\\\' + netloc
else:
raise ValueError(
'non-local file URIs are not supported on this platform: %r'
% url
)

path = urllib_request.url2pathname(netloc + path)
return path
Expand Down
3 changes: 2 additions & 1 deletion src/pip/_internal/req/constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ def install_req_from_req(
PyPI.file_storage_domain,
TestPyPI.file_storage_domain,
]
if req.url and comes_from.link.netloc in domains_not_allowed:
if req.url and comes_from and comes_from.link \
and comes_from.link.netloc in domains_not_allowed:
# Explicitly disallow pypi packages that depend on external urls
raise InstallationError(
"Packages installed from PyPI cannot depend on packages "
Expand Down
37 changes: 34 additions & 3 deletions tests/functional/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
from pip._internal.models.index import PyPI, TestPyPI
from pip._internal.utils.misc import rmtree
from tests.lib import (
_create_svn_repo, _create_test_package, create_test_package_with_setup,
need_bzr, need_mercurial, path_to_url, pyversion, pyversion_tuple,
requirements_file,
_create_svn_repo, _create_test_package, create_basic_wheel_for_package,
create_test_package_with_setup, need_bzr, need_mercurial, path_to_url,
pyversion, pyversion_tuple, requirements_file,
)
from tests.lib.local_repos import local_checkout
from tests.lib.path import Path
Expand Down Expand Up @@ -1366,6 +1366,37 @@ def test_install_pep508_with_url_in_install_requires(script):
assert "Successfully installed packaging-15.3" in str(res), str(res)


def test_pep508_url_already_installed(script):
pkgA_path = create_basic_wheel_for_package(
script,
name='pkgA', version='1.0',
depends=[], extras={},
)
pkgA_file_url = path_to_url(pkgA_path)
assert pkgA_file_url.startswith('file:///')
pkgA_file_url = 'file://localhost/' + pkgA_file_url[8:]
pkgB_path = create_basic_wheel_for_package(
script,
name='pkgB', version='2.0',
depends=['pkgA @ %s' % pkgA_file_url],
extras={},
)
pkgC_path = create_basic_wheel_for_package(
script,
name='pkgC', version='3.0',
depends=['pkgB'],
extras={},
)
r = script.pip_install_local(
'-v', pkgB_path
)
assert "Successfully installed pkgA-1.0 pkgB-2.0" in r.stdout, str(r)
r = script.pip_install_local(
'-v', pkgC_path
)
assert "Successfully installed pkgC-3.0" in r.stdout, str(r)


@pytest.mark.network
@pytest.mark.parametrize('index', (PyPI.simple_url, TestPyPI.simple_url))
def test_install_from_test_pypi_with_ext_url_dep_is_blocked(script, index):
Expand Down
10 changes: 9 additions & 1 deletion tests/unit/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,12 @@ def test_path_to_url_unix():

@pytest.mark.skipif("sys.platform == 'win32'")
def test_url_to_path_unix():
assert url_to_path('file:tmp') == 'tmp'
assert url_to_path('file:///tmp/file') == '/tmp/file'
assert url_to_path('file:/path/to/file') == '/path/to/file'
assert url_to_path('file://localhost/tmp/file') == '/tmp/file'
with pytest.raises(ValueError):
url_to_path('file://somehost/tmp/file')


@pytest.mark.skipif("sys.platform != 'win32'")
Expand All @@ -141,8 +146,11 @@ def test_path_to_url_win():

@pytest.mark.skipif("sys.platform != 'win32'")
def test_url_to_path_win():
assert url_to_path('file:///c:/tmp/file') == 'C:\\tmp\\file'
assert url_to_path('file:tmp') == 'tmp'
assert url_to_path('file:///c:/tmp/file') == r'C:\tmp\file'
assert url_to_path('file://unc/as/path') == r'\\unc\as\path'
assert url_to_path('file:c:/path/to/file') == r'C:\path\to\file'
assert url_to_path('file://localhost/c:/tmp/file') == r'C:\tmp\file'


@pytest.mark.skipif("sys.platform != 'win32'")
Expand Down