This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: functools.lru_cache does not cache exceptions
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: rhettinger, webknjaz
Priority: normal Keywords:

Created on 2014-05-19 18:58 by Zero, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg218808 - (view) Author: Stephen Paul Chappell (Zero) Date: 2014-05-19 18:58
While examining the implementation for lru_cache, it came to my attention that the wrappers ignore the possibility of exceptions. Is this on purpose? If the cache is designed to reduce the overhead of running certain functions, it seems like lru_cache should handle returned values along with raised exceptions.
msg218810 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-05-19 19:44
> Is this on purpose? 

The short answer is yes.  It is a can of worms and there isn't much of a payoff.
msg302158 - (view) Author: Sviatoslav Sydorenko (webknjaz) * Date: 2017-09-14 09:26
Hi,

I've got similar need (caching exceptions). My use case is syscall, which is not going to result in something different regardless of the number of times it's called. But I'd like to be able to have exception already raised for me by lru_cache, so that I won't poke kernel with no need.

I'd suggest adding `cache_exceptions=False` key to the decorator to let user control whether it's worth enabling. This might be useful in stdlib.
History
Date User Action Args
2022-04-11 14:58:03adminsetgithub: 65736
2019-10-31 14:22:37Zerosetnosy: - Zero
2017-09-14 09:26:05webknjazsetnosy: + webknjaz
messages: + msg302158
2014-05-19 19:44:30rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg218810

resolution: not a bug
2014-05-19 18:58:59Zerocreate