Revert "Use inspect.signature() if available"
This change introduces behavior that's not compatible
with real world usage of mox3 in the nova project, and
breaks huge amounts of the unit tests under python3.
Closes-Bug: #1657757
This reverts commit 4a85ff0ec4
.
Change-Id: Iacb0d9307af4602a9e4f5725504f98e196cd10fc
This commit is contained in:
parent
4a85ff0ec4
commit
475ad79771
75
mox3/mox.py
75
mox3/mox.py
|
@ -905,7 +905,15 @@ class MethodSignatureChecker(object):
|
||||||
possible. Some methods and functions like built-ins
|
possible. Some methods and functions like built-ins
|
||||||
can't be inspected.
|
can't be inspected.
|
||||||
"""
|
"""
|
||||||
|
try:
|
||||||
|
self._args, varargs, varkw, defaults = inspect.getargspec(method)
|
||||||
|
except TypeError:
|
||||||
|
raise ValueError('Could not get argument specification for %r'
|
||||||
|
% (method,))
|
||||||
|
if (inspect.ismethod(method) or class_to_bind or (
|
||||||
|
hasattr(self, '_args') and len(self._args) > 0
|
||||||
|
and self._args[0] == 'self')):
|
||||||
|
self._args = self._args[1:] # Skip 'self'.
|
||||||
self._method = method
|
self._method = method
|
||||||
self._instance = None # May contain the instance this is bound to.
|
self._instance = None # May contain the instance this is bound to.
|
||||||
self._instance = getattr(method, "__self__", None)
|
self._instance = getattr(method, "__self__", None)
|
||||||
|
@ -917,65 +925,14 @@ class MethodSignatureChecker(object):
|
||||||
self._bounded_to = class_to_bind or getattr(method, "im_class",
|
self._bounded_to = class_to_bind or getattr(method, "im_class",
|
||||||
None)
|
None)
|
||||||
|
|
||||||
# inspect.getargspec() is deprecated since Python 3.0, prefer
|
self._has_varargs = varargs is not None
|
||||||
# inspect.signature() (available since Python 3.3) which supports
|
self._has_varkw = varkw is not None
|
||||||
# also Python 3 keyword-only arguments.
|
if defaults is None:
|
||||||
if hasattr(inspect, 'signature'):
|
self._required_args = self._args
|
||||||
try:
|
self._default_args = []
|
||||||
signature = inspect.signature(method)
|
|
||||||
params = list(signature.parameters.values())
|
|
||||||
except TypeError:
|
|
||||||
raise ValueError('Could not get signature for %r'
|
|
||||||
% (method,))
|
|
||||||
|
|
||||||
Parameter = inspect.Parameter
|
|
||||||
self._has_varargs = any(param.kind == Parameter.VAR_POSITIONAL
|
|
||||||
for param in params)
|
|
||||||
self._has_varkw = any(param.kind == Parameter.VAR_KEYWORD
|
|
||||||
for param in params)
|
|
||||||
|
|
||||||
if (inspect.ismethod(method) or class_to_bind
|
|
||||||
or (params and params[0].name == 'self')):
|
|
||||||
params = params[1:]
|
|
||||||
|
|
||||||
# Truncate params at the first *args or **kwargs parameter
|
|
||||||
for index, param in enumerate(params):
|
|
||||||
if param.kind in (Parameter.VAR_POSITIONAL,
|
|
||||||
Parameter.VAR_KEYWORD):
|
|
||||||
del params[index:]
|
|
||||||
break
|
|
||||||
|
|
||||||
required = 0
|
|
||||||
for param in params:
|
|
||||||
if param.default is not Parameter.empty:
|
|
||||||
break
|
|
||||||
required += 1
|
|
||||||
|
|
||||||
self._args = [param.name for param in params]
|
|
||||||
self._required_args = self._args[:required]
|
|
||||||
self._default_args = self._args[required:]
|
|
||||||
else:
|
else:
|
||||||
try:
|
self._required_args = self._args[:-len(defaults)]
|
||||||
argspec = inspect.getargspec(method)
|
self._default_args = self._args[-len(defaults):]
|
||||||
except TypeError:
|
|
||||||
raise ValueError('Could not get argument specification for %r'
|
|
||||||
% (method,))
|
|
||||||
|
|
||||||
args = argspec.args
|
|
||||||
if (inspect.ismethod(method) or class_to_bind or (
|
|
||||||
args and args[0] == 'self')):
|
|
||||||
args = args[1:] # Skip 'self'.
|
|
||||||
|
|
||||||
self._has_varargs = (argspec.varargs is not None)
|
|
||||||
self._has_varkw = (argspec.keywords is not None)
|
|
||||||
self._args = args
|
|
||||||
if argspec.defaults:
|
|
||||||
pos = -len(argspec.defaults)
|
|
||||||
self._required_args = args[:pos]
|
|
||||||
self._default_args = args[pos:]
|
|
||||||
else:
|
|
||||||
self._required_args = args
|
|
||||||
self._default_args = []
|
|
||||||
|
|
||||||
def _RecordArgumentGiven(self, arg_name, arg_status):
|
def _RecordArgumentGiven(self, arg_name, arg_status):
|
||||||
"""Mark an argument as being given.
|
"""Mark an argument as being given.
|
||||||
|
|
Loading…
Reference in New Issue