summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2016-09-15 15:58:15 -0400
committerYury Selivanov <yury@magic.io>2016-09-15 15:58:15 -0400
commit1cc748b5a2c136e2e065ccd74b37b57dc95e97e1 (patch)
tree276f1ffa5f5e31d6158f30f9b661a0150814bdfe
parent1147108262eb1c9188159149c398aa64b25150de (diff)
downloadcpython-1cc748b5a2c136e2e065ccd74b37b57dc95e97e1.tar.gz
Issue #26654: Inspect functools.partial in asyncio.Handle.__repr__.
Patch by iceboy.
-rw-r--r--Lib/asyncio/coroutines.py2
-rw-r--r--Lib/asyncio/events.py27
-rw-r--r--Lib/test/test_asyncio/test_events.py9
-rw-r--r--Misc/NEWS3
4 files changed, 26 insertions, 15 deletions
diff --git a/Lib/asyncio/coroutines.py b/Lib/asyncio/coroutines.py
index 72ffb44e95..e013d64edf 100644
--- a/Lib/asyncio/coroutines.py
+++ b/Lib/asyncio/coroutines.py
@@ -271,7 +271,7 @@ def _format_coroutine(coro):
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 --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py
index cc9a986b99..3a33646df8 100644
--- a/Lib/asyncio/events.py
+++ b/Lib/asyncio/events.py
@@ -35,23 +35,25 @@ def _get_function_source(func):
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 @@ def _format_callback(func, args, suffix=''):
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 --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py
index 7c901f27aa..b3f35ceba6 100644
--- a/Lib/test/test_asyncio/test_events.py
+++ b/Lib/test/test_asyncio/test_events.py
@@ -2224,7 +2224,7 @@ else:
return asyncio.SelectorEventLoop(selectors.SelectSelector())
-def noop(*args):
+def noop(*args, **kwargs):
pass
@@ -2305,6 +2305,13 @@ class HandleTests(test_utils.TestCase):
% (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'^<Handle noop\(x=1\)\(2, 3\) at %s:%s>$'
+ % (re.escape(filename), lineno))
+ self.assertRegex(repr(h), regex)
+
# partial method
if sys.version_info >= (3, 4):
method = HandleTests.test_handle_repr
diff --git a/Misc/NEWS b/Misc/NEWS
index b63aafd96b..ebd1144d63 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -269,6 +269,9 @@ Library
- 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
----