From b391137bca65d547ae248bd5c30ed09b08c470b6 Mon Sep 17 00:00:00 2001 From: Arun Babu Neelicattu Date: Fri, 22 Mar 2024 23:38:55 +0100 Subject: [PATCH 1/2] inspection/info: correctly raise PackageInfoError Previously, when processing wheel files, PackageInfo incorrectly returned an empty instance. This change ensures a PackageInfoError is correctly raised. --- src/poetry/inspection/info.py | 4 +-- tests/repositories/test_http_repository.py | 30 +++++++++++++++++----- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/poetry/inspection/info.py b/src/poetry/inspection/info.py index a088fd9c0db..e0e4884f868 100644 --- a/src/poetry/inspection/info.py +++ b/src/poetry/inspection/info.py @@ -545,8 +545,8 @@ def from_wheel(cls, path: Path) -> PackageInfo: try: wheel = pkginfo.Wheel(str(path)) return cls._from_distribution(wheel) - except ValueError: - return PackageInfo() + except ValueError as e: + raise PackageInfoError(path, e) @classmethod def from_bdist(cls, path: Path) -> PackageInfo: diff --git a/tests/repositories/test_http_repository.py b/tests/repositories/test_http_repository.py index 263da397491..6d7fd667b02 100644 --- a/tests/repositories/test_http_repository.py +++ b/tests/repositories/test_http_repository.py @@ -1,5 +1,6 @@ from __future__ import annotations +import contextlib import shutil from pathlib import Path @@ -12,6 +13,7 @@ from packaging.metadata import parse_email from poetry.core.packages.utils.link import Link +from poetry.inspection.info import PackageInfoError from poetry.inspection.lazy_wheel import HTTPRangeRequestUnsupported from poetry.repositories.http_repository import HTTPRepository from poetry.utils.helpers import HTTPRangeRequestSupported @@ -116,13 +118,18 @@ def test_get_info_from_wheel_state_sequence(mocker: MockerFixture) -> None: # 1. range request and download mock_metadata_from_wheel_url.side_effect = HTTPRangeRequestUnsupported - repo._get_info_from_wheel(link) + + with contextlib.suppress(PackageInfoError): + repo._get_info_from_wheel(link) + assert mock_metadata_from_wheel_url.call_count == 1 assert mock_download.call_count == 1 assert mock_download.call_args[1]["raise_accepts_ranges"] is False # 2. only download - repo._get_info_from_wheel(link) + with contextlib.suppress(PackageInfoError): + repo._get_info_from_wheel(link) + assert mock_metadata_from_wheel_url.call_count == 1 assert mock_download.call_count == 2 assert mock_download.call_args[1]["raise_accepts_ranges"] is True @@ -130,27 +137,38 @@ def test_get_info_from_wheel_state_sequence(mocker: MockerFixture) -> None: # 3. download and range request mock_metadata_from_wheel_url.side_effect = None mock_download.side_effect = HTTPRangeRequestSupported - repo._get_info_from_wheel(link) + + with contextlib.suppress(PackageInfoError): + repo._get_info_from_wheel(link) + assert mock_metadata_from_wheel_url.call_count == 2 assert mock_download.call_count == 3 assert mock_download.call_args[1]["raise_accepts_ranges"] is True # 4. only range request - repo._get_info_from_wheel(link) + with contextlib.suppress(PackageInfoError): + repo._get_info_from_wheel(link) + assert mock_metadata_from_wheel_url.call_count == 3 assert mock_download.call_count == 3 # 5. range request and download mock_metadata_from_wheel_url.side_effect = HTTPRangeRequestUnsupported mock_download.side_effect = None - repo._get_info_from_wheel(link) + + with contextlib.suppress(PackageInfoError): + repo._get_info_from_wheel(link) + assert mock_metadata_from_wheel_url.call_count == 4 assert mock_download.call_count == 4 assert mock_download.call_args[1]["raise_accepts_ranges"] is False # 6. only range request mock_metadata_from_wheel_url.side_effect = None - repo._get_info_from_wheel(link) + + with contextlib.suppress(PackageInfoError): + repo._get_info_from_wheel(link) + assert mock_metadata_from_wheel_url.call_count == 5 assert mock_download.call_count == 4 From ef6168997f8a3ea5f5868ea72148473c4b104e17 Mon Sep 17 00:00:00 2001 From: Arun Babu Neelicattu Date: Fri, 22 Mar 2024 23:39:58 +0100 Subject: [PATCH 2/2] inspection: error on unsupported metadata versions Resolves: #9195 --- src/poetry/inspection/info.py | 5 +++++ ...ta_version_unknown-0.1.0-py2.py3-none-any.whl | Bin 0 -> 1153 bytes tests/inspection/test_info.py | 14 ++++++++++++++ 3 files changed, 19 insertions(+) create mode 100644 tests/fixtures/distributions/demo_metadata_version_unknown-0.1.0-py2.py3-none-any.whl diff --git a/src/poetry/inspection/info.py b/src/poetry/inspection/info.py index e0e4884f868..557ebcf3a11 100644 --- a/src/poetry/inspection/info.py +++ b/src/poetry/inspection/info.py @@ -266,6 +266,11 @@ def _from_distribution( :param dist: The distribution instance to parse information from. """ + if dist.metadata_version not in pkginfo.distribution.HEADER_ATTRS: + # This check can be replaced once upstream implements strict parsing + # https://bugs.launchpad.net/pkginfo/+bug/2058697 + raise ValueError("Unknown metadata version") + requirements = cls._requirements_from_distribution(dist) info = cls( diff --git a/tests/fixtures/distributions/demo_metadata_version_unknown-0.1.0-py2.py3-none-any.whl b/tests/fixtures/distributions/demo_metadata_version_unknown-0.1.0-py2.py3-none-any.whl new file mode 100644 index 0000000000000000000000000000000000000000..03988228fec381e1eb7267cbb8a549152ffdffee GIT binary patch literal 1153 zcmWIWW@Zs#W?Ke_98l9aHoi4WgZv)KB$_xKXRI1EB zEWUY3f|SMAZ>e?r^Pd^?KQVOgzo;Fl-YaGx*I#)iC`3g;J@tnD%a3;NH@XIJy>(;Q zZQixWRBY;xNp?&8^EGFm?Aemdlh;&l9(Lf-_w>$l-j@t`&n3HAi%fKi@n2$>C?!C;lHhzv{}t zbGrlPe=FGdRlI5Kq%CtTyyyAx7R;IXCtqPx4tHdbFWai(<eGA5L@q7F3{dy~2aG9-kbHSFmpVL^J zg9UrP35w)i+pV|bppH`OvgyxO?f5L1dy#ANvd{gxJC=Vva%jPkvo$d10PSW_WN6|l zd?^NGfv^A&gA%%ae0*kJW=VX!UO{Did|7Hyab|vAe7u6Kf;u=0sB;B)GcwsTXnrS literal 0 HcmV?d00001 diff --git a/tests/inspection/test_info.py b/tests/inspection/test_info.py index 66bb1f825fb..99fd04d66c0 100644 --- a/tests/inspection/test_info.py +++ b/tests/inspection/test_info.py @@ -199,6 +199,20 @@ def test_info_from_wheel_metadata_version_23(fixture_dir: FixtureDirGetter) -> N assert info._source_url == path.resolve().as_posix() +def test_info_from_wheel_metadata_version_unknown( + fixture_dir: FixtureDirGetter, +) -> None: + path = ( + fixture_dir("distributions") + / "demo_metadata_version_unknown-0.1.0-py2.py3-none-any.whl" + ) + + with pytest.raises(PackageInfoError) as e: + PackageInfo.from_wheel(path) + + assert "Unknown metadata version" in str(e.value) + + def test_info_from_wheel_metadata(demo_wheel_metadata: RawMetadata) -> None: info = PackageInfo.from_metadata(demo_wheel_metadata) demo_check_info(info)