Skip to content

Commit

Permalink
Merge pull request #1070 from girder/nostyle-copy
Browse files Browse the repository at this point in the history
The cache could reuse a class inappropriately
  • Loading branch information
manthey authored Feb 27, 2023
2 parents f5e10e9 + 31a0f47 commit 93b1a34
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
### Improvements
- Allow ICC correction to specify intent ([#1066](../../pull/1066))

### Bug Fixes
- The cache could reuse a class inappropriately ([#1070](../../pull/1070))

## 1.20.1

### Bug Fixes
Expand Down
7 changes: 3 additions & 4 deletions large_image/cache_util/cache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import copy
import functools
import threading

Expand Down Expand Up @@ -113,7 +112,6 @@ def __new__(metacls, name, bases, namespace, **kwargs): # noqa - N804
# Get metaclass parameters by finding and removing them from the class
# namespace (necessary for Python 2), or preferentially as metaclass
# arguments (only in Python 3).

cacheName = namespace.get('cacheName', None)
cacheName = kwargs.get('cacheName', cacheName)

Expand Down Expand Up @@ -192,12 +190,13 @@ def __call__(cls, *args, **kwargs): # noqa - N805
return result
except KeyError:
pass
# This conditionally copies a non-styled class and add a style.
# This conditionally copies a non-styled class and adds a style.
if kwargs.get('style') and hasattr(cls, '_setStyle'):
subkwargs = kwargs.copy()
subkwargs.pop('style')
subresult = cls(*args, **subkwargs)
result = copy.copy(subresult)
result = subresult.__class__.__new__(subresult.__class__)
result.__dict__ = subresult.__dict__.copy()
result._setStyle(kwargs['style'])
result._classkey = key
with cacheLock:
Expand Down
3 changes: 3 additions & 0 deletions test/test_cache_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def testCacheSourceStyle():
ts1 = large_image.open(imagePath)
ts2 = large_image.open(imagePath, style={'max': 128})
ts3 = large_image.open(imagePath, style={'max': 160})
assert id(ts1) != id(ts2)
assert id(ts2) != id(ts3)
tile1 = ts1.getTile(0, 0, 4)
assert ts1.getTile(0, 0, 4) is not None
assert ts2.getTile(0, 0, 4) is not None
Expand All @@ -33,6 +35,7 @@ def testCacheSourceStyleFirst():
imagePath = datastore.fetch('sample_image.ptif')
ts2 = large_image.open(imagePath, style={'max': 128})
ts1 = large_image.open(imagePath)
assert id(ts1) != id(ts2)
tile1 = ts1.getTile(0, 0, 4)
assert ts1.getTile(0, 0, 4) is not None
assert ts2.getTile(0, 0, 4) is not None
Expand Down

0 comments on commit 93b1a34

Please sign in to comment.