1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-01-31 10:11:14 +00:00

Fix cache problem when going from Python 2 to 3

This commit is contained in:
mcarton 2015-11-15 16:55:07 +01:00
parent b55464b2ea
commit c3ea2fd0c7

View File

@ -1,18 +1,17 @@
from difflib import get_close_matches import dbm
from functools import wraps
import shelve
from warnings import warn
from decorator import decorator
from contextlib import closing
import os import os
import pickle import pickle
import re
from inspect import getargspec
from pathlib import Path
import pkg_resources import pkg_resources
import re
import shelve
from .conf import settings from .conf import settings
from contextlib import closing
from decorator import decorator
from difflib import get_close_matches
from functools import wraps
from inspect import getargspec
from pathlib import Path
from warnings import warn
DEVNULL = open(os.devnull, 'w') DEVNULL = open(os.devnull, 'w')
@ -210,13 +209,24 @@ def cache(*depends_on):
etag = '.'.join(_get_mtime(name) for name in depends_on) etag = '.'.join(_get_mtime(name) for name in depends_on)
cache_path = _get_cache_path() cache_path = _get_cache_path()
with closing(shelve.open(cache_path)) as db: try:
if db.get(key, {}).get('etag') == etag: with closing(shelve.open(cache_path)) as db:
return db[key]['value'] if db.get(key, {}).get('etag') == etag:
else: return db[key]['value']
else:
value = fn(*args, **kwargs)
db[key] = {'etag': etag, 'value': value}
return value
except dbm.error:
# Caused when going from Python 2 to Python 3
warn("Removing possibly out-dated cache")
os.remove(cache_path)
with closing(shelve.open(cache_path)) as db:
value = fn(*args, **kwargs) value = fn(*args, **kwargs)
db[key] = {'etag': etag, 'value': value} db[key] = {'etag': etag, 'value': value}
return value return value
return _cache return _cache
cache.disabled = False cache.disabled = False