From d1409f51ff40f6d0ca875c4fafc4189dbe258b37 Mon Sep 17 00:00:00 2001 From: Tim Park Date: Sat, 4 Jul 2020 20:49:36 -0700 Subject: [PATCH] Fixed #31732 -- Cached callables signatures in django.utils.inspect methods. --- django/utils/inspect.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/django/utils/inspect.py b/django/utils/inspect.py index 293cbdffa1..9a7eb21647 100644 --- a/django/utils/inspect.py +++ b/django/utils/inspect.py @@ -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