Skip to content

Commit adf2774

Browse files
authored
Do not attempt to __reduce__ when hashing classes in cache (#7484)
* Do not attempt to __reduce__ when hashing classes in cache * Add hash * Fix tests and add changelog
1 parent 54c9416 commit adf2774

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ This release fixes a number of smaller regressions related to `Tabulator` `row_c
4646
- Do not mutate layout `Children` inplace ([#7417](https://github.com/holoviz/panel/pull/7403))
4747
- Set `Tabulator` null formatter to empty string ([#7421](https://github.com/holoviz/panel/pull/7421))
4848
- Ensure Tabulator table content does not overflow ([#7425](https://github.com/holoviz/panel/pull/7425))
49-
49+
- Ensure `cache` handles hashing of classes and instances correctly ([#7478](https://github.com/holoviz/panel/issues/7478))
5050

5151
### Compatibility
5252

doc/about/releases.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ This release fixes a number of smaller regressions related to `Tabulator` `row_c
4848
- Do not mutate layout `Children` inplace ([#7417](https://github.com/holoviz/panel/pull/7403))
4949
- Set `Tabulator` null formatter to empty string ([#7421](https://github.com/holoviz/panel/pull/7421))
5050
- Ensure Tabulator table content does not overflow ([#7425](https://github.com/holoviz/panel/pull/7425), [#7431](https://github.com/holoviz/panel/pull/7431))
51+
- Ensure `cache` handles hashing of classes and instances correctly ([#7478](https://github.com/holoviz/panel/issues/7478))
5152

5253
### Compatibility
5354

panel/io/cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def _generate_hash_inner(obj):
266266
f'{obj!r} with following error: {type(e).__name__}("{e}").'
267267
) from e
268268
return output
269-
if hasattr(obj, '__reduce__'):
269+
if hasattr(obj, '__reduce__') and inspect.isclass(obj):
270270
h = hashlib.new("md5")
271271
try:
272272
reduce_data = obj.__reduce__()

panel/tests/io/test_cache.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
diskcache = None
1818
diskcache_available = pytest.mark.skipif(diskcache is None, reason="requires diskcache")
1919

20-
from panel.io.cache import (
21-
_find_hash_func, _generate_hash, cache, is_equal,
22-
)
20+
from panel.io.cache import _generate_hash, cache, is_equal
2321
from panel.io.state import set_curdoc, state
2422
from panel.tests.util import serve_and_wait
2523

@@ -28,7 +26,7 @@
2826
################
2927

3028
def hashes_equal(v1, v2):
31-
a, b = _find_hash_func(v1)(v1), _find_hash_func(v2)(v2)
29+
a, b = _generate_hash(v1), _generate_hash(v2)
3230
return a == b
3331

3432
def test_str_hash():
@@ -52,6 +50,11 @@ def test_none_hash():
5250
assert hashes_equal(None, None)
5351
assert not hashes_equal(None, False)
5452

53+
def test_object_hash():
54+
obj1, obj2 = object(), object()
55+
assert hashes_equal(obj1, obj1)
56+
assert not hashes_equal(obj1, obj2)
57+
5558
def test_bytes_hash():
5659
assert hashes_equal(b'0', b'0')
5760
assert not hashes_equal(b'0', b'1')
@@ -70,10 +73,11 @@ def test_list_hash():
7073
assert not hashes_equal([0], [1])
7174
assert not hashes_equal(['a', ['b']], ['a', ['c']])
7275

76+
def test_list_hash_recursive():
7377
# Recursion
7478
l = [0]
7579
l.append(l)
76-
assert hashes_equal(l, list(l))
80+
assert hashes_equal(list(l), list(l))
7781

7882
def test_tuple_hash():
7983
assert hashes_equal((0,), (0,))
@@ -88,10 +92,10 @@ def test_dict_hash():
8892
assert not hashes_equal({'a': 0}, {'a': 1})
8993
assert not hashes_equal({'a': {'b': 0}}, {'a': {'b': 1}})
9094

91-
# Recursion
95+
def test_dict_hash_recursive():
9296
d = {'a': {}}
9397
d['a'] = d
94-
assert hashes_equal(d, dict(d))
98+
assert hashes_equal(dict(d), dict(d))
9599

96100
def test_stringio_hash():
97101
sio1, sio2 = io.StringIO(), io.StringIO()

0 commit comments

Comments
 (0)