Skip to content

Commit

Permalink
Markers: Use PEP 440 version comparison only when both sides are vali…
Browse files Browse the repository at this point in the history
…d version specifier.
  • Loading branch information
vphilippon committed Mar 24, 2017
1 parent 3477839 commit 2156649
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Changelog

.. note:: This version is not yet released and is under active development.

* Fix a bug where ``packaging.markers.Marker.evaluate`` used PEP 440
version comparison when the left side is not a valid version specifier.


16.8 - 2016-10-29
~~~~~~~~~~~~~~~~~
Expand Down
6 changes: 5 additions & 1 deletion packaging/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from ._compat import string_types
from .specifiers import Specifier, InvalidSpecifier
from .version import Version, InvalidVersion


__all__ = [
Expand Down Expand Up @@ -182,8 +183,11 @@ def _format_marker(marker, first=True):

def _eval_op(lhs, op, rhs):
try:
# PEP 508: Use the PEP-440 version comparison rules
# when both sides have a valid version specifier.
spec = Specifier("".join([op.serialize(), rhs]))
except InvalidSpecifier:
lhs = Version(lhs)
except (InvalidSpecifier, InvalidVersion):
pass
else:
return spec.contains(lhs)
Expand Down
40 changes: 40 additions & 0 deletions tests/test_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,46 @@ def test_extra_with_no_extra_in_environment(self):
{"extra": "security"},
True,
),
(
"extra == 'security'",
{"extra": None},
False,
),
(
"extra == '3.7.0'",
{"extra": None},
False,
),
(
"extra == '3.7.0'",
{"extra": "foobar"},
False,
),
(
"extra == '3.7.0'",
{"extra": "3.7.0"},
True,
),
(
"extra == '3.7.0'",
{"extra": "3.7"},
True,
),
(
"extra == '3.7.0'",
{"extra": "3.7.0.0"},
True,
),
(
"extra == '3.7.0'",
{"extra": "3.7.0+youaregreat"},
True,
),
(
"extra == '3.7.0'",
{"extra": "3.7.1"},
False,
),
],
)
def test_evaluates(self, marker_string, environment, expected):
Expand Down

0 comments on commit 2156649

Please sign in to comment.