From df253805916c9752221f8a39289dee738e122a46 Mon Sep 17 00:00:00 2001 From: Benoit Pierre Date: Wed, 14 Sep 2016 22:51:20 +0200 Subject: [PATCH] fix regression with environment markers handling Ensure the result of str(Requirement()) is itself a valid requirement line that can be parsed back. --- pip/_vendor/packaging/markers.py | 10 +++++++--- tests/unit/test_req.py | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/pip/_vendor/packaging/markers.py b/pip/_vendor/packaging/markers.py index 9195243533c..c5975b0e53f 100644 --- a/pip/_vendor/packaging/markers.py +++ b/pip/_vendor/packaging/markers.py @@ -56,11 +56,15 @@ def __repr__(self): class Variable(Node): - pass + + def __str__(self): + return self.value class Value(Node): - pass + + def __str__(self): + return repr(str(self.value)) VARIABLE = ( @@ -151,7 +155,7 @@ def _format_marker(marker, first=True): else: return "(" + " ".join(inner) + ")" elif isinstance(marker, tuple): - return '{0} {1} "{2}"'.format(*marker) + return '{0} {1} {2}'.format(*marker) else: return marker diff --git a/tests/unit/test_req.py b/tests/unit/test_req.py index 40ceb8f6fd7..f079881fe29 100644 --- a/tests/unit/test_req.py +++ b/tests/unit/test_req.py @@ -603,3 +603,26 @@ def test_exclusive_environment_markers(): req_set.add_requirement(eq26) req_set.add_requirement(ne26) assert req_set.has_requirement('Django') + + +def test_requirement_str(): + for req_str, req_cannonical_str in ( + ('pywin32 (>1.0); sys.platform == "win32"', + "pywin32>1.0; sys_platform == 'win32'"), + ("pywin31; sys.platform == 'win32'", + "pywin31; sys_platform == 'win32'"), + ("foo != 1.3; platform.machine=='i386'", + "foo!=1.3; platform_machine == 'i386'"), + ("bar; python_version=='2.4'or python_version==\"2.5\"", + "bar; python_version == '2.4' or python_version == '2.5'"), + ("bar; ('3.2' <= python_version and python_version <= '3.5')", + "bar; '3.2' <= python_version and python_version <= '3.5'"), + ("libxslt; 'linux' in sys.platform", + "libxslt; 'linux' in sys_platform"), + ("foobar; '\"' in sys_platform", + "foobar; '\"' in sys_platform"), + ("foobar; \"'\" in sys_platform", + "foobar; \"'\" in sys_platform"), + ): + req = Requirement(req_str) + assert str(req) == req_cannonical_str