Skip to content

Commit

Permalink
Use version comparison logic for python_full_version. (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
apeschar committed Oct 31, 2022
1 parent 98b9b89 commit 6153498
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 6 additions & 2 deletions distlib/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
__all__ = ['interpret']

_VERSION_PATTERN = re.compile(r'((\d+(\.\d+)*\w*)|\'(\d+(\.\d+)*\w*)\'|\"(\d+(\.\d+)*\w*)\")')
_VERSION_MARKERS = {'python_version', 'python_full_version'}

def _is_version_marker(s):
return isinstance(s, string_types) and s in _VERSION_MARKERS

def _is_literal(o):
if not isinstance(o, string_types) or not o:
Expand Down Expand Up @@ -77,11 +81,11 @@ def evaluate(self, expr, context):

lhs = self.evaluate(elhs, context)
rhs = self.evaluate(erhs, context)
if ((elhs == 'python_version' or erhs == 'python_version') and
if ((_is_version_marker(elhs) or _is_version_marker(erhs)) and
op in ('<', '<=', '>', '>=', '===', '==', '!=', '~=')):
lhs = NV(lhs)
rhs = NV(rhs)
elif elhs == 'python_version' and op in ('in', 'not in'):
elif _is_version_marker(elhs) and op in ('in', 'not in'):
lhs = NV(lhs)
rhs = _get_versions(rhs)
result = self.operations[op](lhs, rhs)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ def test_interpret(self):
self.assertTrue(interpret(
"'buuuu' not in os_name and '%s' in os_name" % os_name))

# normalized version comparison correctness
self.assertTrue(interpret('python_version > "5.0"', {'python_version': '10.0'}))
self.assertTrue(interpret('python_version == "5.0"', {'python_version': '5.0'}))
self.assertTrue(interpret('python_version < "5.0"', {'python_version': '5.0b0'}))
self.assertTrue(interpret('python_full_version > "5.0"', {'python_full_version': '10.0'}))

# execution context
self.assertTrue(interpret('python_version == "0.1"',
{'python_version': '0.1'}))
Expand Down

0 comments on commit 6153498

Please sign in to comment.