[issue13177] Avoid chained exceptions in lru_cache

Ezio Melotti report at bugs.python.org
Fri Oct 14 15:27:22 CEST 2011


Ezio Melotti <ezio.melotti at gmail.com> added the comment:

Here's an example (copied from msg142063) of what the traceback is without the patch:
>>> from functools import lru_cache
>>> @lru_cache()
... def func(arg): raise ValueError()
... 
>>> func(3)
Traceback (most recent call last):
  File "/home/wolf/dev/py/3.2/Lib/functools.py", line 176, in wrapper
    result = cache[key]
KeyError: (3,)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/wolf/dev/py/3.2/Lib/functools.py", line 180, in wrapper
    result = user_function(*args, **kwds)
  File "<stdin>", line 2, in func
ValueError

The patch gets rid of the first traceback (the one before "During handling...").
I should also mention that my second point might not be valid if the cache hits are mostly successful.  I haven't done any specific benchmark, and I don't know how fast is 'key in dict' compared to raising an exception.  If it's e.g. 10 times faster, it should make lru_cache faster when the hits:miss ratio is lower than 10:1.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue13177>
_______________________________________


More information about the Python-bugs-list mailing list