MCPcopy
hub / github.com/cherrypy/cherrypy / test_callable_spec

Function test_callable_spec

cherrypy/_cpdispatch.py:66–199  ·  view source on GitHub ↗

Inspect callable and test to see if the given args are suitable for it. When an error occurs during the handler's invoking stage there are 2 erroneous cases: 1. Too many parameters passed to a function which doesn't define one of *args or **kwargs. 2. Too little parameters

(callable, callable_args, callable_kwargs)

Source from the content-addressed store, hash-verified

64
65
66def test_callable_spec(callable, callable_args, callable_kwargs):
67 """Inspect callable and test to see if the given args are suitable for it.
68
69 When an error occurs during the handler's invoking stage there are 2
70 erroneous cases:
71 1. Too many parameters passed to a function which doesn't define
72 one of *args or **kwargs.
73 2. Too little parameters are passed to the function.
74
75 There are 3 sources of parameters to a cherrypy handler.
76 1. query string parameters are passed as keyword parameters to the
77 handler.
78 2. body parameters are also passed as keyword parameters.
79 3. when partial matching occurs, the final path atoms are passed as
80 positional args.
81 Both the query string and path atoms are part of the URI. If they are
82 incorrect, then a 404 Not Found should be raised. Conversely the body
83 parameters are part of the request; if they are invalid a 400 Bad Request.
84 """
85 show_mismatched_params = getattr(
86 cherrypy.serving.request, 'show_mismatched_params', False)
87 try:
88 (args, varargs, varkw, defaults) = getargspec(callable)
89 except TypeError:
90 if isinstance(callable, object) and hasattr(callable, '__call__'):
91 (args, varargs, varkw,
92 defaults) = getargspec(callable.__call__)
93 else:
94 # If it wasn't one of our own types, re-raise
95 # the original error
96 raise
97
98 if args and (
99 # For callable objects, which have a __call__(self) method
100 hasattr(callable, '__call__') or
101 # For normal methods
102 inspect.ismethod(callable)
103 ):
104 # Strip 'self'
105 args = args[1:]
106
107 arg_usage = dict([(arg, 0,) for arg in args])
108 vararg_usage = 0
109 varkw_usage = 0
110 extra_kwargs = set()
111
112 for i, value in enumerate(callable_args):
113 try:
114 arg_usage[args[i]] += 1
115 except IndexError:
116 vararg_usage += 1
117
118 for key in callable_kwargs.keys():
119 try:
120 arg_usage[key] += 1
121 except KeyError:
122 varkw_usage += 1
123 extra_kwargs.add(key)

Callers 1

__call__Method · 0.85

Calls 4

getargspecFunction · 0.85
keysMethod · 0.80
itemsMethod · 0.80
joinMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…