Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path.local: fix == doesn't imply same hash on Windows for paths which differ only by case #243

Merged
merged 1 commit into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
1.8.2 (TBD)
===========

- On Windows, ``py.path.local``s which differ only in case now have the same
Python hash value. Previously, such paths were considered equal but had
different hashes, which is not allowed and breaks the assumptions made by
dicts, sets and other users of hashes.

1.8.1 (2019-12-27)
==================

Expand Down
5 changes: 4 additions & 1 deletion py/_path/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ def __init__(self, path=None, expanduser=False):
self.strpath = abspath(path)

def __hash__(self):
return hash(self.strpath)
s = self.strpath
if iswin32:
s = s.lower()
return hash(s)

def __eq__(self, other):
s1 = fspath(self)
Expand Down
11 changes: 11 additions & 0 deletions testing/path/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ def test_tilde_expansion(self, monkeypatch, tmpdir):
p = py.path.local("~", expanduser=True)
assert p == os.path.expanduser("~")

@pytest.mark.skipif(
not sys.platform.startswith("win32"), reason="case insensitive only on windows"
)
def test_eq_hash_are_case_insensitive_on_windows(self):
a = py.path.local("/some/path")
b = py.path.local("/some/PATH")
assert a == b
assert hash(a) == hash(b)
assert a in {b}
assert a in {b: 'b'}

def test_eq_with_strings(self, path1):
path1 = path1.join('sampledir')
path2 = str(path1)
Expand Down