Jason R. Coombs avatar Jason R. Coombs committed 516ef94

Fixed some errors - the cach now nominally works on Python 2.7

Comments (0)

Files changed (1)

py31compat/cache.py

+from __future__ import absolute_import
+
+import functools
+import __builtin__
 from collections import namedtuple
 try:
 	from thread import allocate_lock as Lock
 
 from py26compat.collections import OrderedDict
 
+def _move_to_end(odict, key):
+	'''
+	Move an existing element to the end.
+
+	Raises KeyError if the element does not exist.
+	'''
+	odict[key] = odict.pop(key)
+
 # lru_cache implementation from Python 3.3dev [dfffb293f4b3]
 _CacheInfo = namedtuple("CacheInfo", "hits misses maxsize currsize")
 
     # to allow the implementation to change (including a possible C version).
 
     def decorating_function(user_function, **kwargs):
-        tuple=kwargs.get('tuple', tuple)
-        sorted=kwargs.get('sorted', sorted)
-        map=kwargs.get('map', map)
-        len=kwargs.get('len', len)
-        type=kwargs.get('type', type)
-        KeyError=kwargs.get('KeyError', KeyError)
+        tuple=kwargs.get('tuple', __builtin__.tuple)
+        sorted=kwargs.get('sorted', __builtin__.sorted)
+        map=kwargs.get('map', __builtin__.map)
+        len=kwargs.get('len', __builtin__.len)
+        type=kwargs.get('type', __builtin__.type)
+        KeyError=kwargs.get('KeyError', __builtin__.KeyError)
 
         hits = [0]
         misses = [0]
         if maxsize is None:
             cache = dict()              # simple cache without ordering or size limit
 
-            @wraps(user_function)
+            @functools.wraps(user_function)
             def wrapper(*args, **kwds):
                 #nonlocal hits, misses
                 key = args
         else:
             cache = OrderedDict()           # ordered least recent to most recent
             cache_popitem = cache.popitem
-            cache_renew = cache.move_to_end
+            # use the move_to_end method if available, otherwise fallback to
+            #  the function.
+            cache_renew = getattr(cache, 'move_to_end',
+                functools.partial(_move_to_end, cache))
 
-            @wraps(user_function)
+            @functools.wraps(user_function)
             def wrapper(*args, **kwds):
                 #nonlocal hits, misses
                 key = args
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.