sqlmap/lib/core/decorators.py

79 lines
2.0 KiB
Python
Raw Normal View History

2019-05-08 13:47:52 +03:00
#!/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-06-04 15:44:06 +03:00
from lib.core.datatype import LRUDict
2019-01-30 01:44:58 +03:00
from lib.core.settings import MAX_CACHE_ITEMS
2019-04-30 14:20:31 +03:00
from lib.core.settings import UNICODE_ENCODING
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
2019-05-06 15:41:35 +03:00
>>> __ = cachedmethod(lambda _: _)
>>> __(1)
1
>>> __ = cachedmethod(lambda *args, **kwargs: args[0])
>>> __(2)
2
>>> __ = cachedmethod(lambda *args, **kwargs: list(kwargs.values())[0])
>>> __(foobar=3)
3
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-04-30 14:20:31 +03:00
key = int(hashlib.md5("|".join(str(_) for _ in (f, args, kwargs)).encode(UNICODE_ENCODING)).hexdigest(), 16) & 0x7fffffffffffffff
2016-05-14 15:18:34 +03:00
2019-02-10 01:18:08 +03:00
try:
2019-02-12 12:49:47 +03:00
with _lock:
result = cache[key]
2019-02-10 01:18:08 +03:00
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)
2019-05-06 15:41:35 +03:00
>>> threadData = getCurrentThreadData()
>>> original = len(threadData.valueStack)
>>> __ = stackedmethod(lambda _: threadData.valueStack.append(_))
>>> __(1)
>>> len(threadData.valueStack) == original
True
"""
@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 _