Renamed *args, **kwargs: bind correctly on Py2.7

Use search and sort for adding

Change-Id: Ib040bbc9883d50e9ecc471321c4f246ab61b4876
This commit is contained in:
Alexey Stepanov 2016-10-07 13:03:21 +03:00
parent 4e324fdd93
commit e97ef2154a
2 changed files with 52 additions and 4 deletions

View File

@ -166,10 +166,21 @@ def _getcallargs(func, *positional, **named):
if six.PY2:
# args and kwargs is not bound in py27
# Note: py27 inspect is not unicode
if 'args' in orig_args:
arguments[b'args'] = orig_args['args']
if 'kwargs' in orig_args:
arguments[b'kwargs'] = orig_args['kwargs']
missed = (
(key, val)
for key, val in orig_args.items()
if key not in arguments)
args, kwargs = (), ()
for record in missed:
if isinstance(record[1], (list, tuple)):
args = record
elif isinstance(record[1], dict):
kwargs = record
if args:
arguments[args[0]] = args[1]
if kwargs:
arguments[kwargs[0]] = kwargs[1]
return arguments
sig = inspect.signature(func).bind(*positional, **named)
sig.apply_defaults() # after bind we doesn't have defaults

View File

@ -418,6 +418,43 @@ class TestLogWrap(unittest.TestCase):
),
))
def test_renamed_args_kwargs(self, logger):
arg = 'arg'
targs = ['string1', 'string2']
tkwargs = {'key': 'tkwargs'}
@decorators.logwrap
def func(arg, *positional, **named):
return arg, tuple(positional), named
result = func(arg, *targs, **tkwargs)
self.assertEqual(result, (arg, tuple(targs), tkwargs))
# raise ValueError(logger.mock_calls)
logger.assert_has_calls((
mock.call.log(
level=logging.DEBUG,
msg="Calling: \n'func'("
"\n 'arg'={arg},"
"\n 'positional'={args},"
"\n 'named'={kwargs},\n)".format(
arg=decorators.pretty_repr(
arg,
indent=8, no_indent_start=True),
args=decorators.pretty_repr(
tuple(targs),
indent=8, no_indent_start=True),
kwargs=decorators.pretty_repr(
tkwargs,
indent=8, no_indent_start=True)
)
),
mock.call.log(
level=logging.DEBUG,
msg="Done: 'func' with result:\n{}".format(
decorators.pretty_repr(result))
),
))
def test_negative(self, logger):
@decorators.logwrap
def func():