Skip to content

Commit

Permalink
fix regression with environment markers handling
Browse files Browse the repository at this point in the history
Ensure the result of str(Requirement()) is itself a valid
requirement line that can be parsed back.
  • Loading branch information
benoit-pierre committed Oct 10, 2016
1 parent 281eb61 commit df25380
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pip/_vendor/packaging/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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

Expand Down
23 changes: 23 additions & 0 deletions tests/unit/test_req.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit df25380

Please sign in to comment.