(original) (raw)
changeset: 103838:1dbe3addba28 parent: 103835:e9c37e3efcb2 parent: 103837:03257f04ee9f user: Yury Selivanov yury@magic.io date: Thu Sep 15 16:01:56 2016 -0400 files: Misc/NEWS description: Merge 3.6 (issue #26654) diff -r e9c37e3efcb2 -r 1dbe3addba28 Lib/asyncio/coroutines.py --- a/Lib/asyncio/coroutines.py Thu Sep 15 15:46:40 2016 -0400 +++ b/Lib/asyncio/coroutines.py Thu Sep 15 16:01:56 2016 -0400 @@ -271,7 +271,7 @@ func = coro if coro_name is None: - coro_name = events._format_callback(func, ()) + coro_name = events._format_callback(func, (), {}) try: coro_code = coro.gi_code diff -r e9c37e3efcb2 -r 1dbe3addba28 Lib/asyncio/events.py --- a/Lib/asyncio/events.py Thu Sep 15 15:46:40 2016 -0400 +++ b/Lib/asyncio/events.py Thu Sep 15 16:01:56 2016 -0400 @@ -35,23 +35,25 @@ return None -def _format_args(args): - """Format function arguments. +def _format_args_and_kwargs(args, kwargs): + """Format function arguments and keyword arguments. Special case for a single parameter: ('hello',) is formatted as ('hello'). """ # use reprlib to limit the length of the output - args_repr = reprlib.repr(args) - if len(args) == 1 and args_repr.endswith(',)'): - args_repr = args_repr[:-2] + ')' - return args_repr + items = [] + if args: + items.extend(reprlib.repr(arg) for arg in args) + if kwargs: + items.extend('{}={}'.format(k, reprlib.repr(v)) + for k, v in kwargs.items()) + return '(' + ', '.join(items) + ')' -def _format_callback(func, args, suffix=''): +def _format_callback(func, args, kwargs, suffix=''): if isinstance(func, functools.partial): - if args is not None: - suffix = _format_args(args) + suffix - return _format_callback(func.func, func.args, suffix) + suffix = _format_args_and_kwargs(args, kwargs) + suffix + return _format_callback(func.func, func.args, func.keywords, suffix) if hasattr(func, '__qualname__'): func_repr = getattr(func, '__qualname__') @@ -60,14 +62,13 @@ else: func_repr = repr(func) - if args is not None: - func_repr += _format_args(args) + func_repr += _format_args_and_kwargs(args, kwargs) if suffix: func_repr += suffix return func_repr def _format_callback_source(func, args): - func_repr = _format_callback(func, args) + func_repr = _format_callback(func, args, None) source = _get_function_source(func) if source: func_repr += ' at %s:%s' % source diff -r e9c37e3efcb2 -r 1dbe3addba28 Lib/test/test_asyncio/test_events.py --- a/Lib/test/test_asyncio/test_events.py Thu Sep 15 15:46:40 2016 -0400 +++ b/Lib/test/test_asyncio/test_events.py Thu Sep 15 16:01:56 2016 -0400 @@ -2224,7 +2224,7 @@ return asyncio.SelectorEventLoop(selectors.SelectSelector()) -def noop(*args): +def noop(*args, **kwargs): pass @@ -2305,6 +2305,13 @@ % (re.escape(filename), lineno)) self.assertRegex(repr(h), regex) + # partial function with keyword args + cb = functools.partial(noop, x=1) + h = asyncio.Handle(cb, (2, 3), self.loop) + regex = (r'^$' + % (re.escape(filename), lineno)) + self.assertRegex(repr(h), regex) + # partial method if sys.version_info >= (3, 4): method = HandleTests.test_handle_repr diff -r e9c37e3efcb2 -r 1dbe3addba28 Misc/NEWS --- a/Misc/NEWS Thu Sep 15 15:46:40 2016 -0400 +++ b/Misc/NEWS Thu Sep 15 16:01:56 2016 -0400 @@ -435,6 +435,9 @@ - Issue #28174: Handle when SO_REUSEPORT isn't properly supported. Patch by Seth Michael Larson. +- Issue #26654: Inspect functools.partial in asyncio.Handle.__repr__. + Patch by iceboy. + IDLE ----/yury@magic.io