Fixed #31732 -- Cached callables signatures in django.utils.inspect methods.

This commit is contained in:
Tim Park 2020-07-04 20:49:36 -07:00 committed by Mariusz Felisiak
parent 512da9d585
commit d1409f51ff
1 changed files with 12 additions and 6 deletions

View File

@ -1,8 +1,14 @@
import functools
import inspect
@functools.lru_cache(maxsize=512)
def _get_signature(func):
return inspect.signature(func)
def get_func_args(func):
sig = inspect.signature(func)
sig = _get_signature(func)
return [
arg_name for arg_name, param in sig.parameters.items()
if param.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD
@ -15,7 +21,7 @@ def get_func_full_args(func):
does not have a default value, omit it in the tuple. Arguments such as
*args and **kwargs are also included.
"""
sig = inspect.signature(func)
sig = _get_signature(func)
args = []
for arg_name, param in sig.parameters.items():
name = arg_name
@ -35,7 +41,7 @@ def get_func_full_args(func):
def func_accepts_kwargs(func):
return any(
p for p in inspect.signature(func).parameters.values()
p for p in _get_signature(func).parameters.values()
if p.kind == p.VAR_KEYWORD
)
@ -45,7 +51,7 @@ def func_accepts_var_args(func):
Return True if function 'func' accepts positional arguments *args.
"""
return any(
p for p in inspect.signature(func).parameters.values()
p for p in _get_signature(func).parameters.values()
if p.kind == p.VAR_POSITIONAL
)
@ -53,11 +59,11 @@ def func_accepts_var_args(func):
def method_has_no_args(meth):
"""Return True if a method only accepts 'self'."""
count = len([
p for p in inspect.signature(meth).parameters.values()
p for p in _get_signature(meth).parameters.values()
if p.kind == p.POSITIONAL_OR_KEYWORD
])
return count == 0 if inspect.ismethod(meth) else count == 1
def func_supports_parameter(func, parameter):
return parameter in inspect.signature(func).parameters
return parameter in _get_signature(func).parameters