mirror of https://github.com/django/django.git
Fixed #31732 -- Cached callables signatures in django.utils.inspect methods.
This commit is contained in:
parent
512da9d585
commit
d1409f51ff
|
@ -1,8 +1,14 @@
|
||||||
|
import functools
|
||||||
import inspect
|
import inspect
|
||||||
|
|
||||||
|
|
||||||
|
@functools.lru_cache(maxsize=512)
|
||||||
|
def _get_signature(func):
|
||||||
|
return inspect.signature(func)
|
||||||
|
|
||||||
|
|
||||||
def get_func_args(func):
|
def get_func_args(func):
|
||||||
sig = inspect.signature(func)
|
sig = _get_signature(func)
|
||||||
return [
|
return [
|
||||||
arg_name for arg_name, param in sig.parameters.items()
|
arg_name for arg_name, param in sig.parameters.items()
|
||||||
if param.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD
|
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
|
does not have a default value, omit it in the tuple. Arguments such as
|
||||||
*args and **kwargs are also included.
|
*args and **kwargs are also included.
|
||||||
"""
|
"""
|
||||||
sig = inspect.signature(func)
|
sig = _get_signature(func)
|
||||||
args = []
|
args = []
|
||||||
for arg_name, param in sig.parameters.items():
|
for arg_name, param in sig.parameters.items():
|
||||||
name = arg_name
|
name = arg_name
|
||||||
|
@ -35,7 +41,7 @@ def get_func_full_args(func):
|
||||||
|
|
||||||
def func_accepts_kwargs(func):
|
def func_accepts_kwargs(func):
|
||||||
return any(
|
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
|
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 True if function 'func' accepts positional arguments *args.
|
||||||
"""
|
"""
|
||||||
return any(
|
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
|
if p.kind == p.VAR_POSITIONAL
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -53,11 +59,11 @@ def func_accepts_var_args(func):
|
||||||
def method_has_no_args(meth):
|
def method_has_no_args(meth):
|
||||||
"""Return True if a method only accepts 'self'."""
|
"""Return True if a method only accepts 'self'."""
|
||||||
count = len([
|
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
|
if p.kind == p.POSITIONAL_OR_KEYWORD
|
||||||
])
|
])
|
||||||
return count == 0 if inspect.ismethod(meth) else count == 1
|
return count == 0 if inspect.ismethod(meth) else count == 1
|
||||||
|
|
||||||
|
|
||||||
def func_supports_parameter(func, parameter):
|
def func_supports_parameter(func, parameter):
|
||||||
return parameter in inspect.signature(func).parameters
|
return parameter in _get_signature(func).parameters
|
||||||
|
|
Loading…
Reference in New Issue