Skip to content

Commit

Permalink
Merge pull request #243 from bluetech/win-path-hash
Browse files Browse the repository at this point in the history
path.local: fix == doesn't imply same hash on Windows for paths which differ only by case
  • Loading branch information
nicoddemus authored Jun 15, 2020
2 parents f7b20d2 + cd245f5 commit 2f51e6f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
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

0 comments on commit 2f51e6f

Please sign in to comment.