mirror of
https://github.com/nvbn/thefuck.git
synced 2025-01-31 10:11:14 +00:00
Fix misinterpretation of the disabled flag
The old implementation was misinterpretating the disabled flag and effectively applying memoization even when explicitly disabled. The 'or' operator is a short-circuit one; namely, it evaluates the second argument if and only if the first is False. Therefore the following conditions caused unexpected side effects: - memoize.disabled = True, key not yet memoized Having disabled the memoize function wrapper, the client expects that no memoization happens. Instead the execution enters the if clause and store the value into the 'memo' dictionary - memoize.disabled = True, key memoized Having disabled the memoize function wrapper, the client expects that no memoization happens and the function will be evaluated anyway, whether or not its return value had already been stored in the 'memo' dictionary by a previous call. On the contrary, the last statement of wrapper() access the value stored by the last function execution. This commit attempts to improve the function readability too.
This commit is contained in:
parent
89f868c115
commit
7f777213c5
@ -23,11 +23,16 @@ def memoize(fn):
|
|||||||
|
|
||||||
@wraps(fn)
|
@wraps(fn)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
key = pickle.dumps((args, kwargs))
|
if not memoize.disabled:
|
||||||
if key not in memo or memoize.disabled:
|
key = pickle.dumps((args, kwargs))
|
||||||
memo[key] = fn(*args, **kwargs)
|
if key not in memo:
|
||||||
|
memo[key] = fn(*args, **kwargs)
|
||||||
|
value = memo[key]
|
||||||
|
else:
|
||||||
|
# Memoize is disabled, call the function
|
||||||
|
value = fn(*args, **kwargs)
|
||||||
|
|
||||||
return memo[key]
|
return value
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
memoize.disabled = False
|
memoize.disabled = False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user