@@ -42,28 +42,34 @@ def cachedmethod(f):
4242
4343 @functools .wraps (f )
4444 def _f (* args , ** kwargs ):
45- parts = (
46- f .__module__ + "." + f .__name__ ,
47- "^" .join (repr (a ) for a in args ),
48- "^" .join ("%s=%r" % (k , kwargs [k ]) for k in sorted (kwargs ))
49- )
5045 try :
51- key = struct .unpack (">Q" , hashlib .md5 ("`" .join (parts ).encode (UNICODE_ENCODING )).digest ()[:8 ])[0 ] & 0x7fffffffffffffff
52- except (struct .error , ValueError ): # https://github.com/sqlmapproject/sqlmap/issues/4281 (NOTE: non-standard Python behavior where hexdigest returns binary value)
53- result = f (* args , ** kwargs )
54- else :
55- lock , cache = _method_locks [f ], _cache [f ]
56-
57- with lock :
58- if key in cache :
59- return cache [key ]
46+ # NOTE: fast-path
47+ if kwargs :
48+ key = hash ((f , args , tuple (map (type , args )), frozenset (kwargs .items ()))) & 0x7fffffffffffffff
49+ else :
50+ key = hash ((f , args , tuple (map (type , args )))) & 0x7fffffffffffffff
51+ except TypeError :
52+ # NOTE: failback slow-path
53+ parts = (
54+ f .__module__ + "." + f .__name__ ,
55+ "^" .join (repr (a ) for a in args ),
56+ "^" .join ("%s=%r" % (k , kwargs [k ]) for k in sorted (kwargs ))
57+ )
58+ try :
59+ key = struct .unpack (">Q" , hashlib .md5 ("`" .join (parts ).encode (UNICODE_ENCODING )).digest ()[:8 ])[0 ] & 0x7fffffffffffffff
60+ except (struct .error , ValueError ):
61+ return f (* args , ** kwargs )
62+
63+ lock , cache = _method_locks [f ], _cache [f ]
6064
61- result = f (* args , ** kwargs )
65+ with lock :
66+ if key in cache :
67+ return cache [key ]
6268
63- with lock :
64- cache [key ] = result
69+ result = f (* args , ** kwargs )
6570
66- return result
71+ with lock :
72+ cache [key ] = result
6773
6874 return result
6975
0 commit comments