From 6feb10db8f3cafd36ab26f23b86e897f0c3cbe64 Mon Sep 17 00:00:00 2001 From: Dustin Ingram Date: Wed, 25 Apr 2018 19:23:49 -0500 Subject: [PATCH] Fix edge case w/ identical canonical versions --- tests/unit/packaging/test_models.py | 8 ++++++++ warehouse/packaging/models.py | 19 +++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/tests/unit/packaging/test_models.py b/tests/unit/packaging/test_models.py index f8fe94e10246..e3bf417f2d2d 100644 --- a/tests/unit/packaging/test_models.py +++ b/tests/unit/packaging/test_models.py @@ -92,6 +92,14 @@ def test_traversal_cant_find(self, db_request): with pytest.raises(KeyError): project["1.0"] + def test_traversal_cant_find_if_multiple(self, db_request): + project = DBProjectFactory.create() + DBReleaseFactory.create(version='1.0.0', project=project) + DBReleaseFactory.create(version='1.0', project=project) + + with pytest.raises(KeyError): + project["1"] + def test_doc_url_doesnt_exist(self, db_request): project = DBProjectFactory.create() assert project.documentation_url is None diff --git a/warehouse/packaging/models.py b/warehouse/packaging/models.py index 765d109fed76..a344d1aff1e2 100644 --- a/warehouse/packaging/models.py +++ b/warehouse/packaging/models.py @@ -156,13 +156,20 @@ def __getitem__(self, version): # There are multiple releases of this project which have the same # canonical version that were uploaded before we checked for # canonical version equivalence, so return the exact match instead - return ( - session.query(Release) - .filter( - (Release.project == self) & (Release.version == version) + try: + return ( + session.query(Release) + .filter( + (Release.project == self) & + (Release.version == version) + ) + .one() ) - .one() - ) + except NoResultFound: + # There are multiple releases of this project which have the + # same canonical version, but none that have the exact version + # specified, so just 404 + raise KeyError from None except NoResultFound: raise KeyError from None