Skip to content

Commit

Permalink
Preventing catching KeyError exceptions from callback function
Browse files Browse the repository at this point in the history
  • Loading branch information
awolverp committed Feb 1, 2025
1 parent 40a7754 commit cc1f1c7
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions cachebox/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,17 @@ def _wrapped(*args, **kwds):
# try to get result from cache
try:
result = cache[key]
except KeyError:
pass
else:
# A NOTE FOR ME: we don't want to catch KeyError exceptions from `callback`
# so don't wrap it with try except
hits += 1

if callback is not None:
callback(EVENT_HIT, key, result)

return _copy_if_need(result, level=copy_level)
except KeyError:
pass

with locks[key]:
if exceptions.get(key, None) is not None:
Expand Down Expand Up @@ -327,6 +330,11 @@ async def _wrapped(*args, **kwds):
# try to get result from cache
try:
result = cache[key]
except KeyError:
pass
else:
# A NOTE FOR ME: we don't want to catch KeyError exceptions from `callback`
# so don't wrap it with try except
hits += 1

if callback is not None:
Expand All @@ -335,8 +343,6 @@ async def _wrapped(*args, **kwds):
await awaitable

return _copy_if_need(result, level=copy_level)
except KeyError:
pass

async with locks[key]:
if exceptions.get(key, None) is not None:
Expand Down

0 comments on commit cc1f1c7

Please sign in to comment.