Skip to content

Commit c8ccc31

Browse files
committed
Simplifying cachedmethod
1 parent 914b449 commit c8ccc31

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

data/txt/sha256sums.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ a6397b10de7ae7c56ed6b0fa3b3c58eb7a9dbede61bf93d786e73258175c981e lib/core/compa
172172
a9997e97ebe88e0bf7efcf21e878bc5f62c72348e5aba18f64d6861390a4dcf2 lib/core/convert.py
173173
c03dc585f89642cfd81b087ac2723e3e1bb3bfa8c60e6f5fe58ef3b0113ebfe6 lib/core/data.py
174174
ca06a0e9d66a58e74ef994d53f9b3cd2ebaed98735bbab99854054235a8083d6 lib/core/datatype.py
175-
e18c0c2c5a57924a623792a48bfd36e98d9bc085f6db61a95fc0dc8a3bcedc0c lib/core/decorators.py
175+
70fb2528e580b22564899595b0dff6b1bc257c6a99d2022ce3996a3d04e68e4e lib/core/decorators.py
176176
147823c37596bd6a56d677697781f34b8d1d1671d5a2518fbc9468d623c6d07d lib/core/defaults.py
177177
6b366f897e66b9df39df2ee45fef77d46efb7a2d4e294440d3aa7dc1b2f4cedf lib/core/dicts.py
178178
a033f92d136c707a25927c2383125ddb004d4283db62c004dcd67c3fc242bb1c lib/core/dump.py
@@ -188,7 +188,7 @@ c1cb56f2a43e9f2f6b25d5f3d504e856ea21df6fc14af5e37b1000feef2bdb5a lib/core/optio
188188
48797d6c34dd9bb8a53f7f3794c85f4288d82a9a1d6be7fcf317d388cb20d4b3 lib/core/replication.py
189189
0b8c38a01bb01f843d94a6c5f2075ee47520d0c4aa799cecea9c3e2c5a4a23a6 lib/core/revision.py
190190
888daba83fd4a34e9503fe21f01fef4cc730e5cde871b1d40e15d4cbc847d56c lib/core/session.py
191-
610650be1f560c70944d801a355330c9315ab218e1ded611a054d907d6c05a87 lib/core/settings.py
191+
1418691b5449412e60c693b6afc2f12b00051c1e280d2261762a36f094e0da66 lib/core/settings.py
192192
cd5a66deee8963ba8e7e9af3dd36eb5e8127d4d68698811c29e789655f507f82 lib/core/shell.py
193193
bcb5d8090d5e3e0ef2a586ba09ba80eef0c6d51feb0f611ed25299fbb254f725 lib/core/subprocessng.py
194194
d35650179816193164a5f177102f18379dfbe6bb6d40fbb67b78d907b41c8038 lib/core/target.py

lib/core/decorators.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66
"""
77

88
import functools
9-
import hashlib
10-
import struct
119
import threading
1210

1311
from lib.core.datatype import LRUDict
1412
from lib.core.settings import MAX_CACHE_ITEMS
15-
from lib.core.settings import UNICODE_ENCODING
1613
from lib.core.threads import getCurrentThreadData
1714

1815
_cache = {}
@@ -40,31 +37,37 @@ def cachedmethod(f):
4037
_cache[f] = LRUDict(capacity=MAX_CACHE_ITEMS)
4138
_method_locks[f] = threading.RLock()
4239

40+
def _freeze(val):
41+
if isinstance(val, (list, set, tuple)):
42+
return tuple(_freeze(x) for x in val)
43+
if isinstance(val, dict):
44+
return tuple(sorted((k, _freeze(v)) for k, v in val.items()))
45+
return val
46+
4347
@functools.wraps(f)
4448
def _f(*args, **kwargs):
49+
lock, cache = _method_locks[f], _cache[f]
50+
4551
try:
46-
# NOTE: fast-path
4752
if kwargs:
48-
key = hash((f, args, tuple(map(type, args)), frozenset(kwargs.items()))) & 0x7fffffffffffffff
53+
key = (args, frozenset(kwargs.items()))
4954
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)
55+
key = args
6256

63-
lock, cache = _method_locks[f], _cache[f]
57+
with lock:
58+
if key in cache:
59+
return cache[key]
6460

65-
with lock:
66-
if key in cache:
67-
return cache[key]
61+
except TypeError:
62+
# Note: fallback (slowpath(
63+
if kwargs:
64+
key = (_freeze(args), _freeze(kwargs))
65+
else:
66+
key = _freeze(args)
67+
68+
with lock:
69+
if key in cache:
70+
return cache[key]
6871

6972
result = f(*args, **kwargs)
7073

lib/core/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from thirdparty import six
2121

2222
# sqlmap version (<major>.<minor>.<month>.<monthly commit>)
23-
VERSION = "1.10.1.84"
23+
VERSION = "1.10.1.85"
2424
TYPE = "dev" if VERSION.count('.') > 2 and VERSION.split('.')[-1] != '0' else "stable"
2525
TYPE_COLORS = {"dev": 33, "stable": 90, "pip": 34}
2626
VERSION_STRING = "sqlmap/%s#%s" % ('.'.join(VERSION.split('.')[:-1]) if VERSION.count('.') > 2 and VERSION.split('.')[-1] == '0' else VERSION, TYPE)

0 commit comments

Comments
 (0)