sqlmap/lib/core/decorators.py

60 lines
1.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python
"""
2019-01-05 23:38:52 +03:00
Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/)
2017-10-11 15:50:46 +03:00
See the file 'LICENSE' for copying permission
"""
import functools
2017-12-25 01:54:43 +03:00
import hashlib
2019-01-30 01:44:58 +03:00
import threading
2017-12-25 01:54:43 +03:00
2019-01-30 01:44:58 +03:00
from lib.core.settings import MAX_CACHE_ITEMS
from lib.core.datatype import LRUDict
from lib.core.threads import getCurrentThreadData
2019-01-30 01:44:58 +03:00
_lock = threading.Lock()
def cachedmethod(f, cache=LRUDict(capacity=MAX_CACHE_ITEMS)):
"""
Method with a cached content
Reference: http://code.activestate.com/recipes/325205-cache-decorator-in-python-24/
"""
2013-01-30 13:38:11 +04:00
@functools.wraps(f)
2012-07-30 12:06:14 +04:00
def _(*args, **kwargs):
2019-02-10 01:18:08 +03:00
key = int(hashlib.md5("|".join(str(_) for _ in (f, args, kwargs))).hexdigest(), 16) & 0x7fffffffffffffff
2016-05-14 15:18:34 +03:00
2019-02-10 01:18:08 +03:00
try:
result = cache[key]
except KeyError:
result = f(*args, **kwargs)
with _lock:
cache[key] = result
return result
2013-01-30 13:38:11 +04:00
2012-07-30 12:06:14 +04:00
return _
def stackedmethod(f):
"""
Method using pushValue/popValue functions (fallback function for stack realignment)
"""
@functools.wraps(f)
def _(*args, **kwargs):
threadData = getCurrentThreadData()
originalLevel = len(threadData.valueStack)
try:
result = f(*args, **kwargs)
finally:
if len(threadData.valueStack) > originalLevel:
threadData.valueStack = threadData.valueStack[:originalLevel]
return result
2018-06-10 00:38:00 +03:00
return _