Skip to content

Commit f3e5d51

Browse files
committed
Better support for unicode cache entries
1 parent 69cdf75 commit f3e5d51

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

src/pip/_internal/cache.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# mypy: strict-optional=False
66

77
import hashlib
8+
import json
89
import logging
910
import os
1011
import sys
@@ -29,14 +30,9 @@
2930

3031
def _hash_dict(d):
3132
# type: (Dict[str, str]) -> str
32-
"""Return a sha224 of a dictionary where keys and values are strings."""
33-
h = hashlib.new('sha224')
34-
for k in sorted(d.keys()):
35-
h.update(k.encode())
36-
h.update("=".encode())
37-
h.update(d[k].encode())
38-
h.update(b"\0")
39-
return h.hexdigest()
33+
"""Return a stable sha224 of a dictionary."""
34+
s = json.dumps(d, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
35+
return hashlib.sha224(s.encode("ascii")).hexdigest()
4036

4137

4238
class Cache(object):

tests/unit/test_cache.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ def test_wheel_name_filter(tmpdir):
4646

4747
def test_cache_hash():
4848
h = _hash_dict({"url": "https://g.c/o/r"})
49-
assert h == "c7d60d08b1079254d236e983501fa26c016d58d16010725b27ed0af2"
49+
assert h == "72aa79d3315c181d2cc23239d7109a782de663b6f89982624d8c1e86"
5050
h = _hash_dict({"url": "https://g.c/o/r", "subdirectory": "sd"})
51-
assert h == "9cba35d4ccf04b7cde751b44db347fd0f21fa47d1276e32f9d47864c"
51+
assert h == "8b13391b6791bf7f3edeabb41ea4698d21bcbdbba7f9c7dc9339750d"
52+
h = _hash_dict({"subdirectory": u"/\xe9e"})
53+
assert h == "f83b32dfa27a426dec08c21bf006065dd003d0aac78e7fc493d9014d"
5254

5355

5456
def test_get_path_for_link_legacy(tmpdir):

0 commit comments

Comments
 (0)